From 88430f31c73f469086b68f2b77d1e1ba5f9178e7 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 22 Apr 2019 21:50:00 +0200 Subject: More minimal game state I'd prefer to start with just the state that I need, and progressively readd the bits that I've skipped as I find I need them, or as the competition evolves. --- src/game.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 15 deletions(-) (limited to 'src/game.rs') diff --git a/src/game.rs b/src/game.rs index 492eaf3..70960aa 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,6 +1,7 @@ use crate::geometry::*; +use crate::json; -struct GameBoard { +pub struct GameBoard { player: Player, opponent: Player, powerups: Vec, @@ -13,24 +14,14 @@ struct Player { } struct Worm { - id: i32, health: i32, - position: Point2d, - digging_range: u32, - movement_range: u32, - // This is unnecessary for now, but necessary later. I know - // for sure for the first round that all the worms will do the - // same damage and there isn't any way to change it. - weapon: Option, -} - -struct Weapon { - damage: u32, - range: u32, + position: Point2d, + weapon_damage: i32, + weapon_range: u8 } enum Powerup { - Health(Point2d, i32) + Health(Point2d, i32) } struct Map { @@ -44,3 +35,43 @@ enum CellType { Dirt, DeepSpace, } + + +impl GameBoard { + pub fn new(json: json::State) -> GameBoard { + let commando_damage = json.my_player.worms[0].weapon.damage; + let commando_range = json.my_player.worms[0].weapon.range; + + GameBoard { + player: Player { + active_worm: json.active_worm_index().unwrap(), + worms: json.my_player.worms.iter().map(|w| Worm { + health: w.health, + position: Point2d::new(w.position.x, w.position.y), + weapon_damage: commando_damage, + weapon_range: commando_range + }).collect() + }, + opponent: Player { + active_worm: 0, + worms: json.opponents.iter().flat_map(|o| &o.worms).map(|w| Worm { + health: w.health, + position: Point2d::new(w.position.x, w.position.y), + weapon_damage: commando_damage, + weapon_range: commando_range + }).collect() + }, + powerups: json.map.iter().flatten().filter_map(|c| { + c.powerup.clone().map(|p| Powerup::Health(Point2d::new(c.x, c.y), p.value)) + }).collect(), + map: Map { + size: json.map_size, + cells: json.map.iter().flatten().map(|c| match c.cell_type { + json::CellType::Air => CellType::Air, + json::CellType::Dirt => CellType::Dirt, + json::CellType::DeepSpace => CellType::DeepSpace, + }).collect() + } + } + } +} -- cgit v1.2.3