From e154577012e6f47102703010b2fe5db1ff3dca4f Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 23 Dec 2021 12:05:56 -0500 Subject: [PATCH] Simplify spawning of items, as they are now created by the raws --- src/raws.rs | 4 +- src/raws/item_structs.rs | 13 +++ src/raws/rawmaster.rs | 24 ++++- src/spawner.rs | 194 +-------------------------------------- 4 files changed, 38 insertions(+), 197 deletions(-) diff --git a/src/raws.rs b/src/raws.rs index 80ae80d..ccbd56a 100644 --- a/src/raws.rs +++ b/src/raws.rs @@ -21,8 +21,8 @@ pub fn load_raws() { .unwrap(); let raw_string = - std::str::from_utf8(&raw_data).expect("Unable to convert to a valid UTF-8 string."); - let decoder: Raws = serde_json::from_str(&raw_string).expect("Unable to parse JSON"); + std::str::from_utf8(raw_data).expect("Unable to convert to a valid UTF-8 string."); + let decoder: Raws = serde_json::from_str(raw_string).expect("Unable to parse JSON"); RAWS.lock().unwrap().load(decoder); } diff --git a/src/raws/item_structs.rs b/src/raws/item_structs.rs index 2a414a3..862dd08 100644 --- a/src/raws/item_structs.rs +++ b/src/raws/item_structs.rs @@ -12,6 +12,8 @@ pub struct Item { pub name: String, pub renderable: Option, pub consumable: Option, + pub weapon: Option, + pub shield: Option, } #[derive(Deserialize, Debug)] @@ -26,3 +28,14 @@ pub struct Renderable { pub struct Consumable { pub effects: HashMap, } + +#[derive(Deserialize, Debug)] +pub struct Weapon { + pub range: String, + pub power_bonus: i32, +} + +#[derive(Deserialize, Debug)] +pub struct Shield { + pub defense_bonus: i32, +} diff --git a/src/raws/rawmaster.rs b/src/raws/rawmaster.rs index a0d005f..ea60f75 100644 --- a/src/raws/rawmaster.rs +++ b/src/raws/rawmaster.rs @@ -60,7 +60,9 @@ pub fn spawn_named_item( }); } - eb = eb.with(Name::from(&item_template.name)).with(Item {}); + eb = eb.with(Name::from(&item_template.name)); + + eb = eb.with(Item {}); if let Some(consumable) = &item_template.consumable { eb = eb.with(Consumable {}); @@ -93,7 +95,7 @@ pub fn spawn_named_item( }) } "magic_mapping" => eb = eb.with(MagicMapper {}), - "food" => [eb = eb.with(ProvidesFood {})], + "food" => eb = eb.with(ProvidesFood {}), _ => { rltk::console::log(format!( "Warning: consumable effect {} not implemented.", @@ -104,6 +106,24 @@ pub fn spawn_named_item( } } + if let Some(weapon) = &item_template.weapon { + eb = eb.with(Equippable { + slot: EquipmentSlot::Melee, + }); + eb = eb.with(MeleePowerBonus { + power: weapon.power_bonus, + }); + } + + if let Some(shield) = &item_template.shield { + eb = eb.with(Equippable { + slot: EquipmentSlot::Shield, + }); + eb = eb.with(DefenseBonus { + defense: shield.defense_bonus, + }); + } + return Some(eb.build()); } diff --git a/src/spawner.rs b/src/spawner.rs index 5b69d34..71bc957 100644 --- a/src/spawner.rs +++ b/src/spawner.rs @@ -136,7 +136,7 @@ pub fn spawn_entity(ecs: &mut World, spawn: &(&usize, &String)) { let item_result = spawn_named_item( &RAWS.lock().unwrap(), ecs.create_entity(), - &spawn.1, + spawn.1, SpawnType::AtPosition { x, y }, ); if item_result.is_some() { @@ -146,16 +146,6 @@ pub fn spawn_entity(ecs: &mut World, spawn: &(&usize, &String)) { match spawn.1.as_ref() { "Goblin" => goblin(ecs, x, y), "Orc" => orc(ecs, x, y), - "Health Potion" => health_potion(ecs, x, y), - "Fireball Scroll" => fireball_scroll(ecs, x, y), - "Confusion Scroll" => confusion_scroll(ecs, x, y), - "Magic Missile Scroll" => magic_missile_scroll(ecs, x, y), - "Dagger" => dagger(ecs, x, y), - "Shield" => shield(ecs, x, y), - "Longsword" => longsword(ecs, x, y), - "Tower Shield" => tower_shield(ecs, x, y), - "Rations" => rations(ecs, x, y), - "Magic Mapping Scroll" => magic_mapping_scroll(ecs, x, y), "Bear Trap" => bear_trap(ecs, x, y), "Door" => door(ecs, x, y), _ => {} @@ -193,188 +183,6 @@ fn monster(ecs: &mut World, x: i32, y: i32, glyph: rltk::FontCharTy .build(); } -fn health_potion(ecs: &mut World, x: i32, y: i32) { - ecs.create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph: rltk::to_cp437('ยก'), - fg: RGB::named(rltk::MAGENTA), - bg: RGB::named(rltk::BLACK), - render_order: 2, - }) - .with(Name::from("Health Potion")) - .with(Item {}) - .with(Consumable {}) - .with(ProvidesHealing { heal_amount: 8 }) - .marked::>() - .build(); -} - -fn magic_missile_scroll(ecs: &mut World, x: i32, y: i32) { - ecs.create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph: rltk::to_cp437(')'), - fg: RGB::named(rltk::CYAN), - bg: RGB::named(rltk::BLACK), - render_order: 2, - }) - .with(Name::from("Magic Missile Scroll")) - .with(Item {}) - .with(Consumable {}) - .with(Ranged { range: 6 }) - .with(InflictsDamage { damage: 20 }) - .marked::>() - .build(); -} - -fn fireball_scroll(ecs: &mut World, x: i32, y: i32) { - ecs.create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph: rltk::to_cp437(')'), - fg: RGB::named(rltk::ORANGE), - bg: RGB::named(rltk::BLACK), - render_order: 2, - }) - .with(Name::from("Fireball Scroll")) - .with(Item {}) - .with(Consumable {}) - .with(Ranged { range: 6 }) - .with(InflictsDamage { damage: 20 }) - .with(AreaOfEffect { radius: 3 }) - .marked::>() - .build(); -} - -fn confusion_scroll(ecs: &mut World, x: i32, y: i32) { - ecs.create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph: rltk::to_cp437(')'), - fg: RGB::named(rltk::PINK), - bg: RGB::named(rltk::BLACK), - render_order: 2, - }) - .with(Name::from("Confusion Scroll")) - .with(Item {}) - .with(Consumable {}) - .with(Ranged { range: 6 }) - .with(Confusion { turns: 4 }) - .marked::>() - .build(); -} - -fn dagger(ecs: &mut World, x: i32, y: i32) { - ecs.create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph: rltk::to_cp437('/'), - fg: RGB::named(rltk::CYAN), - bg: RGB::named(rltk::BLACK), - render_order: 2, - }) - .with(Name::from("Dagger")) - .with(Item {}) - .with(Equippable { - slot: EquipmentSlot::Melee, - }) - .with(MeleePowerBonus { power: 2 }) - .marked::>() - .build(); -} - -fn shield(ecs: &mut World, x: i32, y: i32) { - ecs.create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph: rltk::to_cp437('('), - fg: RGB::named(rltk::CYAN), - bg: RGB::named(rltk::BLACK), - render_order: 2, - }) - .with(Name::from("Shield")) - .with(Item {}) - .with(Equippable { - slot: EquipmentSlot::Shield, - }) - .with(DefenseBonus { defense: 1 }) - .marked::>() - .build(); -} - -fn longsword(ecs: &mut World, x: i32, y: i32) { - ecs.create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph: rltk::to_cp437('/'), - fg: RGB::named(rltk::CYAN), - bg: RGB::named(rltk::BLACK), - render_order: 2, - }) - .with(Name::from("Longsword")) - .with(Item {}) - .with(Equippable { - slot: EquipmentSlot::Melee, - }) - .with(MeleePowerBonus { power: 4 }) - .marked::>() - .build(); -} - -fn tower_shield(ecs: &mut World, x: i32, y: i32) { - ecs.create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph: rltk::to_cp437('('), - fg: RGB::named(rltk::CYAN), - bg: RGB::named(rltk::BLACK), - render_order: 2, - }) - .with(Name::from("Tower Shield")) - .with(Item {}) - .with(Equippable { - slot: EquipmentSlot::Shield, - }) - .with(DefenseBonus { defense: 3 }) - .marked::>() - .build(); -} - -fn rations(ecs: &mut World, x: i32, y: i32) { - ecs.create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph: rltk::to_cp437('%'), - fg: RGB::named(rltk::GREEN), - bg: RGB::named(rltk::BLACK), - render_order: 2, - }) - .with(Name::from("Rations")) - .with(Item {}) - .with(ProvidesFood {}) - .with(Consumable {}) - .marked::>() - .build(); -} - -fn magic_mapping_scroll(ecs: &mut World, x: i32, y: i32) { - ecs.create_entity() - .with(Position { x, y }) - .with(Renderable { - glyph: rltk::to_cp437(')'), - fg: RGB::named(rltk::CYAN3), - bg: RGB::named(rltk::BLACK), - render_order: 2, - }) - .with(Name::from("Scroll of Magic Mapping")) - .with(Item {}) - .with(MagicMapper {}) - .with(Consumable {}) - .marked::>() - .build(); -} - fn bear_trap(ecs: &mut World, x: i32, y: i32) { ecs.create_entity() .with(Position { x, y })