diff options
| author | troido <troido@protonmail.com> | 2020-03-04 12:15:58 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-04 12:15:58 +0100 |
| commit | 21919636f95a1214b7ed1a3e4aa6527f45b9d073 (patch) | |
| tree | dfe7a465cdf81048c3ac04b5719571bb0192791f /src | |
| parent | daae8b511291b8cc854a8747e64e76c6ff2d462e (diff) | |
interacting now works
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/mod.rs | 15 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 1 | ||||
| -rw-r--r-- | src/controls.rs | 10 | ||||
| -rw-r--r-- | src/room.rs | 8 | ||||
| -rw-r--r-- | src/systems/fight.rs | 2 | ||||
| -rw-r--r-- | src/systems/interact.rs | 63 | ||||
| -rw-r--r-- | src/systems/mod.rs | 4 |
7 files changed, 97 insertions, 6 deletions
diff --git a/src/components/mod.rs b/src/components/mod.rs index d2087b0..66ac724 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -216,4 +216,19 @@ pub struct Clan { pub name: String, } +#[derive(Component, Debug, Clone, PartialEq, Eq)] +#[storage(HashMapStorage)] +pub enum Interactable { + Harvest +} + +impl Interactable { + pub fn from_str(txt: &str) -> Option<Interactable> { + match txt { + "harvest" => Some(Interactable::Harvest), + _ => None + } + } +} + diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 3acc8e9..638531b 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -135,6 +135,7 @@ components!( Clan (name: String) Clan{name}; Home (home: Pos) Home{home}; Faction (faction: String) {Faction::from_str(faction.as_str()).unwrap()}; + Interactable (action: String) {Interactable::from_str(action.as_str()).unwrap()}; ); diff --git a/src/controls.rs b/src/controls.rs index d3dec68..581b522 100644 --- a/src/controls.rs +++ b/src/controls.rs @@ -47,7 +47,8 @@ pub enum Control { Drop(usize), Use(usize), Attack(Vec<Direction>), - AttackTarget(Entity) + AttackTarget(Entity), + Interact(Vec<Direction>) } @@ -69,6 +70,13 @@ impl Control { } directions })), + "interact" => Some(Control::Interact({ + let mut directions = Vec::new(); + for dir in val.get(1)?.as_array()? { + directions.push(Direction::from_json(dir)?); + } + directions + })), _ => None } } else {None} diff --git a/src/room.rs b/src/room.rs index 4202251..02a3b75 100644 --- a/src/room.rs +++ b/src/room.rs @@ -62,7 +62,8 @@ use crate::{ UpdateCooldowns, ControlAI, Die, - Spawn + Spawn, + Interact } }; @@ -76,11 +77,12 @@ pub fn default_dispatcher<'a, 'b>() -> Dispatcher<'a, 'b> { .with(ControlAI, "controlai", &["cool_down"]) .with(Take, "take", &["controlinput", "controlai"]) .with(Use, "use", &["controlinput", "controlai"]) + .with(Interact, "interact", &["controlinput", "controlai"]) .with(Move, "move", &["controlinput", "controlai"]) .with(Trapping, "trapping", &["move"]) .with(Fight, "fight", &["move"]) .with(Heal, "heal", &["registernew"]) - .with(Attacking, "attacking", &["use", "trapping", "fight", "heal"]) + .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact"]) .with(Die, "die", &["attacking"]) .with(View::default(), "view", &["move", "attacking", "volate", "die"]) .with(Migrate, "migrate", &["view"]) @@ -115,7 +117,7 @@ impl <'a, 'b>Room<'a, 'b> { world.insert(NewEntities::new(encyclopedia)); register_insert!( world, - (Position, Visible, Controller, Movable, Blocking, Floor, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, Dead, Trap, Fighter, Healing, Volatile, ControlCooldown, Autofight, MonsterAI, Home, Mortal, AttackInbox, Item, Spawner, Clan, Faction), + (Position, Visible, Controller, Movable, Blocking, Floor, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, Dead, Trap, Fighter, Healing, Volatile, ControlCooldown, Autofight, MonsterAI, Home, Mortal, AttackInbox, Item, Spawner, Clan, Faction, Interactable), (Ground, Input, Output, Size, Spawn, Players, Emigration, TimeStamp) ); diff --git a/src/systems/fight.rs b/src/systems/fight.rs index 47b5811..cd6399f 100644 --- a/src/systems/fight.rs +++ b/src/systems/fight.rs @@ -31,7 +31,7 @@ impl <'a> System<'a> for Fight { type SystemData = ( Entities<'a>, ReadStorage<'a, Controller>, - WriteStorage<'a, Position>, + ReadStorage<'a, Position>, Read<'a, Ground>, WriteStorage<'a, AttackInbox>, ReadStorage<'a, Fighter>, diff --git a/src/systems/interact.rs b/src/systems/interact.rs new file mode 100644 index 0000000..4c0245d --- /dev/null +++ b/src/systems/interact.rs @@ -0,0 +1,63 @@ + +use std::collections::HashSet; +use specs::{ + Entities, + ReadStorage, + WriteStorage, + System, + Join, + Read +}; + +use crate::components::{ + Controller, + Position, + ControlCooldown, + Interactable, + Dead +}; + +use crate::controls::{Control}; +use crate::resources::{Ground}; + + + +pub struct Interact; +impl <'a> System<'a> for Interact { + type SystemData = ( + Entities<'a>, + ReadStorage<'a, Controller>, + ReadStorage<'a, Position>, + Read<'a, Ground>, + WriteStorage<'a, ControlCooldown>, + ReadStorage<'a, Interactable>, + WriteStorage<'a, Dead> + ); + + fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut deads): Self::SystemData) { + for (entity, controller, position) in (&entities, &controllers, &positions).join(){ + let mut target = None; + match &controller.control { + Control::Interact(directions) => { + 'targets: for direction in directions { + for ent in ground.cells.get(&(position.pos + direction.to_position())).unwrap_or(&HashSet::new()) { + if let Some(interactable) = interactables.get(*ent) { + target = Some((*ent, interactable)); + break 'targets; + } + } + } + } + _ => {} + } + if let Some((ent, interactable)) = target { + match interactable { + Interactable::Harvest => { + deads.insert(ent, Dead).unwrap(); + } + } + cooldowns.insert(entity, ControlCooldown{amount: 2}).unwrap(); + } + } + } +} diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 37afcd0..887a1f9 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -17,6 +17,7 @@ mod updatecooldowns; mod controlai; mod die; mod spawn; +mod interact; pub use self::{ controlinput::ControlInput, @@ -36,5 +37,6 @@ pub use self::{ updatecooldowns::UpdateCooldowns, controlai::ControlAI, die::Die, - spawn::Spawn + spawn::Spawn, + interact::Interact }; |
