summaryrefslogtreecommitdiff
path: root/src/game.rs
blob: 626a3775c16082538fee4847cb80bbf394e7cdf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::geometry::*;
use crate::command::Command;
use crate::json;

pub struct GameBoard {
    players: [Player; 2],
    powerups: Vec<Powerup>,
    map: Map,
}

struct Player {
    active_worm: usize,
    worms: Vec<Worm>
}

struct Worm {
    id: i32,
    health: i32,
    position: Point2d<i8>,
    weapon_damage: i32,
    weapon_range: u8
}

enum Powerup {
    Health(Point2d<i8>, i32)
}

struct Map {
    size: u8,
    /// This is 2d, each row is size long
    cells: Vec<CellType>
}

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
    }
}