Add method to remove gems with bounds check
This commit is contained in:
parent
67d6396e60
commit
b417c53a01
4 changed files with 23 additions and 7 deletions
|
@ -61,7 +61,12 @@ fn main() -> BError {
|
||||||
for (i, tile) in &mut map.get_tiles().iter().enumerate() {
|
for (i, tile) in &mut map.get_tiles().iter().enumerate() {
|
||||||
if rng.roll_dice(1, 16) < 2 && *tile == TileType::Floor {
|
if rng.roll_dice(1, 16) < 2 && *tile == TileType::Floor {
|
||||||
let position = map.index_to_point2d(i);
|
let position = map.index_to_point2d(i);
|
||||||
let kind = MonsterKind::Slow;
|
let kind = match rng.range(0, 3) {
|
||||||
|
0 => MonsterKind::Slow,
|
||||||
|
1 => MonsterKind::Medium,
|
||||||
|
2 => MonsterKind::Fast,
|
||||||
|
_ => MonsterKind::Slow,
|
||||||
|
};
|
||||||
ecs.create_entity()
|
ecs.create_entity()
|
||||||
.with(Position {
|
.with(Position {
|
||||||
x: position.x,
|
x: position.x,
|
||||||
|
|
|
@ -48,3 +48,18 @@ pub struct Stats {
|
||||||
pub teleports: u32,
|
pub teleports: u32,
|
||||||
pub keys: u32,
|
pub keys: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PlayerSurvived = bool;
|
||||||
|
|
||||||
|
impl Stats {
|
||||||
|
pub fn take_gems(&mut self, num_gems: u32) -> PlayerSurvived {
|
||||||
|
let new_num_gems = self.gems as i64 - num_gems as i64;
|
||||||
|
if new_num_gems <= 0 {
|
||||||
|
self.gems = 0;
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
self.gems = new_num_gems as u32;
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -111,9 +111,7 @@ impl State {
|
||||||
} else {
|
} else {
|
||||||
if let Some(e) = map.get_tile_content(map.point2d_to_index(destination)) {
|
if let Some(e) = map.get_tile_content(map.point2d_to_index(destination)) {
|
||||||
if let Some(monster) = monsters.get(e) {
|
if let Some(monster) = monsters.get(e) {
|
||||||
if stats.gems > 0 {
|
stats.take_gems(damage_for_kind(monster.kind));
|
||||||
stats.gems -= damage_for_kind(monster.kind);
|
|
||||||
}
|
|
||||||
let _ = entities.delete(e);
|
let _ = entities.delete(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,9 +77,7 @@ impl<'a> System<'a> for MonsterAiSystem {
|
||||||
if let Some(e) = map.get_tile_content(map.point2d_to_index(destination)) {
|
if let Some(e) = map.get_tile_content(map.point2d_to_index(destination)) {
|
||||||
if let Some(_player) = players.get(e) {
|
if let Some(_player) = players.get(e) {
|
||||||
// TODO: Sound
|
// TODO: Sound
|
||||||
if stats.gems > 0 {
|
stats.take_gems(damage_for_kind(monster.kind));
|
||||||
stats.gems -= damage_for_kind(monster.kind);
|
|
||||||
}
|
|
||||||
let _ = entities.delete(*entity);
|
let _ = entities.delete(*entity);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue