summaryrefslogtreecommitdiff
path: root/src/playerstate.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-18 02:27:21 +0100
committertroido <troido@protonmail.com>2020-02-18 02:27:21 +0100
commit32dd60bca02cf2cfccc8d4309691df9f2f84398c (patch)
treee6d67140f3f0ca2272f796775a9dc65e5b25484f /src/playerstate.rs
parentc71ecb48fa4368035a852e2d06869a21382a6876 (diff)
refactoring: roomid is now a type, not string anymore
Diffstat (limited to 'src/playerstate.rs')
-rw-r--r--src/playerstate.rs35
1 files changed, 21 insertions, 14 deletions
diff --git a/src/playerstate.rs b/src/playerstate.rs
index e68e890..d06e3e7 100644
--- a/src/playerstate.rs
+++ b/src/playerstate.rs
@@ -5,13 +5,14 @@ use crate::template::Template;
use crate::{
componentwrapper::{ComponentWrapper, PreEntity},
PlayerId,
+ RoomId,
components::{Visible, Player, Inventory, Health, Item}
};
#[derive(Debug, Clone)]
pub struct PlayerState {
- pub name: String,
- pub room: String,
+ pub id: PlayerId,
+ pub room: Option<RoomId>,
pub inventory_capacity: usize,
pub inventory: Vec<Template>,
pub health: i64,
@@ -20,10 +21,10 @@ pub struct PlayerState {
impl PlayerState {
- pub fn new(name: String) -> Self {
+ pub fn new(id: PlayerId) -> Self {
Self{
- name: name,
- room: String::new(),
+ id: id,
+ room: None,
inventory: Vec::new(),
inventory_capacity: 10,
health: 9,
@@ -31,10 +32,10 @@ impl PlayerState {
}
}
- pub fn create(name: String, room: String, inventory: Vec<Template>, inventory_capacity: usize, health: i64, maximum_health: i64) -> Self {
+ pub fn create(id: PlayerId, room: RoomId, inventory: Vec<Template>, inventory_capacity: usize, health: i64, maximum_health: i64) -> Self {
Self {
- name,
- room,
+ id,
+ room: Some(room),
inventory,
health,
inventory_capacity,
@@ -44,8 +45,11 @@ impl PlayerState {
pub fn to_json(&self) -> Value {
json!({
- "name": self.name,
- "roomname": self.room,
+ "name": self.id.name,
+ "roomname": match &self.room {
+ Some(id) => json!(id.to_string()),
+ None => json!(null)
+ },
"inventory": {
"capacity": self.inventory_capacity,
"items": self.inventory.iter().map(Template::to_json).collect::<Vec<Value>>()
@@ -66,8 +70,11 @@ impl PlayerState {
items.push(Template::from_json(item)?);
}
Some(Self {
- name: val.get("name")?.as_str()?.to_string(),
- room: val.get("roomname")?.as_str()?.to_string(),
+ id: PlayerId{name: val.get("name")?.as_str()?.to_string()},
+ room: match val.get("roomname")? {
+ Value::String(name) => Some(RoomId::from_str(name)),
+ _ => None
+ },
inventory: items,
health: val.get("health")?.as_i64()?,
inventory_capacity: inventory.get("capacity")?.as_i64()? as usize,
@@ -75,10 +82,10 @@ impl PlayerState {
})
}
- pub fn construct(&self, id: PlayerId) -> PreEntity {
+ pub fn construct(&self) -> PreEntity {
vec![
ComponentWrapper::Visible(Visible{sprite: "player".to_string(), height: 1.0}),
- ComponentWrapper::Player(Player::new(id)),
+ ComponentWrapper::Player(Player::new(self.id.clone())),
ComponentWrapper::Inventory(Inventory{
items: self.inventory.iter().map(
|template| Item{ent: template.clone(), name: template.name.clone()}