Add regex crate to parse dice specs for equipment

This commit is contained in:
Timothy Warren 2022-01-04 10:08:06 -05:00
parent 2e02e30bc6
commit d96d1ce003
6 changed files with 43 additions and 6 deletions

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
lazy_static = "1.4.0" lazy_static = "1.4.0"
regex = "1.5.4"
rltk = { version = "0.8.0", features=["serde"] } rltk = { version = "0.8.0", features=["serde"] }
specs = { version = "0.16.1", features=["serde"] } specs = { version = "0.16.1", features=["serde"] }
specs-derive = "0.4.1" specs-derive = "0.4.1"

View File

@ -178,9 +178,19 @@ pub struct Equipped {
pub slot: EquipmentSlot, pub slot: EquipmentSlot,
} }
#[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
pub enum WeaponAttribute {
Might,
Quickness,
}
#[derive(Component, ConvertSaveload, Clone)] #[derive(Component, ConvertSaveload, Clone)]
pub struct MeleePowerBonus { pub struct MeleeWeapon {
pub power: i32, pub attribute: WeaponAttribute,
pub damage_n_dice: i32,
pub damage_die_type: i32,
pub damage_bonus: i32,
pub hit_bonus: i32,
} }
#[derive(Component, ConvertSaveload, Clone)] #[derive(Component, ConvertSaveload, Clone)]

View File

@ -518,7 +518,7 @@ fn main() -> ::rltk::BError {
SerializationHelper, SerializationHelper,
Equippable, Equippable,
Equipped, Equipped,
MeleePowerBonus, MeleeWeapon,
DefenseBonus, DefenseBonus,
WantsToRemoveItem, WantsToRemoveItem,
ParticleLifetime, ParticleLifetime,

View File

@ -27,7 +27,9 @@ pub struct Consumable {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Weapon { pub struct Weapon {
pub range: String, pub range: String,
pub power_bonus: i32, pub attribute: String,
pub base_damage: String,
pub hit_bonus: i32,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]

View File

@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use ::regex::Regex;
use ::specs::prelude::*; use ::specs::prelude::*;
use crate::components::*; use crate::components::*;
@ -7,6 +8,29 @@ use crate::gamesystem::{mana_at_level, npc_hp};
use crate::random_table::RandomTable; use crate::random_table::RandomTable;
use crate::raws::Raws; use crate::raws::Raws;
pub fn parse_dice_string(dice: &str) -> (i32, i32, i32) {
lazy_static! {
static ref DICE_RE: Regex = Regex::new(r"(\d+)d(\d+)([\+\-]\d+)?").unwrap();
}
let mut n_dice = 1;
let mut die_type = 4;
let mut die_bonus = 0;
for cap in DIC_RE.captures_iter(dice) {
if let Some(group) = cap.get(1) {
n_dice = group.as_str().parse::<i32>().expect("Not a digit");
}
if let Some(group) = cap.get(2) {
die_type = group.as_str().parse::<i32>().expect("Not a digit");
}
if let Some(group) = cap.get(3) {
die_bonus = group.as_str().parse::<i32>().expect("Not a digit");
}
}
(n_dice, die_type, die_bonus)
}
pub enum SpawnType { pub enum SpawnType {
AtPosition { x: i32, y: i32 }, AtPosition { x: i32, y: i32 },
} }

View File

@ -75,7 +75,7 @@ pub fn save_game(ecs: &mut World) {
SerializationHelper, SerializationHelper,
Equippable, Equippable,
Equipped, Equipped,
MeleePowerBonus, MeleeWeapon,
DefenseBonus, DefenseBonus,
WantsToRemoveItem, WantsToRemoveItem,
ParticleLifetime, ParticleLifetime,
@ -171,7 +171,7 @@ pub fn load_game(ecs: &mut World) {
SerializationHelper, SerializationHelper,
Equippable, Equippable,
Equipped, Equipped,
MeleePowerBonus, MeleeWeapon,
DefenseBonus, DefenseBonus,
WantsToRemoveItem, WantsToRemoveItem,
ParticleLifetime, ParticleLifetime,