inventory_system.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. use specs::prelude::*;
  2. use crate::{Consumable, ProvidesHealing, InflictsDamage};
  3. use super::{WantsToPickupItem, Name, InBackpack,
  4. Position, GameLog, WantsToUseItem,
  5. CombatStats, Item, WantsToDropItem,
  6. Map, SufferDamage};
  7. pub struct ItemCollectionSystem {}
  8. impl<'a> System<'a> for ItemCollectionSystem {
  9. #[allow(clippy::type_complexity)]
  10. type SystemData = ( ReadExpect<'a, Entity>,
  11. WriteExpect<'a, GameLog>,
  12. WriteStorage<'a, WantsToPickupItem>,
  13. WriteStorage<'a, Position>,
  14. ReadStorage<'a, Name>,
  15. WriteStorage<'a, InBackpack>
  16. );
  17. fn run(&mut self, data : Self::SystemData) {
  18. let (player_entity, mut gamelog, mut wants_pickup, mut positions, names, mut backpack) = data;
  19. for pickup in wants_pickup.join() {
  20. positions.remove(pickup.item);
  21. backpack.insert(pickup.item, InBackpack{ owner: pickup.collected_by }).expect("Unable to insert backpack entry");
  22. if pickup.collected_by == *player_entity {
  23. gamelog.entries.push(format!("You pick up the {}.", names.get(pickup.item).unwrap().name));
  24. }
  25. }
  26. wants_pickup.clear();
  27. }
  28. }
  29. pub struct ItemUseSystem {}
  30. impl<'a> System<'a> for ItemUseSystem {
  31. #[allow(clippy::type_complexity)]
  32. type SystemData = ( ReadExpect<'a, Entity>,
  33. WriteExpect<'a, GameLog>,
  34. ReadExpect<'a, Map>,
  35. Entities<'a>,
  36. WriteStorage<'a, WantsToUseItem>,
  37. ReadStorage<'a, Name>,
  38. ReadStorage<'a, ProvidesHealing>,
  39. WriteStorage<'a, CombatStats>,
  40. ReadStorage<'a, Consumable>,
  41. ReadStorage<'a, Item>,
  42. ReadStorage<'a, InflictsDamage>,
  43. WriteStorage<'a, SufferDamage>
  44. );
  45. fn run(&mut self, data : Self::SystemData) {
  46. let (player_entity,
  47. mut gamelog,
  48. map,
  49. entities,
  50. mut wants_use,
  51. names,
  52. healing,
  53. mut combat_stats,
  54. consumables,
  55. item,
  56. inflict_damage,
  57. mut suffer_damage) = data;
  58. for (entity, useitem, stats) in (&entities, &wants_use, &mut combat_stats).join() {
  59. let mut used_item = true;
  60. // If it inflicts damage, apply it to the target cell
  61. let item_damages = inflict_damage.get(useitem.item);
  62. match item_damages {
  63. None => {}
  64. Some(damage) => {
  65. let target_point = useitem.target.unwrap();
  66. let idx = map.xy_idx(target_point.x, target_point.y);
  67. used_item = false;
  68. for mob in map.tile_content[idx].iter() {
  69. SufferDamage::new_damage(&mut suffer_damage, *mob, damage.damage);
  70. if entity == *player_entity {
  71. let mob_name = names.get(*mob).unwrap();
  72. let item_name = names.get(useitem.item).unwrap();
  73. gamelog.entries.push(format!("You use {} on {}, inflicting {} damage.", item_name.name, mob_name.name, damage.damage));
  74. }
  75. used_item = true;
  76. }
  77. }
  78. }
  79. let item_heals = healing.get(useitem.item);
  80. match item_heals {
  81. None => {}
  82. Some(healer) => {
  83. stats.hp = i32::min(stats.max_hp, stats.hp + healer.heal_amount);
  84. if entity == *player_entity {
  85. gamelog.entries.push(format!("You drink the {}, healing {} hp.", names.get(useitem.item).unwrap().name, healer.heal_amount));
  86. }
  87. }
  88. }
  89. let consumable = consumables.get(useitem.item);
  90. match consumable {
  91. None => {}
  92. Some(_) => {
  93. entities.delete(useitem.item).expect("Delete failed");
  94. }
  95. }
  96. }
  97. wants_use.clear();
  98. }
  99. }
  100. pub struct ItemDropSystem {}
  101. impl<'a> System<'a> for ItemDropSystem {
  102. #[allow(clippy::type_complexity)]
  103. type SystemData = ( ReadExpect<'a, Entity>,
  104. WriteExpect<'a, GameLog>,
  105. Entities<'a>,
  106. WriteStorage<'a, WantsToDropItem>,
  107. ReadStorage<'a, Name>,
  108. WriteStorage<'a, Position>,
  109. WriteStorage<'a, InBackpack>
  110. );
  111. fn run(&mut self, data : Self::SystemData) {
  112. let (player_entity, mut gamelog, entities, mut wants_drop, names, mut positions, mut backpack) = data;
  113. for (entity, to_drop) in (&entities, &wants_drop).join() {
  114. let mut dropper_pos : Position = Position{x:0, y:0};
  115. {
  116. let dropped_pos = positions.get(entity).unwrap();
  117. dropper_pos.x = dropped_pos.x;
  118. dropper_pos.y = dropped_pos.y;
  119. }
  120. positions.insert(to_drop.item, Position{ x : dropper_pos.x, y : dropper_pos.y }).expect("Unable to insert position");
  121. backpack.remove(to_drop.item);
  122. if entity == *player_entity {
  123. gamelog.entries.push(format!("You drop the {}.", names.get(to_drop.item).unwrap().name));
  124. }
  125. }
  126. wants_drop.clear();
  127. }
  128. }