summaryrefslogtreecommitdiff
path: root/src/playerstate.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-18 01:11:49 +0100
committertroido <troido@protonmail.com>2020-02-18 01:11:49 +0100
commitc71ecb48fa4368035a852e2d06869a21382a6876 (patch)
tree8d5a598fedf4e42bd6d8d1b0d476301dede2f20c /src/playerstate.rs
parentc921686355c86afb5bf47e4b6c696057ede01b1f (diff)
Players are now saved/loaded too
Diffstat (limited to 'src/playerstate.rs')
-rw-r--r--src/playerstate.rs50
1 files changed, 40 insertions, 10 deletions
diff --git a/src/playerstate.rs b/src/playerstate.rs
index dac463b..e68e890 100644
--- a/src/playerstate.rs
+++ b/src/playerstate.rs
@@ -2,27 +2,43 @@
use serde_json::{Value, json};
use crate::template::Template;
+use crate::{
+ componentwrapper::{ComponentWrapper, PreEntity},
+ PlayerId,
+ components::{Visible, Player, Inventory, Health, Item}
+};
-
+#[derive(Debug, Clone)]
pub struct PlayerState {
- name: String,
- room: String,
- inventory_capacity: usize,
- inventory: Vec<Template>,
- health: i64,
- maximum_health: i64
+ pub name: String,
+ pub room: String,
+ pub inventory_capacity: usize,
+ pub inventory: Vec<Template>,
+ pub health: i64,
+ pub maximum_health: i64
}
impl PlayerState {
- pub fn new(name: String, room: String, inventory: Vec<Template>, health: i64) -> Self {
+ pub fn new(name: String) -> Self {
+ Self{
+ name: name,
+ room: String::new(),
+ inventory: Vec::new(),
+ inventory_capacity: 10,
+ health: 9,
+ maximum_health: 10
+ }
+ }
+
+ pub fn create(name: String, room: String, inventory: Vec<Template>, inventory_capacity: usize, health: i64, maximum_health: i64) -> Self {
Self {
name,
room,
inventory,
health,
- inventory_capacity: 10,
- maximum_health: 50
+ inventory_capacity,
+ maximum_health
}
}
@@ -58,4 +74,18 @@ impl PlayerState {
maximum_health: val.get("maxhealth")?.as_i64()?
})
}
+
+ pub fn construct(&self, id: PlayerId) -> PreEntity {
+ vec![
+ ComponentWrapper::Visible(Visible{sprite: "player".to_string(), height: 1.0}),
+ ComponentWrapper::Player(Player::new(id)),
+ ComponentWrapper::Inventory(Inventory{
+ items: self.inventory.iter().map(
+ |template| Item{ent: template.clone(), name: template.name.clone()}
+ ).collect(),
+ capacity: self.inventory_capacity
+ }),
+ ComponentWrapper::Health(Health{health: self.health, maxhealth: self.maximum_health})
+ ]
+ }
}