summaryrefslogtreecommitdiff
path: root/src/state.rs
blob: b87db1b11c7663b72d701b94978cfee603087fc3 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::command::Command;
use crate::consts::*;

pub struct GameState {
    players: [Player; 2],
    obstacles: Vec<Position>,
    powerup_oils: Vec<Position>,
    powerup_boosts: Vec<Position>,
}

pub struct Player {
    position: Position,
    next_position: Position,
    speed: usize,
    boost_remaining: usize,
    oils: usize,
    boosts: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Position {
    x: usize,
    y: usize,
}

impl GameState {
    pub fn update(&mut self, commands: [Command; 2]) {
        self.do_command(0, &commands[0]);
        self.do_command(1, &commands[1]);
        self.update_player_collisions();
        self.update_player_travel();
    }

    fn do_command(&mut self, player_index: usize, command: &Command) {
        use Command::*;

        // TODO: Command validation assertions
        match command {
            Nothing => {}
            Accelerate => self.players[player_index].accelerate(),
            Decelerate => self.players[player_index].decelerate(),
            TurnLeft => self.players[player_index].turn(false),
            TurnRight => self.players[player_index].turn(true),
            UseBoost => self.players[player_index].boost(),
            UseOil => {
                self.players[player_index].oils -= 1;
                let player_position = self.players[player_index].position;
                self.obstacles.push(Position {
                    x: player_position.x - 1,
                    y: player_position.y,
                });
            }
        }

        self.players[player_index].set_next_position_x();
    }

    fn update_player_collisions(&mut self) {

        // TODO: One player rear-ending another
        // TODO: Ending up in the same block from different lanes
    }

    fn update_player_travel(&mut self) {
        for player in &mut self.players {
            player.move_along(&self.obstacles, &self.powerup_oils, &self.powerup_boosts);
        }
    }
}

impl Player {
    fn accelerate(&mut self) {
        self.speed = match self.speed {
            i if i < SPEED_1 => SPEED_1,
            i if i < SPEED_2 => SPEED_2,
            i if i < SPEED_3 => SPEED_3,
            _ => SPEED_4,
        };
    }

    fn decelerate(&mut self) {
        self.speed = match self.speed {
            i if i <= SPEED_1 => SPEED_0,
            i if i <= SPEED_2 => SPEED_1,
            i if i <= SPEED_3 => SPEED_2,
            i if i <= SPEED_4 => SPEED_3,
            _ => SPEED_4,
        };
        self.boost_remaining = 0;
    }

    fn decelerate_from_obstacle(&mut self) {
        self.speed = match self.speed {
            i if i <= SPEED_2 => SPEED_1,
            i if i <= SPEED_3 => SPEED_2,
            i if i <= SPEED_4 => SPEED_3,
            _ => SPEED_4,
        };
        self.boost_remaining = 0;
    }

    fn turn(&mut self, right: bool) {
        if right {
            self.next_position.y = self.position.y + 1;
        } else {
            self.next_position.y = self.position.y - 1;
        }
    }

    fn boost(&mut self) {
        self.speed = SPEED_BOOST;
        self.boost_remaining = BOOST_DURATION;
        self.boosts -= 1;
    }

    fn set_next_position_x(&mut self) {
        if self.position.y == self.next_position.y {
            self.next_position.x = self.position.x + self.speed;
        } else {
            self.next_position.x = self.position.x + self.speed - 1;
        }
    }

    fn move_along(
        &mut self,
        obstacles: &[Position],
        powerup_oils: &[Position],
        powerup_boosts: &[Position],
    ) {
        let start_x = if self.position.y == self.next_position.y {
            self.position.x + 1
        } else {
            self.position.x
        };
        for x in start_x..=self.next_position.x {
            let position = Position {
                x,
                y: self.next_position.y,
            };
            if obstacles.contains(&position) {
                self.decelerate_from_obstacle();
            }
            if powerup_oils.contains(&position) {
                self.oils += 1;
            }
            if powerup_boosts.contains(&position) {
                self.boosts += 1;
            }
        }
        self.position = self.next_position;
    }
}