From 9caffaa6b67d26560178992d6bee8e2661496276 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Sun, 19 Apr 2020 10:55:16 +0200 Subject: Use btree ranges --- src/state.rs | 41 ++++++++++++++++++++++++----------------- 1 file 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, finish_lines: &BTreeSet, ) { - 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; } } -- cgit v1.2.3