From 7ec48d0d454499177b63bc5bd512a3a2d6baa839 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 19 Apr 2022 21:26:49 +0200 Subject: Refile for merging repos --- 2018-tower-defence/src/engine/geometry.rs | 71 +++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 2018-tower-defence/src/engine/geometry.rs (limited to '2018-tower-defence/src/engine/geometry.rs') diff --git a/2018-tower-defence/src/engine/geometry.rs b/2018-tower-defence/src/engine/geometry.rs new file mode 100644 index 0000000..9cd1d90 --- /dev/null +++ b/2018-tower-defence/src/engine/geometry.rs @@ -0,0 +1,71 @@ +use engine::constants::*; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Point { + pub index: u8 +} + +impl Point { + pub fn new(x: u8, y: u8) -> Point { + let flipped_x = if x >= SINGLE_MAP_WIDTH { + FULL_MAP_WIDTH - x - 1 + } else { + x + }; + Point { + index: y * SINGLE_MAP_WIDTH + flipped_x + } + } + + pub fn new_index(index: u8) -> Point { + Point { + index + } + } + + pub fn new_double_bitfield(x: u8, y: u8, is_left_player: bool) -> (u64, u64) { + let bitfield = Point::new(x, y).to_bitfield(); + if (x >= SINGLE_MAP_WIDTH) == is_left_player { + (0, bitfield) + } else { + (bitfield, 0) + } + } + + pub fn x(self) -> u8 { + self.index % SINGLE_MAP_WIDTH + } + + pub fn y(self) -> u8 { + self.index / SINGLE_MAP_WIDTH + } +} + +impl Point { + /** + * # Bitfields + * + * 0,0 is the top left point. + * >> (towards 0) moves bits towards the player that owns that side + * << (towards max) moves bits towards the opponent + * This involves mirroring the x dimension for the opponent's side + */ + + pub fn to_bitfield(self) -> u64 { + 1u64 << self.index + } +} + +use std::cmp::Ord; +use std::cmp::Ordering; + +impl PartialOrd for Point { + fn partial_cmp(&self, other: &Point) -> Option { + Some(self.cmp(other)) + } +} +impl Ord for Point { + fn cmp(&self, other: &Point) -> Ordering { + self.index.cmp(&other.index) + } +} -- cgit v1.2.3