summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-10 22:15:55 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-10 22:15:55 +0200
commit7bdae0763fa4a63424a19c51d5a2a372ee067695 (patch)
tree56fc914474b01370f28e2c689b9486c662c3c998
parent82ceee2c432d26589834e803af59b710a66a2636 (diff)
Collisions
-rw-r--r--src/state.rs24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/state.rs b/src/state.rs
index b87db1b..ca18f71 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -56,9 +56,22 @@ impl GameState {
}
fn update_player_collisions(&mut self) {
-
- // TODO: One player rear-ending another
- // TODO: Ending up in the same block from different lanes
+ let same_lanes_before = self.players[0].position.y == self.players[1].position.y;
+ let same_lanes_after = self.players[0].next_position.y == self.players[1].next_position.y;
+ let first_passing_second = self.players[0].position.x < self.players[1].position.x
+ && self.players[0].next_position.x >= self.players[1].next_position.x;
+ let second_passing_first = self.players[1].position.x < self.players[0].position.x
+ && self.players[1].next_position.x >= self.players[0].next_position.x;
+ let same_x_after = self.players[0].next_position.x == self.players[1].next_position.x;
+
+ if same_lanes_before && same_lanes_after && first_passing_second {
+ self.players[0].next_position.x = self.players[1].next_position.x - 1;
+ } else if same_lanes_before && same_lanes_after && second_passing_first {
+ self.players[1].next_position.x = self.players[0].next_position.x - 1;
+ } else if same_lanes_after && same_x_after {
+ self.players[0].bonked_while_changing_lanes();
+ self.players[1].bonked_while_changing_lanes();
+ }
}
fn update_player_travel(&mut self) {
@@ -121,6 +134,11 @@ impl Player {
}
}
+ fn bonked_while_changing_lanes(&mut self) {
+ self.next_position.y = self.position.y;
+ self.next_position.x = self.position.x + self.speed - 1;
+ }
+
fn move_along(
&mut self,
obstacles: &[Position],