diff --git a/src/components/monster.rs b/src/components/monster.rs index 98da013..f254f50 100644 --- a/src/components/monster.rs +++ b/src/components/monster.rs @@ -39,3 +39,11 @@ pub fn ticks_for_kind(kind: MonsterKind) -> i32 { MonsterKind::Fast => 6, } } + +pub fn damage_for_kind(kind: MonsterKind) -> u32 { + match kind { + MonsterKind::Slow => 1, + MonsterKind::Medium => 2, + MonsterKind::Fast => 3, + } +} diff --git a/src/main.rs b/src/main.rs index c3d8a5d..a2d4329 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ pub mod vga_color; use bracket_lib::prelude::*; use components::{ - monster::{color_for_kind, glyphs_for_kind, ticks_for_kind, MonsterKind}, + monster::{color_for_kind, damage_for_kind, glyphs_for_kind, ticks_for_kind, MonsterKind}, Monster, Player, Position, Renderable, }; use constants::*; @@ -31,9 +31,11 @@ struct State { } fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World, sound_system: &mut SoundSystem) { + let entities = ecs.entities(); let mut positions = ecs.write_storage::(); let mut players = ecs.write_storage::(); let mut map = ecs.write_resource::(); + let mut stats = ecs.write_resource::(); for (player, pos) in (&mut players, &mut positions).join() { let now = Instant::now(); @@ -49,6 +51,16 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World, sound_system: &m if map.is_solid(destination) { sound_system.play_sound(sound_effects.blocked.clone()); } else { + if let TileType::Monster(monster) = map.get_tile_at(destination) { + if let Some(monster_component) = + ecs.read_component::().get(monster) + { + if stats.gems > 0 { + stats.gems -= damage_for_kind(monster_component.kind); + } + let _ = entities.delete(monster); + } + } map.set_tile_at(Point { x: pos.x, y: pos.y }, TileType::Floor); map.set_tile_at(destination, TileType::Player); diff --git a/src/systems/monster_motion.rs b/src/systems/monster_motion.rs index b7a4d31..ed25367 100644 --- a/src/systems/monster_motion.rs +++ b/src/systems/monster_motion.rs @@ -1,6 +1,6 @@ use crate::{ components::{ - monster::{self, ticks_for_kind}, + monster::{self, damage_for_kind, ticks_for_kind}, Monster, Position, Renderable, }, map::{Map, TileType}, @@ -77,7 +77,9 @@ impl<'a> System<'a> for MonsterMotion { match map.get_tile_at(destination) { TileType::Player => { // TODO: Sound - stats.gems -= 1; + if stats.gems > 0 { + stats.gems -= damage_for_kind(monster.kind); + } map.set_tile_at( Point { x: position.x,