diff options
| -rw-r--r-- | content/encyclopediae/default_encyclopedia.json | 9 | ||||
| -rw-r--r-- | content/maps/room.json | 9 | ||||
| -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 |
9 files changed, 111 insertions, 10 deletions
diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index 0a4bb97..c1b8c1e 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -178,5 +178,14 @@ "sprite": ["concat", [["string", "emptyletter-"], ["arg", "char"]]], "height": ["float", 1.0] }]] + }, + "radishplant": { + "sprite": "smallplant", + "name": "radishplant", + "height": 0.5, + "components": [ + ["Interactable", {"action": ["string", "harvest"]}], + "Mortal" + ] } } diff --git a/content/maps/room.json b/content/maps/room.json index fe99559..16e6230 100644 --- a/content/maps/room.json +++ b/content/maps/room.json @@ -17,13 +17,13 @@ "X,^,,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X", "X,,,,,.............bbb...........++++#,,,X", "X,**,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X", - "X,*,*,.,,,,,,,,,,,,~~~,,,T,,,T,,#++++#,,,X", + "X,*,*,.,,,,,V,,V,,,~~~,,,T,,,T,,#++++#,,,X", "X,,*,,.,,,,,,,,,,,,~~~,,,,,,,,,,######,,,X", "X,oo,,.,,,d,,,,,,,~~~~,,,,,,,,,,f,,,,f,,,X", "X,,*,,.,,,,,,,,,,,~~~''''''''''''''''f'''X", - "X*,,,,.,,,d,,,,,,,~~~'''''''''''f''''f'''X", - "X,,,,,.,,,,,,,,,,,~~~'''''''''''ffffff'''X", - "X,,,,,.,,,,,,,,,,,~~~''''''''''''''''''''X", + "X*,,,,.,,,d,VVV,,,~~~'''''''''''f''''f'''X", + "X,,,,,.,,,,,VVV,,,~~~'''''''''''ffffff'''X", + "X,,,,,.,,,,,VVV,,,~~~''''''''''''''''''''X", "XXXXX,.,XXXXXXXXXX~~~XXXXXXXXXXXXXXXXXXXXX", " %%% " ], @@ -44,6 +44,7 @@ "^": ["grass", "spiketrap"], "d": ["grass", {"type": "spawner", "kwargs": {"template": {"type": "dummy"}, "delay": 100}}], "r": ["grass", {"type": "spawner", "kwargs": {"template": {"type": "rat"}, "amount": 3, "clan": "rats", "delay": 200}}], + "V": ["grass", "radishplant"], " ": [] } } 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 }; |
