diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2019-04-22 21:50:00 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2019-04-22 21:50:00 +0200 |
commit | 88430f31c73f469086b68f2b77d1e1ba5f9178e7 (patch) | |
tree | 292a0aceba92e4d0c38679ed919b9b463c82152b /src/game.rs | |
parent | 3e54b01003aa9d27de8f4ca13c9240fe785ec0e1 (diff) |
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.
Diffstat (limited to 'src/game.rs')
-rw-r--r-- | src/game.rs | 61 |
1 files changed, 46 insertions, 15 deletions
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<Powerup>, @@ -13,24 +14,14 @@ struct Player { } struct Worm { - id: i32, health: i32, - position: Point2d<u8>, - 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<Weapon>, -} - -struct Weapon { - damage: u32, - range: u32, + position: Point2d<i8>, + weapon_damage: i32, + weapon_range: u8 } enum Powerup { - Health(Point2d<u8>, i32) + Health(Point2d<i8>, 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() + } + } + } +} |