diff options
| -rw-r--r-- | src/components/mod.rs | 18 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 5 | ||||
| -rw-r--r-- | src/playerstate.rs | 2 | ||||
| -rw-r--r-- | src/room.rs | 6 | ||||
| -rw-r--r-- | src/systems/controlinput.rs | 14 | ||||
| -rw-r--r-- | src/systems/fight.rs | 11 | ||||
| -rw-r--r-- | src/systems/mod.rs | 4 | ||||
| -rw-r--r-- | src/systems/moving.rs | 15 | ||||
| -rw-r--r-- | src/systems/take.rs | 2 | ||||
| -rw-r--r-- | src/systems/updatecooldowns.rs | 33 | ||||
| -rw-r--r-- | src/systems/useitem.rs | 2 |
11 files changed, 86 insertions, 26 deletions
diff --git a/src/components/mod.rs b/src/components/mod.rs index e65bcc6..3b0167d 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -50,7 +50,14 @@ impl Component for Visible { } #[derive(Component, Debug)] -pub struct Controller(pub Control); +pub struct Controller { + pub control: Control +} + +#[derive(Default, Component, Debug, Clone)] +pub struct Movable { + pub cooldown: i64 +} #[derive(Default, Component, Debug, Clone)] #[storage(NullStorage)] @@ -151,7 +158,8 @@ pub struct Trap { #[derive(Component, Debug, Clone)] #[storage(HashMapStorage)] pub struct Fighter { - pub attack: Attack + pub attack: Attack, + pub cooldown: i64 } #[derive(Component, Debug, Clone)] @@ -170,4 +178,10 @@ pub struct Volatile { pub end_time: Option<i64> } +#[derive(Component, Debug, Clone)] +#[storage(HashMapStorage)] +pub struct ControlCooldown { + pub amount: i64 +} + diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 4812b0b..2fe114a 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -8,7 +8,7 @@ use crate::{ Sprite, playerstate::RoomPos, attack::Attack, - components::{Visible, Blocking, Player, Floor, Item, Inventory, Health, Serialise, RoomExit, Trap, Fighter, Healing, Volatile}, + components::{Visible, Movable, Blocking, Player, Floor, Item, Inventory, Health, Serialise, RoomExit, Trap, Fighter, Healing, Volatile}, parameter::{Parameter, ParameterType} }; @@ -97,6 +97,7 @@ components!( name } }; + Movable (cooldown: Int) {Movable {cooldown}}; Blocking () {Blocking}; Floor () {Floor}; Player (name: String) {Player::new(PlayerId{name})}; @@ -115,7 +116,7 @@ components!( } }; Trap (damage: Int) {Trap{attack: Attack::new(damage)}}; - Fighter (damage: Int) {Fighter{attack: Attack::new(damage)}}; + Fighter (damage: Int, cooldown: Int) {Fighter{attack: Attack::new(damage), cooldown}}; Healing (delay: Int, health: Int) {Healing{delay, health, next_heal: None}}; Volatile (delay: Int) {Volatile{delay, end_time: None}}; ); diff --git a/src/playerstate.rs b/src/playerstate.rs index 4117bad..e27af1d 100644 --- a/src/playerstate.rs +++ b/src/playerstate.rs @@ -116,7 +116,7 @@ impl PlayerState { capacity: self.inventory_capacity }), ComponentWrapper::Health(Health{health: self.health, maxhealth: self.maximum_health}), - ComponentWrapper::Fighter(Fighter{attack: Attack::new(5)}), + ComponentWrapper::Fighter(Fighter{attack: Attack::new(5), cooldown: 8}), ComponentWrapper::Healing(Healing{delay: 50, health: 1, next_heal: None}) ] } diff --git a/src/room.rs b/src/room.rs index efec6cc..f3861dc 100644 --- a/src/room.rs +++ b/src/room.rs @@ -38,7 +38,8 @@ use crate::{ Trapping, Fight, Heal, - Volate + Volate, + UpdateCooldowns }, components::{ Position, @@ -83,7 +84,8 @@ impl <'a, 'b>Room<'a, 'b> { let mut dispatcher = DispatcherBuilder::new() .with(Volate, "volate", &[]) .with(RegisterNew::default(), "registernew", &[]) - .with(ControlInput, "controlinput", &["registernew"]) + .with(UpdateCooldowns, "cool_down", &["registernew"]) + .with(ControlInput, "controlinput", &["cool_down"]) .with(Take, "take", &["controlinput"]) .with(Use, "use", &["controlinput"]) .with(Move, "move", &["registernew", "controlinput"]) diff --git a/src/systems/controlinput.rs b/src/systems/controlinput.rs index 71373f5..17bc40f 100644 --- a/src/systems/controlinput.rs +++ b/src/systems/controlinput.rs @@ -8,7 +8,7 @@ use specs::{ Join }; -use crate::components::{Controller, Player}; +use crate::components::{Controller, Player, ControlCooldown}; use crate::resources::{Input}; @@ -18,17 +18,17 @@ impl <'a> System<'a> for ControlInput { Entities<'a>, Write<'a, Input>, WriteStorage<'a, Controller>, - ReadStorage<'a, Player> + ReadStorage<'a, Player>, + ReadStorage<'a, ControlCooldown> ); - fn run(&mut self, (entities, mut input, mut controllers, players): Self::SystemData) { + fn run(&mut self, (entities, mut input, mut controllers, players, cooldowns): Self::SystemData) { controllers.clear(); - for (player, entity) in (&players, &entities).join() { - if let Some(control) = input.actions.get(&player.id){ - let _ = controllers.insert(entity, Controller(control.clone())); + for (player, entity, ()) in (&players, &entities, !&cooldowns).join() { + if let Some(control) = input.actions.remove(&player.id){ + let _ = controllers.insert(entity, Controller{control: control}); } } - input.actions.clear(); } } diff --git a/src/systems/fight.rs b/src/systems/fight.rs index d0342b5..547f5fa 100644 --- a/src/systems/fight.rs +++ b/src/systems/fight.rs @@ -14,7 +14,8 @@ use crate::components::{ Position, Attacked, Fighter, - Health + Health, + ControlCooldown }; use crate::controls::{Control}; @@ -31,17 +32,19 @@ impl <'a> System<'a> for Fight { Read<'a, Ground>, WriteStorage<'a, Attacked>, ReadStorage<'a, Fighter>, - ReadStorage<'a, Health> + ReadStorage<'a, Health>, + WriteStorage<'a, ControlCooldown> ); - fn run(&mut self, (entities, controllers, positions, ground, mut attacked, fighters, healths): Self::SystemData) { + fn run(&mut self, (entities, controllers, positions, ground, mut attacked, fighters, healths, mut cooldowns): Self::SystemData) { for (entity, controller, position, fighter) in (&entities, &controllers, &positions, &fighters).join(){ - match &controller.0 { + match &controller.control { Control::Attack(directions) => { for direction in directions { for ent in ground.cells.get(&(position.pos + direction.to_position())).unwrap_or(&HashSet::new()) { if healths.contains(*ent) && *ent != entity { Attacked::add_attack(&mut attacked, *ent, fighter.attack.clone()); + cooldowns.insert(entity, ControlCooldown{amount: fighter.cooldown}).unwrap(); break; } } diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 28cd128..24ed3df 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -13,6 +13,7 @@ mod trapping; mod fight; mod heal; mod volate; +mod updatecooldowns; pub use self::{ controlinput::ControlInput, @@ -28,5 +29,6 @@ pub use self::{ trapping::Trapping, fight::Fight, heal::Heal, - volate::Volate + volate::Volate, + updatecooldowns::UpdateCooldowns }; diff --git a/src/systems/moving.rs b/src/systems/moving.rs index daa1eb4..5232076 100644 --- a/src/systems/moving.rs +++ b/src/systems/moving.rs @@ -19,7 +19,9 @@ use crate::{ Position, Floor, Moved, - Entered + Entered, + Movable, + ControlCooldown }, controls::{ Control @@ -42,14 +44,16 @@ impl <'a> System<'a> for Move { Write<'a, Ground>, ReadStorage<'a, Floor>, WriteStorage<'a, Moved>, - WriteStorage<'a, Entered> + WriteStorage<'a, Entered>, + ReadStorage<'a, Movable>, + WriteStorage<'a, ControlCooldown> ); - fn run(&mut self, (entities, controllers, mut positions, size, blocking, mut ground, floor, mut moved, mut entered): Self::SystemData) { + fn run(&mut self, (entities, controllers, mut positions, size, blocking, mut ground, floor, mut moved, mut entered, movables, mut cooldowns): Self::SystemData) { moved.clear(); entered.clear(); - for (ent, controller, mut pos) in (&entities, &controllers, &mut positions.restrict_mut()).join(){ - match &controller.0 { + for (ent, controller, mut pos, movable) in (&entities, &controllers, &mut positions.restrict_mut(), &movables).join(){ + match &controller.control { Control::Move(direction) => { let newpos = (pos.get_unchecked().pos + direction.to_position()).clamp(Pos::new(0, 0), Pos::new(size.width - 1, size.height - 1)); let mut blocked = false; @@ -72,6 +76,7 @@ impl <'a> System<'a> for Move { for ent in ground.cells.get(&newpos).unwrap() { let _ = entered.insert(*ent, Entered); } + cooldowns.insert(ent, ControlCooldown{amount: movable.cooldown}).unwrap(); } } _ => {} diff --git a/src/systems/take.rs b/src/systems/take.rs index 99c66d5..1d139d8 100644 --- a/src/systems/take.rs +++ b/src/systems/take.rs @@ -39,7 +39,7 @@ impl <'a> System<'a> for Take { fn run(&mut self, (entities, controllers, positions, ground, mut removed, items, mut inventories, mut new, visibles): Self::SystemData) { for (ent, controller, position, inventory) in (&entities, &controllers, &positions, &mut inventories).join(){ - match &controller.0 { + match &controller.control { Control::Take(rank) if inventory.items.len() < inventory.capacity => { let mut ents = ground.by_height(&position.pos, &visibles, &ent); if let Some(idx) = rank { diff --git a/src/systems/updatecooldowns.rs b/src/systems/updatecooldowns.rs new file mode 100644 index 0000000..73ca770 --- /dev/null +++ b/src/systems/updatecooldowns.rs @@ -0,0 +1,33 @@ + +use specs::{ + WriteStorage, + Entities, + System, + Join +}; + +use crate::components::ControlCooldown; + + +pub struct UpdateCooldowns; +impl <'a> System<'a> for UpdateCooldowns { + type SystemData = ( + Entities<'a>, + WriteStorage<'a, ControlCooldown> + ); + fn run(&mut self, (entities, mut cooldowns): Self::SystemData) { + let mut to_remove = Vec::new(); + for (entity, cooldown) in (&entities, &mut cooldowns).join() { + if cooldown.amount > 0 { + cooldown.amount -= 1; + } + if cooldown.amount <= 0 { + to_remove.push(entity); + } + } + for entity in to_remove { + cooldowns.remove(entity); + } + } +} + diff --git a/src/systems/useitem.rs b/src/systems/useitem.rs index 4414b39..183bd79 100644 --- a/src/systems/useitem.rs +++ b/src/systems/useitem.rs @@ -36,7 +36,7 @@ impl <'a> System<'a> for Use { 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.0 { + match &controller.control { Control::Use(rank) => { if let Some(item) = inventory.items.get(*rank) { match &item.action { |
