From d4d25955ccc8a1adb7d9f7f656f96ded6f77ae47 Mon Sep 17 00:00:00 2001 From: Llywelwyn Date: Sun, 9 Jul 2023 09:23:52 +0100 Subject: [PATCH] destructible items --- src/components.rs | 3 +++ src/inventory_system.rs | 30 +++++++++++++++++++++--------- src/main.rs | 1 + src/spawner.rs | 9 +++++++-- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/components.rs b/src/components.rs index 3d01d36..5087f56 100644 --- a/src/components.rs +++ b/src/components.rs @@ -114,6 +114,9 @@ pub struct WantsToUseItem { #[derive(Component, Debug)] pub struct Consumable {} +#[derive(Component, Debug)] +pub struct Destructible {} + #[derive(Component, Clone)] pub struct ParticleLifetime { pub lifetime_ms: f32, diff --git a/src/inventory_system.rs b/src/inventory_system.rs index 2cfbdcf..33b525c 100644 --- a/src/inventory_system.rs +++ b/src/inventory_system.rs @@ -1,6 +1,7 @@ use super::{ - gamelog::GameLog, CombatStats, Consumable, InBackpack, InflictsDamage, Map, Name, ParticleBuilder, Position, - ProvidesHealing, SufferDamage, WantsToDropItem, WantsToPickupItem, WantsToUseItem, AOE, DEFAULT_PARTICLE_LIFETIME, + gamelog::GameLog, CombatStats, Consumable, Destructible, InBackpack, InflictsDamage, Map, Name, ParticleBuilder, + Position, ProvidesHealing, SufferDamage, WantsToDropItem, WantsToPickupItem, WantsToUseItem, AOE, + DEFAULT_PARTICLE_LIFETIME, }; use specs::prelude::*; @@ -44,6 +45,7 @@ impl<'a> System<'a> for ItemUseSystem { WriteStorage<'a, WantsToUseItem>, ReadStorage<'a, Name>, ReadStorage<'a, Consumable>, + ReadStorage<'a, Destructible>, ReadStorage<'a, ProvidesHealing>, WriteStorage<'a, CombatStats>, WriteStorage<'a, SufferDamage>, @@ -62,6 +64,7 @@ impl<'a> System<'a> for ItemUseSystem { mut wants_to_use, names, consumables, + destructibles, provides_healing, mut combat_stats, mut suffer_damage, @@ -165,13 +168,22 @@ impl<'a> System<'a> for ItemUseSystem { ); } for mob in targets.iter() { - SufferDamage::new_damage(&mut suffer_damage, *mob, damage.amount); - if entity == *player_entity { - let mob_name = names.get(*mob).unwrap(); - gamelog.entries.push(format!( - "{} takes {} damage from the {}!", - mob_name.name, damage.amount, item_being_used.name - )); + let destructible = destructibles.get(*mob); + let entity_name = names.get(*mob).unwrap(); + match destructible { + None => { + SufferDamage::new_damage(&mut suffer_damage, *mob, damage.amount); + if entity == *player_entity { + gamelog.entries.push(format!( + "{} takes {} damage from the {}!", + entity_name.name, damage.amount, item_being_used.name + )); + } + } + Some(_destructible) => { + gamelog.entries.push(format!("{} is destroyed!", entity_name.name)); + entities.delete(*mob).expect("Delete failed"); + } } used_item = true; diff --git a/src/main.rs b/src/main.rs index b1b6c0f..23eeb7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -265,6 +265,7 @@ fn main() -> rltk::BError { gs.ecs.register::(); gs.ecs.register::(); gs.ecs.register::(); + gs.ecs.register::(); gs.ecs.register::(); let map = Map::new_map_rooms_and_corridors(); diff --git a/src/spawner.rs b/src/spawner.rs index b801464..d793929 100644 --- a/src/spawner.rs +++ b/src/spawner.rs @@ -1,6 +1,6 @@ use super::{ - BlocksTile, CombatStats, Consumable, InflictsDamage, Item, Monster, Name, Player, Position, ProvidesHealing, - Ranged, Rect, Renderable, Viewshed, AOE, MAPWIDTH, + BlocksTile, CombatStats, Consumable, Destructible, InflictsDamage, Item, Monster, Name, Player, Position, + ProvidesHealing, Ranged, Rect, Renderable, Viewshed, AOE, MAPWIDTH, }; use rltk::{RandomNumberGenerator, RGB}; use specs::prelude::*; @@ -140,6 +140,7 @@ fn health_potion(ecs: &mut World, x: i32, y: i32) { .with(Name { name: "potion of health".to_string() }) .with(Item {}) .with(Consumable {}) + .with(Destructible {}) .with(ProvidesHealing { amount: 12 }) .build(); } @@ -156,6 +157,7 @@ fn weak_health_potion(ecs: &mut World, x: i32, y: i32) { .with(Name { name: "potion of lesser health".to_string() }) .with(Item {}) .with(Consumable {}) + .with(Destructible {}) .with(ProvidesHealing { amount: 6 }) .build(); } @@ -172,6 +174,7 @@ fn poison_potion(ecs: &mut World, x: i32, y: i32) { .with(Name { name: "potion of ... health?".to_string() }) .with(Item {}) .with(Consumable {}) + .with(Destructible {}) .with(ProvidesHealing { amount: -12 }) .build(); } @@ -188,6 +191,7 @@ fn magic_missile_scroll(ecs: &mut World, x: i32, y: i32) { .with(Name { name: "scroll of magic missile".to_string() }) .with(Item {}) .with(Consumable {}) + .with(Destructible {}) .with(Ranged { range: 12 }) .with(InflictsDamage { amount: 10 }) .build(); @@ -205,6 +209,7 @@ fn fireball_scroll(ecs: &mut World, x: i32, y: i32) { .with(Name { name: "scroll of fireball".to_string() }) .with(Item {}) .with(Consumable {}) + .with(Destructible {}) .with(Ranged { range: 10 }) .with(InflictsDamage { amount: 20 }) .with(AOE { radius: 3 })