diff options
| author | troido <troido@protonmail.com> | 2020-02-24 14:39:04 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-24 14:39:04 +0100 |
| commit | 481e03e04b055cebaf230b3a3cdce3446418c68c (patch) | |
| tree | 565a2d8afc2fba8d73e310cd1a32b6a94b84039f | |
| parent | f6a037faa2b675cd7318d6dd8ccee5133c89845d (diff) | |
added volatile wounds
| -rw-r--r-- | src/components/mod.rs | 9 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 3 | ||||
| -rw-r--r-- | src/defaultencyclopedia.rs | 5 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/room.rs | 6 | ||||
| -rw-r--r-- | src/systems/attacking.rs | 21 | ||||
| -rw-r--r-- | src/systems/mod.rs | 4 | ||||
| -rw-r--r-- | src/systems/volate.rs | 34 | ||||
| -rw-r--r-- | src/world.rs | 13 |
9 files changed, 85 insertions, 12 deletions
diff --git a/src/components/mod.rs b/src/components/mod.rs index 7ca0eba..e65bcc6 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -162,3 +162,12 @@ pub struct Healing { pub next_heal: Option<i64> } + +#[derive(Component, Debug, Clone)] +#[storage(HashMapStorage)] +pub struct Volatile { + pub delay: i64, + pub end_time: Option<i64> +} + + diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 7e48668..4812b0b 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}, + components::{Visible, Blocking, Player, Floor, Item, Inventory, Health, Serialise, RoomExit, Trap, Fighter, Healing, Volatile}, parameter::{Parameter, ParameterType} }; @@ -117,6 +117,7 @@ components!( Trap (damage: Int) {Trap{attack: Attack::new(damage)}}; Fighter (damage: Int) {Fighter{attack: Attack::new(damage)}}; Healing (delay: Int, health: Int) {Healing{delay, health, next_heal: None}}; + Volatile (delay: Int) {Volatile{delay, end_time: None}}; ); diff --git a/src/defaultencyclopedia.rs b/src/defaultencyclopedia.rs index a9bf5b9..c7e947e 100644 --- a/src/defaultencyclopedia.rs +++ b/src/defaultencyclopedia.rs @@ -137,6 +137,11 @@ pub fn default_encyclopedia() -> Encyclopedia { "sprite": "dummy", "height": 1, "components": [["Health", {"health": ["arg", "health"], "maxhealth": ["int", 20]}]] + }, + "wound": { + "sprite": "wound", + "height": 0.25, + "components": [["Volatile", {"delay": ["int", 4]}]] } })).unwrap() } diff --git a/src/main.rs b/src/main.rs index 6ee205f..1322801 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,7 +73,7 @@ fn main() -> Result<()>{ let mut gameserver = GameServer::new(servers); - let loader = WorldLoader::new(PathBuf::from_str(&(std::env::var("CARGO_MANIFEST_DIR").unwrap_or(".".to_string()).to_owned() + "/content/maps/"))?); + let loader = WorldLoader::new(PathBuf::from_str(&(std::env::var("CARGO_MANIFEST_DIR").unwrap_or(".".to_string()) + "/content/maps/"))?); let storage = FileStorage::new(FileStorage::savedir().expect("couldn't find any save directory")); diff --git a/src/room.rs b/src/room.rs index 9b0f33a..efec6cc 100644 --- a/src/room.rs +++ b/src/room.rs @@ -37,7 +37,8 @@ use crate::{ Attacking, Trapping, Fight, - Heal + Heal, + Volate }, components::{ Position, @@ -80,6 +81,7 @@ impl <'a, 'b>Room<'a, 'b> { world.register::<Serialise>(); let mut dispatcher = DispatcherBuilder::new() + .with(Volate, "volate", &[]) .with(RegisterNew::default(), "registernew", &[]) .with(ControlInput, "controlinput", &["registernew"]) .with(Take, "take", &["controlinput"]) @@ -89,7 +91,7 @@ impl <'a, 'b>Room<'a, 'b> { .with(Fight, "fight", &["move", "controlinput"]) .with(Heal, "heal", &["registernew"]) .with(Attacking, "attacking", &["use", "trapping", "fight", "heal"]) - .with(View::default(), "view", &["move", "attacking"]) + .with(View::default(), "view", &["move", "attacking", "volate"]) .with(Migrate, "migrate", &["view"]) .with(Create, "create", &["view", "controlinput"]) .with(Remove, "remove", &["view", "move"]) diff --git a/src/systems/attacking.rs b/src/systems/attacking.rs index 1f518cb..4b47f11 100644 --- a/src/systems/attacking.rs +++ b/src/systems/attacking.rs @@ -1,13 +1,17 @@ use specs::{ + ReadStorage, WriteStorage, + Write, System, Entities, Join }; use crate::{ - components::{Health, Attacked, Dying, Removed}, + components::{Health, Attacked, Dying, Removed, Position}, + resources::NewEntities, + Template, util }; @@ -19,18 +23,29 @@ impl <'a> System<'a> for Attacking { WriteStorage<'a, Attacked>, WriteStorage<'a, Health>, WriteStorage<'a, Dying>, - WriteStorage<'a, Removed> + WriteStorage<'a, Removed>, + ReadStorage<'a, Position>, + Write<'a, NewEntities> ); - fn run(&mut self, (entities, mut victims, mut healths, mut deads, mut removals): Self::SystemData) { + fn run(&mut self, (entities, mut victims, mut healths, mut deads, mut removals, positions, mut new): Self::SystemData) { for (ent, health, attacked) in (&entities, &mut healths, &mut victims).join() { + let mut wounded = false; for attack in attacked.attacks.drain(..) { health.health -= attack.damage; + if attack.damage > 0 { + wounded = true; + } } health.health = util::clamp(health.health, 0, health.maxhealth); if health.health == 0 { deads.insert(ent, Dying).unwrap(); removals.insert(ent, Removed).unwrap(); } + if let Some(position) = positions.get(ent){ + if wounded { + new.create(position.pos, Template::empty("wound")).unwrap(); + } + } } victims.clear(); } diff --git a/src/systems/mod.rs b/src/systems/mod.rs index b1d0994..28cd128 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -12,6 +12,7 @@ mod attacking; mod trapping; mod fight; mod heal; +mod volate; pub use self::{ controlinput::ControlInput, @@ -26,5 +27,6 @@ pub use self::{ attacking::Attacking, trapping::Trapping, fight::Fight, - heal::Heal + heal::Heal, + volate::Volate }; diff --git a/src/systems/volate.rs b/src/systems/volate.rs new file mode 100644 index 0000000..8cfa826 --- /dev/null +++ b/src/systems/volate.rs @@ -0,0 +1,34 @@ + +use specs::{ + Read, + WriteStorage, + Entities, + System, + Join +}; + +use crate::{ + components::{Volatile, Removed}, + resources::TimeStamp +}; + +pub struct Volate; +impl <'a> System<'a> for Volate { + type SystemData = ( + Entities<'a>, + WriteStorage<'a, Volatile>, + WriteStorage<'a, Removed>, + Read<'a, TimeStamp> + ); + fn run(&mut self, (entities, mut volatiles, mut removals, timestamp): Self::SystemData) { + for (ent, volatile) in (&entities, &mut volatiles).join() { + if let Some(time) = volatile.end_time { + if time <= timestamp.time { + removals.insert(ent, Removed).unwrap(); + } + } else { + volatile.end_time = Some(timestamp.time + volatile.delay); + } + } + } +} diff --git a/src/world.rs b/src/world.rs index 40d600a..aed60ff 100644 --- a/src/world.rs +++ b/src/world.rs @@ -57,15 +57,19 @@ impl <'a, 'b>World<'a, 'b> { } fn add_loaded_player(&mut self, state: PlayerState) -> Result<()> { - let roomid = state.clone().room.unwrap_or(self.default_room.clone()); + let roomid = state.clone().room.unwrap_or_else(|| self.default_room.clone()); let room = self.get_room_mut(&roomid)?; room.add_player(&state); - self.players.insert(state.id.clone(), roomid); + self.players.insert(state.id, roomid); Ok(()) } pub fn add_player(&mut self, playerid: &PlayerId) -> Result<()> { - let state = self.persistence.load_player(playerid.clone()).unwrap_or(PlayerState::new(playerid.clone())); + let state = self.persistence + .load_player(playerid.clone()) + .unwrap_or_else(|_err| // todo: what if player exists but can't be loaded for another reason? + PlayerState::new(playerid.clone()) + ); self.add_loaded_player(state) } @@ -84,7 +88,8 @@ impl <'a, 'b>World<'a, 'b> { pub fn control_player(&mut self, player: PlayerId, control: Control) -> Result<()>{ let roomid = self.players.get(&player).ok_or(aerr!("player not found"))?.clone(); - Ok(self.get_room_mut(&roomid)?.control_player(player, control)) + self.get_room_mut(&roomid)?.control_player(player, control); + Ok(()) } fn migrate_player(&mut self, player: &PlayerId, destination: RoomId, roompos: RoomPos) -> Result<()> { |
