hunger_system.rs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. use specs::prelude::*;
  2. use super::{HungerClock, RunState, HungerState, SufferDamage, gamelog::GameLog};
  3. pub struct HungerSystem {}
  4. impl<'a> System<'a> for HungerSystem {
  5. #[allow(clippy::type_complexity)]
  6. type SystemData = (
  7. Entities<'a>,
  8. WriteStorage<'a, HungerClock>,
  9. ReadExpect<'a, Entity>, // The player
  10. ReadExpect<'a, RunState>,
  11. WriteStorage<'a, SufferDamage>,
  12. WriteExpect<'a, GameLog>
  13. );
  14. fn run(&mut self, data : Self::SystemData) {
  15. let (entities, mut hunger_clock, player_entity, runstate, mut inflict_damage, mut log) = data;
  16. for (entity, mut clock) in (&entities, &mut hunger_clock).join() {
  17. let mut proceed = false;
  18. match *runstate {
  19. RunState::PlayerTurn => {
  20. if entity == *player_entity {
  21. proceed = true;
  22. }
  23. }
  24. RunState::MonsterTurn => {
  25. if entity != *player_entity {
  26. proceed = false;
  27. }
  28. }
  29. _ => proceed = false
  30. }
  31. if proceed {
  32. clock.duration -= 1;
  33. if clock.duration < 1 {
  34. match clock.state {
  35. HungerState::WellFed => {
  36. clock.state = HungerState::Normal;
  37. clock.duration = 200;
  38. if entity == *player_entity {
  39. log.entries.insert(0, "You are no longer well fed.".to_string());
  40. }
  41. }
  42. HungerState::Normal => {
  43. clock.state = HungerState::Hungry;
  44. clock.duration = 200;
  45. if entity == *player_entity {
  46. log.entries.insert(0, "You are hungry.".to_string());
  47. }
  48. }
  49. HungerState::Hungry => {
  50. clock.state = HungerState::Starving;
  51. clock.duration = 200;
  52. if entity == *player_entity {
  53. log.entries.insert(0, "You are starving!".to_string());
  54. }
  55. }
  56. HungerState::Starving => {
  57. // Inflict damage from hunger
  58. if entity == *player_entity {
  59. log.entries.insert(0, "Your hunger pangs are getting painful! You suffer 1 hp damage.".to_string());
  60. }
  61. inflict_damage.insert(entity, SufferDamage{ amount: 1 }).expect("Unable to do damage");
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }