Implement the hunger system
This commit is contained in:
parent
15a0f1779c
commit
c490ab114b
82
src/hunger_system.rs
Normal file
82
src/hunger_system.rs
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
use crate::components::{HungerClock, HungerState, SufferDamage};
|
||||||
|
use crate::{game_log::GameLog, RunState};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
pub struct HungerSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for HungerSystem {
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
type SystemData = (
|
||||||
|
Entities<'a>,
|
||||||
|
WriteStorage<'a, HungerClock>,
|
||||||
|
ReadExpect<'a, Entity>, // The player
|
||||||
|
ReadExpect<'a, RunState>,
|
||||||
|
WriteStorage<'a, SufferDamage>,
|
||||||
|
WriteExpect<'a, GameLog>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (entities, mut hunger_clock, player_entity, runstate, mut inflict_damage, mut log) =
|
||||||
|
data;
|
||||||
|
|
||||||
|
for (entity, mut clock) in (&entities, &mut hunger_clock).join() {
|
||||||
|
let mut proceed = false;
|
||||||
|
|
||||||
|
match *runstate {
|
||||||
|
RunState::PlayerTurn => {
|
||||||
|
if entity == *player_entity {
|
||||||
|
proceed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RunState::MonsterTurn => {
|
||||||
|
if entity != *player_entity {
|
||||||
|
proceed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => proceed = false,
|
||||||
|
}
|
||||||
|
|
||||||
|
if proceed {
|
||||||
|
clock.duration -= 1;
|
||||||
|
|
||||||
|
if clock.duration < 1 {
|
||||||
|
match clock.state {
|
||||||
|
HungerState::WellFed => {
|
||||||
|
clock.state = HungerState::Normal;
|
||||||
|
clock.duration = 200;
|
||||||
|
|
||||||
|
if entity == *player_entity {
|
||||||
|
log.entries.push("You are no longer well fed.".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
HungerState::Normal => {
|
||||||
|
clock.state = HungerState::Hungry;
|
||||||
|
clock.duration = 200;
|
||||||
|
|
||||||
|
if entity == *player_entity {
|
||||||
|
log.entries.push("You are hungry.".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
HungerState::Hungry => {
|
||||||
|
clock.state = HungerState::Starving;
|
||||||
|
clock.duration = 200;
|
||||||
|
|
||||||
|
if entity == *player_entity {
|
||||||
|
log.entries.push("You are starving!".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
HungerState::Starving => {
|
||||||
|
// Inflict damage from hunger
|
||||||
|
if entity == *player_entity {
|
||||||
|
log.entries
|
||||||
|
.push("Your hunger pangs are getting painful!".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
SufferDamage::new_damage(&mut inflict_damage, entity, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,7 @@ mod components;
|
|||||||
mod damage_system;
|
mod damage_system;
|
||||||
mod game_log;
|
mod game_log;
|
||||||
mod gui;
|
mod gui;
|
||||||
|
mod hunger_system;
|
||||||
mod inventory_system;
|
mod inventory_system;
|
||||||
mod map;
|
mod map;
|
||||||
mod map_indexing_system;
|
mod map_indexing_system;
|
||||||
@ -101,6 +102,9 @@ impl State {
|
|||||||
let mut item_remove = ItemRemoveSystem {};
|
let mut item_remove = ItemRemoveSystem {};
|
||||||
item_remove.run_now(&self.ecs);
|
item_remove.run_now(&self.ecs);
|
||||||
|
|
||||||
|
let mut hunger = hunger_system::HungerSystem {};
|
||||||
|
hunger.run_now(&self.ecs);
|
||||||
|
|
||||||
let mut particles = particle_system::ParticleSpawnSystem {};
|
let mut particles = particle_system::ParticleSpawnSystem {};
|
||||||
particles.run_now(&self.ecs);
|
particles.run_now(&self.ecs);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user