summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-22 23:41:51 +0100
committertroido <troido@protonmail.com>2020-02-22 23:41:51 +0100
commit522aad7889cd62e96af7c420789507ccbf5b7aaa (patch)
treebfb3c21d627552bcda6f2743854cee920722fd3b /src/components
parentf4331041e5d906f95063f317852f32f19e6cdf9c (diff)
it is now possible to use items
Diffstat (limited to 'src/components')
-rw-r--r--src/components/item.rs30
-rw-r--r--src/components/mod.rs8
2 files changed, 36 insertions, 2 deletions
diff --git a/src/components/item.rs b/src/components/item.rs
index 76c5c1c..bcc672a 100644
--- a/src/components/item.rs
+++ b/src/components/item.rs
@@ -9,10 +9,36 @@ pub struct Item {
pub action: ItemAction
}
-#[derive(Debug, Clone)]
+
+
+use serde_json::{json, Value};
+
+#[derive(Debug, Clone, PartialEq)]
pub enum ItemAction {
- Eat{health: i64},
+ Eat(i64),
Build(Template),
None
}
+use ItemAction::{Eat, Build, None};
+
+impl ItemAction {
+ pub fn to_json(&self) -> Value {
+ match self {
+ Eat(health) => json!(["eat", health]),
+ Build(template) => json!(["build", template.to_json()]),
+ None => json!(["none", null])
+ }
+ }
+
+ pub fn from_json(val: &Value) -> Option<Self> {
+ let typ = val.get(0)?;
+ let arg = val.get(1)?;
+ Some(match typ.as_str()? {
+ "eat" => Eat(arg.as_i64()?),
+ "build" => Build(Template::from_json(arg).ok()?),
+ "none" => None,
+ _ => {return Option::None}
+ })
+ }
+}
diff --git a/src/components/mod.rs b/src/components/mod.rs
index a0ccde4..459590b 100644
--- a/src/components/mod.rs
+++ b/src/components/mod.rs
@@ -84,6 +84,14 @@ pub struct Health {
pub health: i64,
pub maxhealth: i64
}
+impl Health {
+ pub fn heal(&mut self, amount: i64) {
+ self.health += amount;
+ if self.health > self.maxhealth {
+ self.health = self.maxhealth;
+ }
+ }
+}
#[derive(Component, Debug, Clone)]
pub struct Serialise {