summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-03-05 14:47:48 +0100
committertroido <troido@protonmail.com>2020-03-05 14:47:48 +0100
commita520382eb19e4234ed52fd1eb9fb965b5967d522 (patch)
tree5b4a8831cd544f57c6119eac5785973dc9e9bd50 /src
parent7846b871f5c9d57a19dbf09b7acbf0d6b38a69ca (diff)
equipment is now a part of the inventory
Diffstat (limited to 'src')
-rw-r--r--src/components/equipment.rs29
-rw-r--r--src/components/inventory.rs44
-rw-r--r--src/components/item.rs1
-rw-r--r--src/components/mod.rs11
-rw-r--r--src/playerstate.rs4
-rw-r--r--src/room.rs12
-rw-r--r--src/systems/attacking.rs7
-rw-r--r--src/systems/fight.rs11
-rw-r--r--src/systems/take.rs4
-rw-r--r--src/systems/useitem.rs21
-rw-r--r--src/systems/view.rs2
11 files changed, 72 insertions, 74 deletions
diff --git a/src/components/equipment.rs b/src/components/equipment.rs
index 1d86e95..94a440b 100644
--- a/src/components/equipment.rs
+++ b/src/components/equipment.rs
@@ -86,36 +86,9 @@ impl Equippable {
#[derive(Component, Debug, Clone)]
#[storage(HashMapStorage)]
pub struct Equipment {
- pub equipment: HashMap<Slot, Option<Equippable>>
+ pub slots: Vec<Slot>
}
-impl Equipment {
- pub fn get_bonus(&self, stat: Stat) -> i64 {
- let mut bonus = 0;
- for v in self.equipment.values() {
- if let Some(equippable) = v {
- if let Some(s) = equippable.stats.get(&stat) {
- bonus += s;
- }
- }
- }
- bonus
- }
- pub fn all_bonuses(&self) -> HashMap<Stat, i64> {
- let mut bonuses: HashMap<Stat, i64> = HashMap::new();
- for v in self.equipment.values() {
- if let Some(equippable) = v {
- for (stat, s) in equippable.stats.iter(){
- let current: i64 = *bonuses.entry(*stat).or_insert(0);
- bonuses.insert(*stat, current + s);
- }
- }
- }
- bonuses
- }
-}
-
-
#[cfg(test)]
mod tests {
use super::*;
diff --git a/src/components/inventory.rs b/src/components/inventory.rs
new file mode 100644
index 0000000..81064d8
--- /dev/null
+++ b/src/components/inventory.rs
@@ -0,0 +1,44 @@
+
+use std::collections::HashMap;
+use specs::{Component, FlaggedStorage, HashMapStorage};
+use super::{
+ item::{Item, ItemAction},
+ equipment::{Stat, Equippable},
+};
+
+#[derive(Debug, Clone, Default)]
+pub struct Inventory {
+ pub items: Vec<(Item, bool)>,
+ pub capacity: usize
+}
+impl Component for Inventory {
+ type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
+}
+
+impl Inventory {
+
+ fn equipped(&self) -> Vec<Equippable> {
+ let mut equippables = Vec::new();
+ for (item, is_equipped) in self.items.iter() {
+ if *is_equipped {
+ if let ItemAction::Equip(equippable) = &item.action {
+ equippables.push(equippable.clone());
+ } else {
+ panic!("unequippable item equipped!");
+ }
+ }
+ }
+ equippables
+ }
+
+ pub fn equipment_bonuses(&self) -> HashMap<Stat, i64> {
+ let mut bonuses: HashMap<Stat, i64> = HashMap::new();
+ for equippable in self.equipped() {
+ for (stat, s) in equippable.stats.iter(){
+ let current: i64 = *bonuses.entry(*stat).or_insert(0);
+ bonuses.insert(*stat, current + s);
+ }
+ }
+ bonuses
+ }
+}
diff --git a/src/components/item.rs b/src/components/item.rs
index 9e61567..3b931bb 100644
--- a/src/components/item.rs
+++ b/src/components/item.rs
@@ -48,7 +48,6 @@ impl ItemAction {
}
}
-
#[cfg(test)]
mod tests {
use super::*;
diff --git a/src/components/mod.rs b/src/components/mod.rs
index 5dd83bd..c3e8d5d 100644
--- a/src/components/mod.rs
+++ b/src/components/mod.rs
@@ -4,6 +4,7 @@ pub mod messages;
pub mod faction;
pub mod interactable;
pub mod equipment;
+pub mod inventory;
pub use item::Item;
pub use messages::{
@@ -14,6 +15,7 @@ pub use messages::{
pub use faction::Faction;
pub use interactable::Interactable;
pub use equipment::Equipment;
+pub use inventory::Inventory;
use specs::{
DenseVecStorage,
@@ -102,15 +104,6 @@ impl Player {
}
}
-#[derive(Debug, Clone, Default)]
-pub struct Inventory {
- pub items: Vec<Item>,
- pub capacity: usize
-}
-impl Component for Inventory {
- type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
-}
-
#[derive(Component, Debug, Clone)]
pub struct Health {
pub health: i64,
diff --git a/src/playerstate.rs b/src/playerstate.rs
index acf718a..0e4fd3d 100644
--- a/src/playerstate.rs
+++ b/src/playerstate.rs
@@ -131,7 +131,7 @@ impl PlayerState {
let item_ent = encyclopedia.construct(template).unwrap();
for component in item_ent {
if let ComponentWrapper::Item(item) = component {
- return item;
+ return (item, false);
}
}
panic!("Item in inventory does not have item component")
@@ -144,7 +144,7 @@ impl PlayerState {
ComponentWrapper::Movable(Movable{cooldown: 2}),
ComponentWrapper::Autofight(Autofight::default()),
ComponentWrapper::Faction(Faction::Good),
- ComponentWrapper::Equipment(Equipment{equipment: hashmap!(Slot::Hand => None, Slot::Body => None)})
+ ComponentWrapper::Equipment(Equipment{slots: vec!(Slot::Hand, Slot::Body)})
]
}
}
diff --git a/src/room.rs b/src/room.rs
index 44882cf..0d83012 100644
--- a/src/room.rs
+++ b/src/room.rs
@@ -31,8 +31,7 @@ use crate::{
Inventory,
Health,
New,
- Removed,
- Equipment
+ Removed
},
Encyclopedia,
roomtemplate::RoomTemplate,
@@ -223,13 +222,12 @@ impl <'a, 'b>Room<'a, 'b> {
let players = self.world.read_component::<Player>();
let inventories = self.world.read_component::<Inventory>();
let healths = self.world.read_component::<Health>();
- let equipments = self.world.read_component::<Equipment>();
let mut saved = HashMap::new();
- for (player, inventory, health, equipment) in (&players, &inventories, &healths, &equipments).join() {
+ for (player, inventory, health) in (&players, &inventories, &healths).join() {
saved.insert(player.id.clone(), PlayerState::create(
player.id.clone(),
self.id.clone(),
- inventory.items.iter().map(|item| item.ent.clone()).collect(),
+ inventory.items.iter().map(|(item, _)| item.ent.clone()).collect(),
inventory.capacity,
health.health,
health.maxhealth,
@@ -246,12 +244,10 @@ impl <'a, 'b>Room<'a, 'b> {
let inventory = inventories.get(ent)?;
let healths = self.world.read_component::<Health>();
let health = healths.get(ent)?;
- let equipments = self.world.read_component::<Equipment>();
- let equipment = equipments.get(ent)?;
Some(PlayerState::create(
player.id.clone(),
self.id.clone(),
- inventory.items.iter().map(|item| item.ent.clone()).collect(),
+ inventory.items.iter().map(|(item, _)| item.ent.clone()).collect(),
inventory.capacity,
health.health,
health.maxhealth,
diff --git a/src/systems/attacking.rs b/src/systems/attacking.rs
index 597f781..4318695 100644
--- a/src/systems/attacking.rs
+++ b/src/systems/attacking.rs
@@ -11,7 +11,7 @@ use specs::{
};
use crate::{
- components::{Health, AttackInbox, AttackType, Dead, Position, Autofight, Equipment, equipment::Stat},
+ components::{Health, AttackInbox, AttackType, Dead, Position, Autofight},
resources::NewEntities,
Template,
util
@@ -27,10 +27,9 @@ impl <'a> System<'a> for Attacking {
WriteStorage<'a, Dead>,
ReadStorage<'a, Position>,
Write<'a, NewEntities>,
- WriteStorage<'a, Autofight>,
- ReadStorage<'a, Equipment>
+ WriteStorage<'a, Autofight>
);
- fn run(&mut self, (entities, mut attackeds, mut healths, mut deads, positions, mut new, mut autofighters, equipments): Self::SystemData) {
+ fn run(&mut self, (entities, mut attackeds, mut healths, mut deads, positions, mut new, mut autofighters): Self::SystemData) {
for (entity, attacked, autofighter) in (&entities, &attackeds, &mut autofighters).join() {
for attack in &attacked.messages {
diff --git a/src/systems/fight.rs b/src/systems/fight.rs
index 7a02ba0..3e1142b 100644
--- a/src/systems/fight.rs
+++ b/src/systems/fight.rs
@@ -19,8 +19,7 @@ use crate::components::{
ControlCooldown,
Autofight,
Faction,
- Equipment,
- equipment::Stat
+ Inventory
};
use crate::controls::{Control};
@@ -41,10 +40,10 @@ impl <'a> System<'a> for Fight {
WriteStorage<'a, ControlCooldown>,
WriteStorage<'a, Autofight>,
ReadStorage<'a, Faction>,
- ReadStorage<'a, Equipment>
+ ReadStorage<'a, Inventory>
);
- fn run(&mut self, (entities, controllers, positions, ground, mut attacked, fighters, healths, mut cooldowns, mut autofighters, factions, equipments): Self::SystemData) {
+ fn run(&mut self, (entities, controllers, positions, ground, mut attacked, fighters, healths, mut cooldowns, mut autofighters, factions, inventories): Self::SystemData) {
for (entity, controller, position, fighter) in (&entities, &controllers, &positions, &fighters).join(){
let mut target = None;
match &controller.control {
@@ -70,8 +69,8 @@ impl <'a> System<'a> for Fight {
}
if let Some(ent) = target {
let mut attack = fighter.attack.clone();
- if let Some(equipment) = equipments.get(entity) {
- attack = attack.apply_bonuses(&equipment.all_bonuses());
+ if let Some(inventory) = inventories.get(entity) {
+ attack = attack.apply_bonuses(&inventory.equipment_bonuses());
}
AttackInbox::add_message(&mut attacked, ent, AttackMessage{typ: attack, attacker: Some(entity)});
cooldowns.insert(entity, ControlCooldown{amount: fighter.cooldown}).unwrap();
diff --git a/src/systems/take.rs b/src/systems/take.rs
index 1d139d8..0652fdb 100644
--- a/src/systems/take.rs
+++ b/src/systems/take.rs
@@ -50,7 +50,7 @@ impl <'a> System<'a> for Take {
}
for ent in ents {
if let Some(item) = items.get(ent) {
- inventory.items.insert(0, item.clone());
+ inventory.items.insert(0, (item.clone(), false));
if let Err(msg) = removed.insert(ent, Removed) {
println!("{:?}", msg);
}
@@ -62,7 +62,7 @@ impl <'a> System<'a> for Take {
if *rank >= inventory.items.len() {
return
}
- let item = inventory.items.remove(*rank);
+ let (item, _is_equipped) = inventory.items.remove(*rank);
let _ = new.create(position.pos, item.ent);
}
_ => {}
diff --git a/src/systems/useitem.rs b/src/systems/useitem.rs
index 7d37322..bf2d138 100644
--- a/src/systems/useitem.rs
+++ b/src/systems/useitem.rs
@@ -16,8 +16,7 @@ use crate::{
Inventory,
AttackInbox,
AttackMessage,
- AttackType,
- Equipment
+ AttackType
},
resources::{NewEntities},
components::item::ItemAction::{None, Build, Eat, Equip},
@@ -33,16 +32,15 @@ impl <'a> System<'a> for Use {
ReadStorage<'a, Position>,
WriteStorage<'a, Inventory>,
Write<'a, NewEntities>,
- WriteStorage<'a, AttackInbox>,
- WriteStorage<'a, Equipment>
+ WriteStorage<'a, AttackInbox>
);
- fn run(&mut self, (entities, controllers, positions, mut inventories, mut new, mut attacked, mut equipments): Self::SystemData) {
+ fn run(&mut self, (entities, controllers, positions, mut inventories, mut new, mut attacked): Self::SystemData) {
for (ent, controller, position, inventory) in (&entities, &controllers, &positions, &mut inventories).join(){
match &controller.control {
Control::Use(rank) => {
- if let Some(item) = inventory.items.get(*rank) {
- match &item.action {
+ if let Some(slot) = inventory.items.get_mut(*rank) {
+ match &slot.0.action {
Build(template) => {
new.create(position.pos, template.clone()).unwrap();
inventory.items.remove(*rank);
@@ -51,12 +49,9 @@ impl <'a> System<'a> for Use {
AttackInbox::add_message(&mut attacked, ent, AttackMessage{typ: AttackType::Heal(*health_diff), attacker: Option::None});
inventory.items.remove(*rank);
}
- Equip(equippable) => {
- if let Some(equipment) = equipments.get_mut(ent) {
- if equipment.equipment.contains_key(&equippable.slot) {
- equipment.equipment.insert(equippable.slot, Some(equippable.clone()));
- }
- }
+ Equip(_equippable) => {
+ // todo: unequip previous
+ slot.1 = true;
}
None => {}
}
diff --git a/src/systems/view.rs b/src/systems/view.rs
index cff24fa..0ed1cf7 100644
--- a/src/systems/view.rs
+++ b/src/systems/view.rs
@@ -73,7 +73,7 @@ impl <'a> System<'a> for View {
updates.change = Some(changes.clone());
}
if let Some(inventory) = inventories.get(ent){
- updates.inventory = Some(inventory.items.iter().map(|item| item.name.clone()).collect());
+ updates.inventory = Some(inventory.items.iter().map(|(item, _equipped)| item.name.clone()).collect());
}
if let Some(health) = healths.get(ent){
updates.health = Some((health.health, health.maxhealth));