Add player-on-monster collision

This commit is contained in:
Alex Page 2022-01-26 18:44:36 -05:00
parent 0de96c7593
commit e90a10262b
3 changed files with 25 additions and 3 deletions

View file

@ -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,
}
}

View file

@ -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::<Position>();
let mut players = ecs.write_storage::<Player>();
let mut map = ecs.write_resource::<Map>();
let mut stats = ecs.write_resource::<Stats>();
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::<Monster>().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);

View file

@ -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,