summaryrefslogtreecommitdiff
path: root/src/engine/geometry.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/geometry.rs')
-rw-r--r--src/engine/geometry.rs76
1 files changed, 21 insertions, 55 deletions
diff --git a/src/engine/geometry.rs b/src/engine/geometry.rs
index de9d95a..bfa59da 100644
--- a/src/engine/geometry.rs
+++ b/src/engine/geometry.rs
@@ -5,45 +5,36 @@ use engine::constants::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Point {
- pub x: u8,
- pub y: u8
+ pub index: u8
}
impl Point {
pub fn new(x: u8, y: u8) -> Point {
- Point { x, y }
- }
- pub fn move_left(&self) -> Option<Point> {
- self.x.checked_sub(1).map(|x| Point {
- x,
- ..*self
- })
- }
- pub fn move_right(&self, size: &Point) -> Option<Point> {
- if self.x + 1 >= size.x {
- None
+ let flipped_x = if x >= SINGLE_MAP_WIDTH {
+ FULL_MAP_WIDTH - x - 1
} else {
- Some(Point {
- x: self.x + 1,
- ..*self
- })
+ x
+ };
+ Point {
+ index: y * SINGLE_MAP_WIDTH + flipped_x
}
}
- pub fn wrapping_move_left(&mut self) {
- self.x = self.x.wrapping_sub(1);
+ pub fn new_double_bitfield(x: u8, y: u8, is_left_player: bool) -> (u64, u64) {
+ let bitfield = Point::new(x, y).to_either_bitfield();
+ if (x >= SINGLE_MAP_WIDTH) == is_left_player {
+ (0, bitfield)
+ } else {
+ (bitfield, 0)
+ }
}
- pub fn wrapping_move_right(&mut self) {
- self.x = self.x.wrapping_add(1);
+
+ pub fn x(&self) -> u8 {
+ self.index % SINGLE_MAP_WIDTH
}
- pub fn flip_x(&self) -> Point {
- let flipped_x = if self.x >= SINGLE_MAP_WIDTH {
- FULL_MAP_WIDTH - self.x - 1
- } else {
- self.x
- };
- Point::new(flipped_x, self.y)
+ pub fn y(&self) -> u8 {
+ self.index / SINGLE_MAP_WIDTH
}
}
@@ -57,31 +48,8 @@ impl Point {
* This involves mirroring the x dimension for the opponent's side
*/
-
- pub fn to_bitfield(&self) -> (u64, u64) {
- (self.to_left_bitfield(), self.to_right_bitfield())
- }
-
- pub fn to_left_bitfield(&self) -> u64 {
- if self.x >= SINGLE_MAP_WIDTH {
- 0
- } else {
- let index = self.y * SINGLE_MAP_WIDTH + self.x;
- 1 << index
- }
- }
-
- pub fn to_right_bitfield(&self) -> u64 {
- if self.x < SINGLE_MAP_WIDTH {
- 0
- } else {
- let index = self.y * SINGLE_MAP_WIDTH + FULL_MAP_WIDTH - self.x - 1;
- 1 << index
- }
- }
-
pub fn to_either_bitfield(&self) -> u64 {
- self.to_left_bitfield() | self.to_right_bitfield()
+ 1u64 << self.index
}
}
@@ -95,8 +63,6 @@ impl PartialOrd for Point {
}
impl Ord for Point {
fn cmp(&self, other: &Point) -> Ordering {
- let a = self.flip_x();
- let b = other.flip_x();
- a.y.cmp(&b.y).then(a.x.cmp(&b.x))
+ self.index.cmp(&other.index)
}
}