diff options
| author | troido <troido@protonmail.com> | 2020-02-11 01:53:05 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-11 01:53:05 +0100 |
| commit | 62138ca19acdef140359745ebee41be5e4c5ce09 (patch) | |
| tree | a59c0d70d8b024693b2c57fc42ba3d1665623eb6 /src | |
| parent | 97850d8c1ee3522ccce30ef31ed91601da8c0730 (diff) | |
health is now also shown (but never changed)
Diffstat (limited to 'src')
| -rw-r--r-- | src/components.rs | 6 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 5 | ||||
| -rw-r--r-- | src/main.rs | 3 | ||||
| -rw-r--r-- | src/systems/view.rs | 8 | ||||
| -rw-r--r-- | src/worldmessages.rs | 6 |
5 files changed, 21 insertions, 7 deletions
diff --git a/src/components.rs b/src/components.rs index 45c8340..185459a 100644 --- a/src/components.rs +++ b/src/components.rs @@ -82,3 +82,9 @@ pub struct Item { pub name: String } +#[derive(Component, Debug, Clone)] +pub struct Health { + pub health: i64, + pub maxhealth: i64 +} + diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index feb570e..cf16213 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use specs::{Builder, world::LazyBuilder}; -use crate::components::{Visible, Blocking, Player, Floor, Item, Inventory}; +use crate::components::{Visible, Blocking, Player, Floor, Item, Inventory, Health}; use crate::parameter::{Parameter, ParameterType}; @@ -93,7 +93,8 @@ components!( Floor () {Floor}; Player (name: String) {Player::new(name)}; Item (ent: Template, name: String) {Item{ent, name}}; - Inventory (capacity: Int) {Inventory{items: Vec::new(), capacity: capacity as usize}} + Inventory (capacity: Int) {Inventory{items: Vec::new(), capacity: capacity as usize}}; + Health (health: Int, maxhealth: Int) {Health{health, maxhealth}} ); diff --git a/src/main.rs b/src/main.rs index 1c253ea..15171e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -241,7 +241,8 @@ fn default_assemblages() -> Encyclopedia { ["Player", { "name": ["arg", "name"] }], - ["Inventory", {"capacity": ["int", 3]}] + ["Inventory", {"capacity": ["int", 3]}], + ["Health", {"health": ["int", 9], "maxhealth": ["int", 10]}] ] } })).unwrap() diff --git a/src/systems/view.rs b/src/systems/view.rs index e0e5650..57e2b28 100644 --- a/src/systems/view.rs +++ b/src/systems/view.rs @@ -12,7 +12,7 @@ use specs::{ }; use super::super::pos::Pos; -use super::super::components::{Visible, Player, Position, Inventory, New, Moved, Removed}; +use super::super::components::{Visible, Player, Position, Inventory, New, Moved, Removed, Health}; use super::super::resources::{Size, Output, Ground}; use super::super::worldmessages::{WorldMessage, WorldUpdate, FieldMessage}; @@ -25,6 +25,7 @@ impl <'a> System<'a> for View { Entities<'a>, ReadStorage<'a, Position>, ReadStorage<'a, Inventory>, + ReadStorage<'a, Health>, ReadStorage<'a, Visible>, Read<'a, Size>, ReadStorage<'a, Player>, @@ -34,7 +35,7 @@ impl <'a> System<'a> for View { ReadStorage<'a, Removed>, Read<'a, Ground> ); - fn run(&mut self, (entities, positions, inventories, visible, size, players, mut output, new, moved, removed, ground): Self::SystemData) { + fn run(&mut self, (entities, positions, inventories, healths, visible, size, players, mut output, new, moved, removed, ground): Self::SystemData) { let mut changed = HashSet::new(); for (pos, _new) in (&positions, &new).join() { @@ -76,6 +77,9 @@ impl <'a> System<'a> for View { if let Some(inventory) = inventories.get(ent){ updates.push(WorldUpdate::Inventory(inventory.items.iter().map(|item| item.name.clone()).collect())); } + if let Some(health) = healths.get(ent){ + updates.push(WorldUpdate::Health(health.health, health.maxhealth)); + } updates.push(WorldUpdate::Pos(pos.pos)); let message = WorldMessage{updates}; output.output.insert(player.name.clone(), message); diff --git a/src/worldmessages.rs b/src/worldmessages.rs index 48719f6..39eee6a 100644 --- a/src/worldmessages.rs +++ b/src/worldmessages.rs @@ -21,7 +21,8 @@ pub enum WorldUpdate { Field(FieldMessage), Pos(Pos), Change(Vec<(Pos, Vec<String>)>), - Inventory(Vec<String>) + Inventory(Vec<String>), + Health(i64, i64) } impl ToJson for WorldUpdate { @@ -30,7 +31,8 @@ impl ToJson for WorldUpdate { WorldUpdate::Field(msg) => json!(["field", msg]), WorldUpdate::Pos(pos) => json!(["playerpos", pos]), WorldUpdate::Change(changes) => json!(["changecells", changes]), - WorldUpdate::Inventory(items) => json!(["inventory", items]) + WorldUpdate::Inventory(items) => json!(["inventory", items]), + WorldUpdate::Health(health, maxhealth) => json!(["health", [health, maxhealth]]) } } } |
