From d6c934156946360b154cb75571e9e9274831a96e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 20 Jan 2022 09:23:13 -0500 Subject: [PATCH] Split the rest of the inventory system into its own modules --- src/inventory_system.rs | 202 ++---------------- src/inventory_system/collection_system.rs | 65 ++++++ src/inventory_system/drop_system.rs | 77 +++++++ src/inventory_system/identification_system.rs | 38 ++++ src/inventory_system/remove_system.rs | 28 +++ .../{item_use_system.rs => use_system.rs} | 6 +- 6 files changed, 225 insertions(+), 191 deletions(-) create mode 100644 src/inventory_system/collection_system.rs create mode 100644 src/inventory_system/drop_system.rs create mode 100644 src/inventory_system/identification_system.rs create mode 100644 src/inventory_system/remove_system.rs rename src/inventory_system/{item_use_system.rs => use_system.rs} (97%) diff --git a/src/inventory_system.rs b/src/inventory_system.rs index 7d7abbb..ce8659c 100644 --- a/src/inventory_system.rs +++ b/src/inventory_system.rs @@ -1,11 +1,18 @@ -mod item_use_system; +mod collection_system; +mod drop_system; +mod identification_system; +mod remove_system; +mod use_system; use ::specs::prelude::*; -pub use item_use_system::ItemUseSystem; +pub use collection_system::ItemCollectionSystem; +pub use drop_system::ItemDropSystem; +pub use identification_system::ItemIdentificationSystem; +pub use remove_system::ItemRemoveSystem; +pub use use_system::ItemUseSystem; -use crate::components::*; -use crate::game_log::GameLog; -use crate::{raws, MasterDungeonMap}; +use crate::components::{MagicItem, Name, ObfuscatedName}; +use crate::MasterDungeonMap; fn obfuscate_name( item: Entity, @@ -30,188 +37,3 @@ fn obfuscate_name( "Nameless item (bug)".to_string() } } - -pub struct ItemCollectionSystem {} - -impl<'a> System<'a> for ItemCollectionSystem { - #[allow(clippy::type_complexity)] - type SystemData = ( - ReadExpect<'a, Entity>, - WriteExpect<'a, GameLog>, - WriteStorage<'a, WantsToPickupItem>, - WriteStorage<'a, Position>, - ReadStorage<'a, Name>, - WriteStorage<'a, InBackpack>, - WriteStorage<'a, EquipmentChanged>, - ReadStorage<'a, MagicItem>, - ReadStorage<'a, ObfuscatedName>, - ReadExpect<'a, MasterDungeonMap>, - ); - - fn run(&mut self, data: Self::SystemData) { - let ( - player_entity, - mut gamelog, - mut wants_pickup, - mut positions, - names, - mut backpack, - mut dirty, - magic_items, - obfuscated_names, - dm, - ) = data; - - for pickup in wants_pickup.join() { - positions.remove(pickup.item); - backpack - .insert( - pickup.item, - InBackpack { - owner: pickup.collected_by, - }, - ) - .expect("Failed to add item to backpack"); - dirty - .insert(pickup.collected_by, EquipmentChanged {}) - .expect("Unable to insert equipment change"); - - if pickup.collected_by == *player_entity { - gamelog.append(format!( - "You pick up the {}.", - obfuscate_name(pickup.item, &names, &magic_items, &obfuscated_names, &dm) - )); - } - } - - wants_pickup.clear(); - } -} - -pub struct ItemDropSystem {} - -impl<'a> System<'a> for ItemDropSystem { - #[allow(clippy::type_complexity)] - type SystemData = ( - ReadExpect<'a, Entity>, - WriteExpect<'a, GameLog>, - Entities<'a>, - WriteStorage<'a, WantsToDropItem>, - ReadStorage<'a, Name>, - WriteStorage<'a, Position>, - WriteStorage<'a, InBackpack>, - WriteStorage<'a, EquipmentChanged>, - ReadStorage<'a, MagicItem>, - ReadStorage<'a, ObfuscatedName>, - ReadExpect<'a, MasterDungeonMap>, - ); - - fn run(&mut self, data: Self::SystemData) { - let ( - player_entity, - mut gamelog, - entities, - mut wants_drop, - names, - mut positions, - mut backpack, - mut dirty, - magic_items, - obfuscated_names, - dm, - ) = data; - - for (entity, to_drop) in (&entities, &wants_drop).join() { - let mut dropper_pos: Position = Position { x: 0, y: 0 }; - { - let dropped_pos = positions.get(entity).unwrap(); - dropper_pos.x = dropped_pos.x; - dropper_pos.y = dropped_pos.y; - } - - positions - .insert( - to_drop.item, - Position { - x: dropper_pos.x, - y: dropper_pos.y, - }, - ) - .expect("Unable to drop item to position"); - - backpack.remove(to_drop.item); - - dirty - .insert(entity, EquipmentChanged {}) - .expect("Unable to insert equipment change"); - - if entity == *player_entity { - gamelog.append(format!( - "You drop the {}.", - obfuscate_name(to_drop.item, &names, &magic_items, &obfuscated_names, &dm) - )); - } - } - - wants_drop.clear(); - } -} - -pub struct ItemRemoveSystem {} - -impl<'a> System<'a> for ItemRemoveSystem { - #[allow(clippy::type_complexity)] - type SystemData = ( - Entities<'a>, - WriteStorage<'a, WantsToRemoveItem>, - WriteStorage<'a, Equipped>, - WriteStorage<'a, InBackpack>, - ); - - fn run(&mut self, data: Self::SystemData) { - let (entities, mut wants_remove, mut equipped, mut backpack) = data; - - for (entity, to_remove) in (&entities, &wants_remove).join() { - equipped.remove(to_remove.item); - backpack - .insert(to_remove.item, InBackpack { owner: entity }) - .expect("Unable to remove item and put in backpack"); - } - - wants_remove.clear(); - } -} - -pub struct ItemIdentificationSystem {} - -impl<'a> System<'a> for ItemIdentificationSystem { - #[allow(clippy::type_complexity)] - type SystemData = ( - ReadStorage<'a, Player>, - WriteStorage<'a, IdentifiedItem>, - WriteExpect<'a, MasterDungeonMap>, - ReadStorage<'a, Item>, - ReadStorage<'a, Name>, - WriteStorage<'a, ObfuscatedName>, - Entities<'a>, - ); - - fn run(&mut self, data: Self::SystemData) { - let (player, mut identified, mut dm, items, names, mut obfuscated_names, entities) = data; - - for (_p, id) in (&player, &identified).join() { - if !dm.identified_items.contains(&id.name) && raws::is_tag_magic(&id.name) { - dm.identified_items.insert(id.name.clone()); - - for (entity, _item, name) in (&entities, &items, &names).join() { - if name.name == id.name { - obfuscated_names.remove(entity); - } - } - } - } - - // Clean up - identified.clear(); - } -} diff --git a/src/inventory_system/collection_system.rs b/src/inventory_system/collection_system.rs new file mode 100644 index 0000000..bf17e54 --- /dev/null +++ b/src/inventory_system/collection_system.rs @@ -0,0 +1,65 @@ +use ::specs::prelude::*; + +use super::obfuscate_name; +use crate::components::{ + EquipmentChanged, InBackpack, MagicItem, Name, ObfuscatedName, Position, WantsToPickupItem, +}; +use crate::game_log::GameLog; +use crate::MasterDungeonMap; + +pub struct ItemCollectionSystem {} + +impl<'a> System<'a> for ItemCollectionSystem { + #[allow(clippy::type_complexity)] + type SystemData = ( + ReadExpect<'a, Entity>, + WriteExpect<'a, GameLog>, + WriteStorage<'a, WantsToPickupItem>, + WriteStorage<'a, Position>, + ReadStorage<'a, Name>, + WriteStorage<'a, InBackpack>, + WriteStorage<'a, EquipmentChanged>, + ReadStorage<'a, MagicItem>, + ReadStorage<'a, ObfuscatedName>, + ReadExpect<'a, MasterDungeonMap>, + ); + + fn run(&mut self, data: Self::SystemData) { + let ( + player_entity, + mut gamelog, + mut wants_pickup, + mut positions, + names, + mut backpack, + mut dirty, + magic_items, + obfuscated_names, + dm, + ) = data; + + for pickup in wants_pickup.join() { + positions.remove(pickup.item); + backpack + .insert( + pickup.item, + InBackpack { + owner: pickup.collected_by, + }, + ) + .expect("Failed to add item to backpack"); + dirty + .insert(pickup.collected_by, EquipmentChanged {}) + .expect("Unable to insert equipment change"); + + if pickup.collected_by == *player_entity { + gamelog.append(format!( + "You pick up the {}.", + obfuscate_name(pickup.item, &names, &magic_items, &obfuscated_names, &dm) + )); + } + } + + wants_pickup.clear(); + } +} diff --git a/src/inventory_system/drop_system.rs b/src/inventory_system/drop_system.rs new file mode 100644 index 0000000..1f4360f --- /dev/null +++ b/src/inventory_system/drop_system.rs @@ -0,0 +1,77 @@ +use ::specs::prelude::*; + +use super::obfuscate_name; +use crate::components::{ + EquipmentChanged, InBackpack, MagicItem, Name, ObfuscatedName, Position, WantsToDropItem, +}; +use crate::game_log::GameLog; +use crate::MasterDungeonMap; + +pub struct ItemDropSystem {} + +impl<'a> System<'a> for ItemDropSystem { + #[allow(clippy::type_complexity)] + type SystemData = ( + ReadExpect<'a, Entity>, + WriteExpect<'a, GameLog>, + Entities<'a>, + WriteStorage<'a, WantsToDropItem>, + ReadStorage<'a, Name>, + WriteStorage<'a, Position>, + WriteStorage<'a, InBackpack>, + WriteStorage<'a, EquipmentChanged>, + ReadStorage<'a, MagicItem>, + ReadStorage<'a, ObfuscatedName>, + ReadExpect<'a, MasterDungeonMap>, + ); + + fn run(&mut self, data: Self::SystemData) { + let ( + player_entity, + mut gamelog, + entities, + mut wants_drop, + names, + mut positions, + mut backpack, + mut dirty, + magic_items, + obfuscated_names, + dm, + ) = data; + + for (entity, to_drop) in (&entities, &wants_drop).join() { + let mut dropper_pos: Position = Position { x: 0, y: 0 }; + { + let dropped_pos = positions.get(entity).unwrap(); + dropper_pos.x = dropped_pos.x; + dropper_pos.y = dropped_pos.y; + } + + positions + .insert( + to_drop.item, + Position { + x: dropper_pos.x, + y: dropper_pos.y, + }, + ) + .expect("Unable to drop item to position"); + + backpack.remove(to_drop.item); + + dirty + .insert(entity, EquipmentChanged {}) + .expect("Unable to insert equipment change"); + + if entity == *player_entity { + gamelog.append(format!( + "You drop the {}.", + obfuscate_name(to_drop.item, &names, &magic_items, &obfuscated_names, &dm) + )); + } + } + + wants_drop.clear(); + } +} diff --git a/src/inventory_system/identification_system.rs b/src/inventory_system/identification_system.rs new file mode 100644 index 0000000..fa1d63a --- /dev/null +++ b/src/inventory_system/identification_system.rs @@ -0,0 +1,38 @@ +use ::specs::prelude::*; + +use crate::components::{IdentifiedItem, Item, Name, ObfuscatedName, Player}; +use crate::{raws, MasterDungeonMap}; + +pub struct ItemIdentificationSystem {} + +impl<'a> System<'a> for ItemIdentificationSystem { + #[allow(clippy::type_complexity)] + type SystemData = ( + ReadStorage<'a, Player>, + WriteStorage<'a, IdentifiedItem>, + WriteExpect<'a, MasterDungeonMap>, + ReadStorage<'a, Item>, + ReadStorage<'a, Name>, + WriteStorage<'a, ObfuscatedName>, + Entities<'a>, + ); + + fn run(&mut self, data: Self::SystemData) { + let (player, mut identified, mut dm, items, names, mut obfuscated_names, entities) = data; + + for (_p, id) in (&player, &identified).join() { + if !dm.identified_items.contains(&id.name) && raws::is_tag_magic(&id.name) { + dm.identified_items.insert(id.name.clone()); + + for (entity, _item, name) in (&entities, &items, &names).join() { + if name.name == id.name { + obfuscated_names.remove(entity); + } + } + } + } + + // Clean up + identified.clear(); + } +} diff --git a/src/inventory_system/remove_system.rs b/src/inventory_system/remove_system.rs new file mode 100644 index 0000000..a3ac1f6 --- /dev/null +++ b/src/inventory_system/remove_system.rs @@ -0,0 +1,28 @@ +use ::specs::prelude::*; + +use crate::components::{Equipped, InBackpack, WantsToRemoveItem}; + +pub struct ItemRemoveSystem {} + +impl<'a> System<'a> for ItemRemoveSystem { + #[allow(clippy::type_complexity)] + type SystemData = ( + Entities<'a>, + WriteStorage<'a, WantsToRemoveItem>, + WriteStorage<'a, Equipped>, + WriteStorage<'a, InBackpack>, + ); + + fn run(&mut self, data: Self::SystemData) { + let (entities, mut wants_remove, mut equipped, mut backpack) = data; + + for (entity, to_remove) in (&entities, &wants_remove).join() { + equipped.remove(to_remove.item); + backpack + .insert(to_remove.item, InBackpack { owner: entity }) + .expect("Unable to remove item and put in backpack"); + } + + wants_remove.clear(); + } +} diff --git a/src/inventory_system/item_use_system.rs b/src/inventory_system/use_system.rs similarity index 97% rename from src/inventory_system/item_use_system.rs rename to src/inventory_system/use_system.rs index 219d59d..3b218de 100644 --- a/src/inventory_system/item_use_system.rs +++ b/src/inventory_system/use_system.rs @@ -1,6 +1,10 @@ use ::specs::prelude::*; -use crate::components::*; +use crate::components::{ + AreaOfEffect, Confusion, Consumable, EquipmentChanged, Equippable, Equipped, HungerClock, + HungerState, IdentifiedItem, InBackpack, InflictsDamage, MagicMapper, Name, Pools, + Position, ProvidesFood, ProvidesHealing, SufferDamage, TownPortal, WantsToUseItem, +}; use crate::game_log::GameLog; use crate::particle_system::ParticleBuilder; use crate::{colors, spatial, Map, RunState};