init commit

This commit is contained in:
Dane Johnson 2023-10-15 23:41:11 -05:00
commit 8677e05fe1
8 changed files with 3853 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

3694
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "minibaldur"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = "0.11.2"
rand = "0.8.5"

22
src/characters.rs Normal file
View File

@ -0,0 +1,22 @@
use bevy::prelude::Commands;
use crate::components::*;
use crate::util::*;
pub fn spawn_player_character(mut commands: Commands) {
let (hp, _) = roll("1d6 + 3");
commands.spawn(Creature {
health: Health {
hp,
max: hp,
},
armor: Armor {
ac: 10,
},
position: Position {
x: 0.0,
y: 0.0,
},
})
.insert(PlayerCharacter);
}

32
src/components.rs Normal file
View File

@ -0,0 +1,32 @@
use std::collections::HashMap;
use bevy::prelude::*;
#[derive(Component)]
pub struct Health {
pub hp: u32,
pub max: u32,
}
#[derive(Component)]
pub struct Armor {
pub ac: u32,
}
#[derive(Component)]
pub struct Position {
pub x: f32,
pub y: f32,
}
#[derive(Bundle)]
pub struct Creature {
pub health: Health,
pub armor: Armor,
pub position: Position,
}
#[derive(Component)]
pub struct PlayerCharacter;
#[derive(Component)]
pub struct Battle(pub HashMap<u32, Entity>);

17
src/main.rs Normal file
View File

@ -0,0 +1,17 @@
use bevy::prelude::*;
pub mod util;
pub mod components;
pub mod systems;
pub mod characters;
use crate::characters::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// Add systems, whatnot
.add_systems(Startup, spawn_player_character)
.add_systems(Startup, spawn_player_character)
.run();
}

15
src/systems.rs Normal file
View File

@ -0,0 +1,15 @@
use std::collections::HashMap;
use bevy::prelude::*;
use crate::components::*;
use crate::util::roll;
pub fn initiate_battle(query: Query<Entity, With<PlayerCharacter>>, mut commands: Commands) {
let order: HashMap<u32, Entity> = query.iter().map(|ent| {
let (total, _rolls) = roll("1d20");
(total, ent)
}).collect();
commands.spawn(Battle(order));
}

62
src/util.rs Normal file
View File

@ -0,0 +1,62 @@
use rand::prelude::*;
enum Tok {
Roll(u32),
Flat(u32),
}
use Tok::*;
/// Rolls dice corresponding to a ttrpg dice specifier
/// ```
/// assert_eq!(roll("4d6 + 1d4 + 1").1.len(), 2)
/// ```
pub fn roll(spec: &str) -> (u32, Vec<u32>) {
let mut toks = spec.split_whitespace();
let mut rolls = vec![];
let mut total = 0;
let first = toks.next().unwrap();
match parse_tok(first) {
Roll(v) => {
total += v;
rolls.push(v);
}
Flat(v) => total += v,
};
let toks : Vec<&str> = toks.collect();
let mut toks = toks.chunks(2);
while let Some(&[op, tok]) = toks.next() {
let val = match parse_tok(tok) {
Roll(v) => {
rolls.push(v);
v
}
Flat(v) => v,
};
if op == "-" {
total -= val;
} else {
total += val;
}
}
(total, rolls)
}
fn parse_tok(tok: &str) -> Tok {
let mut rng = thread_rng();
match *tok.split('d')
.map(|x| x.parse().unwrap())
.collect::<Vec<_>>()
.as_slice() {
[v] => Flat(v),
[n, d] => {
let mut total = 0;
for _ in 0..n {
total += rng.gen_range(1..=d)
}
Roll(total)
}
_ => panic!()
}
}