Add player-on-monster collision
This commit is contained in:
parent
0de96c7593
commit
e90a10262b
3 changed files with 25 additions and 3 deletions
|
@ -39,3 +39,11 @@ pub fn ticks_for_kind(kind: MonsterKind) -> i32 {
|
||||||
MonsterKind::Fast => 6,
|
MonsterKind::Fast => 6,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn damage_for_kind(kind: MonsterKind) -> u32 {
|
||||||
|
match kind {
|
||||||
|
MonsterKind::Slow => 1,
|
||||||
|
MonsterKind::Medium => 2,
|
||||||
|
MonsterKind::Fast => 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -12,7 +12,7 @@ pub mod vga_color;
|
||||||
|
|
||||||
use bracket_lib::prelude::*;
|
use bracket_lib::prelude::*;
|
||||||
use components::{
|
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,
|
Monster, Player, Position, Renderable,
|
||||||
};
|
};
|
||||||
use constants::*;
|
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) {
|
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 positions = ecs.write_storage::<Position>();
|
||||||
let mut players = ecs.write_storage::<Player>();
|
let mut players = ecs.write_storage::<Player>();
|
||||||
let mut map = ecs.write_resource::<Map>();
|
let mut map = ecs.write_resource::<Map>();
|
||||||
|
let mut stats = ecs.write_resource::<Stats>();
|
||||||
|
|
||||||
for (player, pos) in (&mut players, &mut positions).join() {
|
for (player, pos) in (&mut players, &mut positions).join() {
|
||||||
let now = Instant::now();
|
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) {
|
if map.is_solid(destination) {
|
||||||
sound_system.play_sound(sound_effects.blocked.clone());
|
sound_system.play_sound(sound_effects.blocked.clone());
|
||||||
} else {
|
} 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(Point { x: pos.x, y: pos.y }, TileType::Floor);
|
||||||
map.set_tile_at(destination, TileType::Player);
|
map.set_tile_at(destination, TileType::Player);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
components::{
|
components::{
|
||||||
monster::{self, ticks_for_kind},
|
monster::{self, damage_for_kind, ticks_for_kind},
|
||||||
Monster, Position, Renderable,
|
Monster, Position, Renderable,
|
||||||
},
|
},
|
||||||
map::{Map, TileType},
|
map::{Map, TileType},
|
||||||
|
@ -77,7 +77,9 @@ impl<'a> System<'a> for MonsterMotion {
|
||||||
match map.get_tile_at(destination) {
|
match map.get_tile_at(destination) {
|
||||||
TileType::Player => {
|
TileType::Player => {
|
||||||
// TODO: Sound
|
// TODO: Sound
|
||||||
stats.gems -= 1;
|
if stats.gems > 0 {
|
||||||
|
stats.gems -= damage_for_kind(monster.kind);
|
||||||
|
}
|
||||||
map.set_tile_at(
|
map.set_tile_at(
|
||||||
Point {
|
Point {
|
||||||
x: position.x,
|
x: position.x,
|
||||||
|
|
Loading…
Add table
Reference in a new issue