use crate::geometry::*; use crate::command::Command; use crate::json; pub struct GameBoard { players: [Player; 2], powerups: Vec, map: Map, } struct Player { active_worm: usize, worms: Vec } struct Worm { id: i32, health: i32, position: Point2d, weapon_damage: i32, weapon_range: u8 } enum Powerup { Health(Point2d, i32) } struct Map { size: u8, /// This is 2d, each row is size long cells: Vec } enum CellType { Air, Dirt, DeepSpace, } enum SimulationOutcome { PlayerWon(usize), Draw, Continue, } 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; let player = Player { active_worm: json.active_worm_index().unwrap(), worms: json.my_player.worms.iter().map(|w| Worm { id: w.id, health: w.health, position: Point2d::new(w.position.x, w.position.y), weapon_damage: commando_damage, weapon_range: commando_range }).collect() }; let opponent = Player { active_worm: 0, worms: json.opponents.iter().flat_map(|o| &o.worms).map(|w| Worm { id: w.id, health: w.health, position: Point2d::new(w.position.x, w.position.y), weapon_damage: commando_damage, weapon_range: commando_range }).collect() }; GameBoard { players: [player, opponent], 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() } } } pub fn update(&mut self, _json: json::State) { // TODO // What can change? // - Worm health (and dead worms die) // - Active worms += 1 // - The worms may move // - The powerups may be taken // - The map cells may change from dirt to not dirt } pub fn simulate(&mut self, moves: [Command; 2]) -> SimulationOutcome { SimulationOutcome::Continue } }