summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-19 10:55:16 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-19 10:55:16 +0200
commit9caffaa6b67d26560178992d6bee8e2661496276 (patch)
tree055576e337e3ae80942fcb9aa94343ea7da20da7
parent556cb56d46b9d9bc8ec1ce0ba09ab5bc38c6022b (diff)
Use btree ranges
-rw-r--r--src/state.rs41
1 files changed, 24 insertions, 17 deletions
diff --git a/src/state.rs b/src/state.rs
index b898e4b..0a9c2f2 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -1,6 +1,7 @@
use crate::command::Command;
use crate::consts::*;
use std::collections::BTreeSet;
+use std::ops::Bound::{Excluded, Included};
use std::rc::Rc;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
@@ -239,25 +240,31 @@ impl Player {
powerup_boosts: &BTreeSet<Position>,
finish_lines: &BTreeSet<Position>,
) {
- let start_x = self.position.x.saturating_add(1);
- for x in start_x..=self.next_position.x {
- let position = Position {
- x,
+ let range = (
+ Included(Position {
y: self.next_position.y,
- };
- if obstacles.contains(&position) {
- self.decelerate_from_obstacle();
- }
- if powerup_oils.contains(&position) {
- self.oils = self.oils.saturating_add(1);
- }
- if powerup_boosts.contains(&position) {
- self.boosts = self.boosts.saturating_add(1);
- }
- if finish_lines.contains(&position) {
- self.finished = true;
- }
+ x: self.position.x.saturating_add(1),
+ }),
+ Excluded(Position {
+ y: self.next_position.y,
+ x: self.next_position.x.saturating_add(1),
+ }),
+ );
+
+ for _ in obstacles.range(range) {
+ self.decelerate_from_obstacle();
+ }
+ self.oils = self
+ .oils
+ .saturating_add(powerup_oils.range(range).count() as u16);
+ self.boosts = self
+ .boosts
+ .saturating_add(powerup_boosts.range(range).count() as u16);
+
+ if finish_lines.range(range).count() > 0 {
+ self.finished = true;
}
+
self.position = self.next_position;
}
}