diff options
| author | troido <troido@protonmail.com> | 2020-01-28 22:14:39 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-01-28 22:14:39 +0100 |
| commit | 1175f8b436d15c47fb60866755921fc68183dc72 (patch) | |
| tree | fb4fd4c563a72ecd264823a8d47dc519c312d03d /src/controls.rs | |
| parent | 3280e0bf472f418f1b4f209b1355fcaa1db163c6 (diff) | |
player is now controllable
Diffstat (limited to 'src/controls.rs')
| -rw-r--r-- | src/controls.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/controls.rs b/src/controls.rs new file mode 100644 index 0000000..58c83b0 --- /dev/null +++ b/src/controls.rs @@ -0,0 +1,64 @@ + + +use serde_json::Value; + +#[derive(Debug)] +pub enum Direction { + North, + South, + East, + West, + None +} + +impl Direction { + fn from_json(val: &Value) -> Option<Direction>{ + match val { + Value::String(txt) => match txt.as_str() { + "north" => Some(Direction::North), + "south" => Some(Direction::South), + "east" => Some(Direction::East), + "west"=> Some(Direction::West), + "" => Some(Direction::None), + _ => None + } + Value::Null => Some(Direction::None), + _ => None + } + } + + pub fn to_position(&self) -> (i32, i32) { + match self { + Direction::North => (0, -1), + Direction::South => (0, 1), + Direction::East => (1, 0), + Direction::West => (-1, 0), + Direction::None => (0, 0) + } + } +} + +#[derive(Debug)] +pub enum Control { + Move(Direction), + Take(u64) +} + + +impl Control { + pub fn from_json(val: Value) -> Option<Control>{ + if let Value::String(control_type) = &val[0] { + match control_type.as_str() { + "move" => match Direction::from_json(&val[1]) { + Some(dir) => Some(Control::Move(dir)), + None => None + }, + "take" => match val[1].as_u64() { + Some(rank) => Some(Control::Take(rank)), + _ => None + } + _ => None + } + } else {None} + } +} |
