Simplify spawning of items, as they are now created by the raws

This commit is contained in:
Timothy Warren 2021-12-23 12:05:56 -05:00
parent 23e187c7fd
commit e154577012
4 changed files with 38 additions and 197 deletions

View File

@ -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);
}

View File

@ -12,6 +12,8 @@ pub struct Item {
pub name: String,
pub renderable: Option<Renderable>,
pub consumable: Option<Consumable>,
pub weapon: Option<Weapon>,
pub shield: Option<Shield>,
}
#[derive(Deserialize, Debug)]
@ -26,3 +28,14 @@ pub struct Renderable {
pub struct Consumable {
pub effects: HashMap<String, String>,
}
#[derive(Deserialize, Debug)]
pub struct Weapon {
pub range: String,
pub power_bonus: i32,
}
#[derive(Deserialize, Debug)]
pub struct Shield {
pub defense_bonus: i32,
}

View File

@ -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());
}

View File

@ -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<S: ToString>(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::<SimpleMarker<SerializeMe>>()
.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::<SimpleMarker<SerializeMe>>()
.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::<SimpleMarker<SerializeMe>>()
.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::<SimpleMarker<SerializeMe>>()
.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::<SimpleMarker<SerializeMe>>()
.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::<SimpleMarker<SerializeMe>>()
.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::<SimpleMarker<SerializeMe>>()
.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::<SimpleMarker<SerializeMe>>()
.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::<SimpleMarker<SerializeMe>>()
.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::<SimpleMarker<SerializeMe>>()
.build();
}
fn bear_trap(ecs: &mut World, x: i32, y: i32) {
ecs.create_entity()
.with(Position { x, y })