inventory_system.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use specs::prelude::*;
  2. use super::{WantsToPickupItem, Name, InBackpack,
  3. Position, GameLog, WantsToDrinkPotion,
  4. CombatStats, Potion, WantsToDropItem};
  5. pub struct ItemCollectionSystem {}
  6. impl<'a> System<'a> for ItemCollectionSystem {
  7. #[allow(clippy::type_complexity)]
  8. type SystemData = ( ReadExpect<'a, Entity>,
  9. WriteExpect<'a, GameLog>,
  10. WriteStorage<'a, WantsToPickupItem>,
  11. WriteStorage<'a, Position>,
  12. ReadStorage<'a, Name>,
  13. WriteStorage<'a, InBackpack>
  14. );
  15. fn run(&mut self, data : Self::SystemData) {
  16. let (player_entity, mut gamelog, mut wants_pickup, mut positions, names, mut backpack) = data;
  17. for pickup in wants_pickup.join() {
  18. positions.remove(pickup.item);
  19. backpack.insert(pickup.item, InBackpack{ owner: pickup.collected_by }).expect("Unable to insert backpack entry");
  20. if pickup.collected_by == *player_entity {
  21. gamelog.entries.push(format!("You pick up the {}.", names.get(pickup.item).unwrap().name));
  22. }
  23. }
  24. wants_pickup.clear();
  25. }
  26. }
  27. pub struct PotionUseSystem {}
  28. impl<'a> System<'a> for PotionUseSystem {
  29. #[allow(clippy::type_complexity)]
  30. type SystemData = ( ReadExpect<'a, Entity>,
  31. WriteExpect<'a, GameLog>,
  32. Entities<'a>,
  33. WriteStorage<'a, WantsToDrinkPotion>,
  34. ReadStorage<'a, Name>,
  35. ReadStorage<'a, Potion>,
  36. WriteStorage<'a, CombatStats>
  37. );
  38. fn run(&mut self, data : Self::SystemData) {
  39. let (player_entity, mut gamelog, entities, mut wants_drink, names, potions, mut combat_stats) = data;
  40. for (entity, drink, stats) in (&entities, &wants_drink, &mut combat_stats).join() {
  41. let potion = potions.get(drink.potion);
  42. match potion {
  43. None => {}
  44. Some(potion) => {
  45. stats.hp = i32::min(stats.max_hp, stats.hp + potion.heal_amount);
  46. if entity == *player_entity {
  47. gamelog.entries.push(format!("You drink the {}, healing {} hp.", names.get(drink.potion).unwrap().name, potion.heal_amount));
  48. }
  49. entities.delete(drink.potion).expect("Delete failed");
  50. }
  51. }
  52. }
  53. wants_drink.clear();
  54. }
  55. }
  56. pub struct ItemDropSystem {}
  57. impl<'a> System<'a> for ItemDropSystem {
  58. #[allow(clippy::type_complexity)]
  59. type SystemData = ( ReadExpect<'a, Entity>,
  60. WriteExpect<'a, GameLog>,
  61. Entities<'a>,
  62. WriteStorage<'a, WantsToDropItem>,
  63. ReadStorage<'a, Name>,
  64. WriteStorage<'a, Position>,
  65. WriteStorage<'a, InBackpack>
  66. );
  67. fn run(&mut self, data : Self::SystemData) {
  68. let (player_entity, mut gamelog, entities, mut wants_drop, names, mut positions, mut backpack) = data;
  69. for (entity, to_drop) in (&entities, &wants_drop).join() {
  70. let mut dropper_pos : Position = Position{x:0, y:0};
  71. {
  72. let dropped_pos = positions.get(entity).unwrap();
  73. dropper_pos.x = dropped_pos.x;
  74. dropper_pos.y = dropped_pos.y;
  75. }
  76. positions.insert(to_drop.item, Position{ x : dropper_pos.x, y : dropper_pos.y }).expect("Unable to insert position");
  77. backpack.remove(to_drop.item);
  78. if entity == *player_entity {
  79. gamelog.entries.push(format!("You drop the {}.", names.get(to_drop.item).unwrap().name));
  80. }
  81. }
  82. wants_drop.clear();
  83. }
  84. }