summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-10 22:49:52 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-10 22:49:52 +0200
commit124c89cdeadc53457b016c9f8aa1ba3c4a1aa15d (patch)
treef96426ba6f7ebcf502e79ccad3312890aceed891
parent7bdae0763fa4a63424a19c51d5a2a372ee067695 (diff)
Victory conditions
-rw-r--r--src/state.rs60
1 files changed, 51 insertions, 9 deletions
diff --git a/src/state.rs b/src/state.rs
index ca18f71..45e39eb 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -1,11 +1,20 @@
use crate::command::Command;
use crate::consts::*;
+pub enum GameStatus {
+ Continue,
+ PlayerOneWon,
+ PlayerTwoWon,
+ Draw, // Until I add score I guess
+}
+
pub struct GameState {
+ status: GameStatus,
players: [Player; 2],
obstacles: Vec<Position>,
powerup_oils: Vec<Position>,
powerup_boosts: Vec<Position>,
+ finish_lines: Vec<Position>,
}
pub struct Player {
@@ -15,6 +24,7 @@ pub struct Player {
boost_remaining: usize,
oils: usize,
boosts: usize,
+ finished: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -29,6 +39,22 @@ impl GameState {
self.do_command(1, &commands[1]);
self.update_player_collisions();
self.update_player_travel();
+
+ self.status = if self.players[0].finished && self.players[1].finished {
+ if self.players[0].speed > self.players[1].speed {
+ GameStatus::PlayerOneWon
+ } else if self.players[0].speed < self.players[1].speed {
+ GameStatus::PlayerTwoWon
+ } else {
+ GameStatus::Draw
+ }
+ } else if self.players[0].finished {
+ GameStatus::PlayerOneWon
+ } else if self.players[1].finished {
+ GameStatus::PlayerTwoWon
+ } else {
+ GameStatus::Continue
+ };
}
fn do_command(&mut self, player_index: usize, command: &Command) {
@@ -39,8 +65,8 @@ impl GameState {
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),
+ TurnLeft => self.players[player_index].turn_left(),
+ TurnRight => self.players[player_index].turn_right(),
UseBoost => self.players[player_index].boost(),
UseOil => {
self.players[player_index].oils -= 1;
@@ -76,7 +102,12 @@ impl GameState {
fn update_player_travel(&mut self) {
for player in &mut self.players {
- player.move_along(&self.obstacles, &self.powerup_oils, &self.powerup_boosts);
+ player.move_along(
+ &self.obstacles,
+ &self.powerup_oils,
+ &self.powerup_boosts,
+ &self.finish_lines,
+ );
}
}
}
@@ -112,12 +143,12 @@ impl Player {
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 turn_left(&mut self) {
+ self.next_position.y = self.position.y - 1;
+ }
+
+ fn turn_right(&mut self) {
+ self.next_position.y = self.position.y + 1;
}
fn boost(&mut self) {
@@ -126,6 +157,13 @@ impl Player {
self.boosts -= 1;
}
+ fn tick_boost(&mut self) {
+ self.boost_remaining = self.boost_remaining.saturating_sub(1);
+ if self.boost_remaining == 0 && self.speed == SPEED_BOOST {
+ self.speed = SPEED_4;
+ }
+ }
+
fn set_next_position_x(&mut self) {
if self.position.y == self.next_position.y {
self.next_position.x = self.position.x + self.speed;
@@ -144,6 +182,7 @@ impl Player {
obstacles: &[Position],
powerup_oils: &[Position],
powerup_boosts: &[Position],
+ finish_lines: &[Position],
) {
let start_x = if self.position.y == self.next_position.y {
self.position.x + 1
@@ -164,6 +203,9 @@ impl Player {
if powerup_boosts.contains(&position) {
self.boosts += 1;
}
+ if finish_lines.contains(&position) {
+ self.finished = true;
+ }
}
self.position = self.next_position;
}