diff options
| author | troido <troido@protonmail.com> | 2020-02-23 21:25:36 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-23 21:25:36 +0100 |
| commit | 613952f918b8d72a3e397dc46be309b2320c6ad0 (patch) | |
| tree | 2f34c1d8fb2aed771a5714a266845df095e5b438 /src | |
| parent | 9a814769565ab36c227508c47792e112de338df1 (diff) | |
entities can be attacked
Diffstat (limited to 'src')
| -rw-r--r-- | src/attack.rs | 17 | ||||
| -rw-r--r-- | src/components/mod.rs | 8 | ||||
| -rw-r--r-- | src/main.rs | 1 | ||||
| -rw-r--r-- | src/room.rs | 4 | ||||
| -rw-r--r-- | src/systems/attacking.rs | 30 | ||||
| -rw-r--r-- | src/systems/mod.rs | 4 | ||||
| -rw-r--r-- | src/systems/useitem.rs | 37 |
7 files changed, 83 insertions, 18 deletions
diff --git a/src/attack.rs b/src/attack.rs new file mode 100644 index 0000000..a0b8b99 --- /dev/null +++ b/src/attack.rs @@ -0,0 +1,17 @@ + +use specs::Entity; + +#[derive(Debug, Clone, Default)] +pub struct Attack { + pub damage: i64, + pub attacker: Option<Entity> +} + +impl Attack { + pub fn new(damage: i64) -> Self { + Self { + damage, + attacker: None + } + } +} diff --git a/src/components/mod.rs b/src/components/mod.rs index 14eb248..8941abb 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -18,7 +18,8 @@ use crate::{ Sprite, controls::Control, Template, - playerstate::RoomPos + playerstate::RoomPos, + attack::Attack }; #[derive(Debug, Clone)] @@ -109,3 +110,8 @@ pub struct RoomExit { pub destination: RoomId, pub dest_pos: RoomPos } + +#[derive(Component, Debug, Clone, Default)] +pub struct Attacked { + pub attacks: Vec<Attack> +} diff --git a/src/main.rs b/src/main.rs index b17160a..2e6ca10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ mod persistence; mod worldloader; mod world; mod sprite; +mod attack; pub use self::{ pos::Pos, diff --git a/src/room.rs b/src/room.rs index ff7b75f..a4046b2 100644 --- a/src/room.rs +++ b/src/room.rs @@ -32,7 +32,8 @@ use crate::{ Create, Take, Migrate, - Use + Use, + Attacking }, components::{ Position, @@ -80,6 +81,7 @@ impl <'a, 'b>Room<'a, 'b> { .with(Take, "take", &["controlinput"]) .with(Use, "use", &["controlinput"]) .with(Move, "move", &["registernew", "controlinput"]) + .with(Attacking, "attacking", &["use"]) .with(View::default(), "view", &["move"]) .with(Migrate, "migrate", &["view"]) .with(Create, "create", &["view", "controlinput"]) diff --git a/src/systems/attacking.rs b/src/systems/attacking.rs new file mode 100644 index 0000000..0fb5cf7 --- /dev/null +++ b/src/systems/attacking.rs @@ -0,0 +1,30 @@ + +use specs::{ + WriteStorage, + System, + Join +}; + +use crate::{ + components::{Health, Attacked}, + util +}; + + +pub struct Attacking; +impl <'a> System<'a> for Attacking { + type SystemData = ( + WriteStorage<'a, Attacked>, + WriteStorage<'a, Health> + ); + fn run(&mut self, (mut victims, mut healths): Self::SystemData) { + + for (health, attacked) in (&mut healths, &mut victims).join() { + for attack in attacked.attacks.drain(..) { + health.health -= attack.damage; + } + health.health = util::clamp(health.health, 0, health.maxhealth); + } + } +} + diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 88e67ad..f93088d 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -8,6 +8,7 @@ mod create; mod take; mod migrate; mod useitem; +mod attacking; pub use self::{ controlinput::ControlInput, @@ -18,5 +19,6 @@ pub use self::{ create::Create, take::Take, migrate::Migrate, - useitem::Use + useitem::Use, + attacking::Attacking }; diff --git a/src/systems/useitem.rs b/src/systems/useitem.rs index d77239c..4317162 100644 --- a/src/systems/useitem.rs +++ b/src/systems/useitem.rs @@ -1,6 +1,7 @@ use specs::{ + Entities, ReadStorage, WriteStorage, System, @@ -8,30 +9,33 @@ use specs::{ Write }; -use crate::components::{ - Controller, - Position, - Inventory, - Health +use crate::{ + components::{ + Controller, + Position, + Inventory, + Attacked + }, + resources::{NewEntities}, + components::item::ItemAction::{None, Build, Eat}, + controls::Control, + attack::Attack }; -use crate::resources::{NewEntities}; -use crate::components::item::ItemAction::{None, Build, Eat}; -use crate::controls::Control; - pub struct Use; impl <'a> System<'a> for Use { type SystemData = ( + Entities<'a>, ReadStorage<'a, Controller>, WriteStorage<'a, Position>, WriteStorage<'a, Inventory>, Write<'a, NewEntities>, - WriteStorage<'a, Health> + WriteStorage<'a, Attacked> ); - fn run(&mut self, (controllers, positions, mut inventories, mut new, mut healths): Self::SystemData) { - for (controller, position, inventory, maybe_health) in (&controllers, &positions, &mut inventories, (&mut healths).maybe()).join(){ + 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 { Control::Use(rank) => { if let Some(item) = inventory.items.get(*rank) { @@ -41,9 +45,12 @@ impl <'a> System<'a> for Use { inventory.items.remove(*rank); } Eat(health_diff) => { - if let Some(health) = maybe_health { - health.heal(*health_diff); - } + attacked + .entry(ent) + .unwrap() + .or_insert_with(Attacked::default) + .attacks + .push(Attack::new(-*health_diff)); inventory.items.remove(*rank); } None => {} |
