diff options
| author | troido <troido@protonmail.com> | 2020-03-04 20:53:42 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-04 20:53:42 +0100 |
| commit | ea99b86b89659624133a63f03600f0b57592a5f4 (patch) | |
| tree | 12263acbe0f879ddca7448e9a5cd1faccf6364fd | |
| parent | e0dcd48034e2bab9e0ae17a80a5b4d9f0f2a7900 (diff) | |
controls work with old client now too
| -rw-r--r-- | content/encyclopediae/default_encyclopedia.json | 2 | ||||
| -rw-r--r-- | src/controls.rs | 18 | ||||
| -rw-r--r-- | src/systems/useitem.rs | 2 |
3 files changed, 14 insertions, 8 deletions
diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index a94f388..47fa9d2 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -224,7 +224,7 @@ ["Item", { "ent": ["template", "radishes"], "name": ["string", "radishes"], - "action": ["action", ["eat", 1]] + "action": ["action", ["eat", 3]] }] ] } diff --git a/src/controls.rs b/src/controls.rs index 3254e00..63f418a 100644 --- a/src/controls.rs +++ b/src/controls.rs @@ -1,6 +1,6 @@ -use serde_json::Value; +use serde_json::{Value, json}; use specs::Entity; use crate::{PlayerId, Pos}; @@ -60,14 +60,22 @@ impl Control { Some(dir) => Some(Control::Move(dir)), None => None }, - "take" => Some(Control::Take(val.get(1)?.as_u64().map(|idx| idx as usize))), + "take" => Some(Control::Take(val.get(1).unwrap_or(&json!(0)).as_u64().map(|idx| idx as usize))), "drop" => Some(Control::Drop(val.get(1)?.as_u64().unwrap_or(0) as usize)), "use" => Some({ - println!("use argument {:?}", val); - if val.get(1)?.as_str()? != "inventory" { + let arr = val.as_array()?; + let mut rank = 0; + if arr.len() == 3 { + if arr[1].as_str()? != "inventory" { + return None; + } + rank = arr[2].as_u64()?; + } else if arr.len() == 2 { + rank = arr[1].as_u64()?; + } else if arr.len() > 1 { return None; } - Control::Use(val.get(2)?.as_u64().unwrap_or(0) as usize) + Control::Use(rank as usize) }), "attack" => Some(Control::Attack({ let mut directions = Vec::new(); diff --git a/src/systems/useitem.rs b/src/systems/useitem.rs index 43f38be..89a301c 100644 --- a/src/systems/useitem.rs +++ b/src/systems/useitem.rs @@ -39,9 +39,7 @@ impl <'a> System<'a> for Use { for (ent, controller, position, inventory) in (&entities, &controllers, &positions, &mut inventories).join(){ match &controller.control { Control::Use(rank) => { - println!("rank {:?}", rank); if let Some(item) = inventory.items.get(*rank) { - println!("rank {:?}", item); match &item.action { Build(template) => { new.create(position.pos, template.clone()).unwrap(); |
