1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
use std::collections::HashMap;
use serde_json::{Value, json};
use crate::{
Template,
componentwrapper::{ComponentWrapper, PreEntity},
PlayerId,
RoomId,
ItemId,
components::{
Visible,
Player,
Inventory,
inventory::InventoryEntry,
Health,
Fighter,
Healing,
Movable,
AttackType,
Autofight,
Faction,
Equipment,
equipment::Slot,
Ear
},
Result,
aerr,
Sprite,
Encyclopedia,
Pos,
hashmap,
PResult,
perr
};
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum RoomPos {
Pos(Pos),
Name(String),
Unknown
}
#[derive(Debug, Clone)]
pub struct PlayerState {
pub id: PlayerId,
pub room: Option<RoomId>,
pub pos: RoomPos,
pub inventory_capacity: usize,
pub inventory: Vec<(ItemId, bool)>,
pub health: i64,
pub maximum_health: i64,
pub equipment: HashMap<Slot, Option<Template>>
}
impl PlayerState {
pub fn new(id: PlayerId) -> Self {
Self{
id,
room: None,
pos: RoomPos::Unknown,
inventory: Vec::new(),
inventory_capacity: 10,
health: 25,
maximum_health: 50,
equipment: hashmap!(Slot::Hand => None, Slot::Body => None)
}
}
pub fn create(id: PlayerId, room: RoomId, inventory: Vec<(ItemId, bool)>, inventory_capacity: usize, health: i64, maximum_health: i64, equipment: HashMap<Slot, Option<Template>>) -> Self {
Self {
id,
room: Some(room),
pos: RoomPos::Unknown,
inventory,
health,
inventory_capacity,
maximum_health,
equipment
}
}
pub fn to_json(&self) -> Value {
json!({
"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(|(item, e)| (json!(item.0), *e)).collect::<Vec<(Value, bool)>>()
},
"equipment": {
"hand": null,
"body": null
},
"health": self.health,
"maxhealth": self.maximum_health
})
}
pub fn from_json(val: &Value) -> PResult<Self> {
let inventory = val.get("inventory").ok_or(perr!("player json does not have inventory"))?;
let items =
inventory
.get("items")
.ok_or(perr!("inventory does not have items"))?
.as_array()
.ok_or(perr!("inventory items not an array"))?
.iter()
.map(|entry| {
let itemid = ItemId(
entry
.get(0)
.ok_or(perr!("item does not have name"))?
.as_str()
.ok_or(perr!("item name not a string"))?
.to_string()
);
let is_equipped =
entry
.get(1)
.ok_or(perr!("item does not have equipped flag"))?
.as_bool()
.ok_or(perr!("item is_equipped not a bool"))?;
Ok((itemid, is_equipped))
})
.collect::<PResult<Vec<(ItemId, bool)>>>()?;
Ok(Self {
id: PlayerId{name: val.get("name").ok_or(perr!("player json does not have name"))?.as_str().ok_or(perr!("player name not a string"))?.to_string()},
room: match val.get("roomname").ok_or(perr!("player json does not have room name"))? {
Value::String(name) => Some(RoomId::from_str(name)),
_ => None
},
pos: RoomPos::Unknown,
inventory: items,
health: val.get("health").ok_or(perr!("player json does not have health"))?.as_i64().ok_or(perr!("player health not a number"))?,
inventory_capacity: inventory.get("capacity").ok_or(perr!("inventory does not have capacity"))?.as_i64().ok_or(perr!("inventory capacity not a number"))? as usize,
maximum_health: val.get("maxhealth").ok_or(perr!("player json does not have maxhealth"))?.as_i64().ok_or(perr!("maxhealth not a number"))?,
equipment: HashMap::new()
})
}
pub fn respawn(&mut self) {
self.room = None;
self.pos = RoomPos::Unknown;
self.health = self.maximum_health / 2;
}
pub fn construct(&self, encyclopedia: &Encyclopedia) -> Result<PreEntity> {
Ok(vec![
ComponentWrapper::Visible(Visible{sprite: Sprite{name: "player".to_string()}, height: 1.75, name: self.id.name.clone()}),
ComponentWrapper::Player(Player::new(self.id.clone())),
ComponentWrapper::Inventory(Inventory{
items: self.inventory.iter().map( |(itemid, is_equipped)| {
let item = encyclopedia.get_item(itemid).ok_or(aerr!("failed to load item '{:?} in inventory of player {:?}", itemid, self))?;
Ok(InventoryEntry{itemid: itemid.clone(), item, is_equipped: *is_equipped})
}).collect::<Result<Vec<InventoryEntry>>>()?,
capacity: self.inventory_capacity
}),
ComponentWrapper::Health(Health{health: self.health, maxhealth: self.maximum_health}),
ComponentWrapper::Fighter(Fighter{attack: AttackType::Attack(5), cooldown: 8, range: 1}),
ComponentWrapper::Healing(Healing{delay: 50, health: 1, next_heal: None}),
ComponentWrapper::Movable(Movable{cooldown: 2}),
ComponentWrapper::Autofight(Autofight::default()),
ComponentWrapper::Faction(Faction::Good),
ComponentWrapper::Equipment(Equipment{slots: vec!(Slot::Hand, Slot::Body)}),
ComponentWrapper::Ear(Ear::default())
])
}
}
|