blob: 3c8f91882d7afd755dab772d891f677d36cac99f (
plain)
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
|
use serde_json::{Value};
use specs::{
Component,
HashMapStorage
};
use crate::{
Template
};
#[derive(Component, Debug, Clone, PartialEq)]
#[storage(HashMapStorage)]
pub enum Interactable {
Harvest,
Change(Template),
Say(String)
}
impl Interactable {
pub fn from_json(val: &Value) -> Option<Self> {
let typ = if val.is_string() {val} else {val.get(0)?};
let arg = if val.is_string() {&Value::Null} else {val.get(1)?};
match typ.as_str()? {
"harvest" => Some(Interactable::Harvest),
"change" => Some(Interactable::Change(Template::from_json(arg).ok()?)),
"say" => Some(Interactable::Say(arg.as_str()?.to_string())),
_ => None
}
}
}
|