diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 21:29:11 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 21:29:11 +0200 |
commit | 01a849c6b26afd076989f645c6153d8ce1422281 (patch) | |
tree | a02fbbfbd958b8492dd3dc7bd58b4bb1d8566080 /2018-tower-defence/src/engine/geometry.rs | |
parent | 59f8b6a8ef1abfd0556cc940ccb64305fcdf2968 (diff) | |
parent | 7ec48d0d454499177b63bc5bd512a3a2d6baa839 (diff) |
Merge branch 'tower-defence-main'
Diffstat (limited to '2018-tower-defence/src/engine/geometry.rs')
-rw-r--r-- | 2018-tower-defence/src/engine/geometry.rs | 71 |
1 files changed, 71 insertions, 0 deletions
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<Ordering> { + Some(self.cmp(other)) + } +} +impl Ord for Point { + fn cmp(&self, other: &Point) -> Ordering { + self.index.cmp(&other.index) + } +} |