summaryrefslogtreecommitdiff
path: root/2018-tower-defence/src/engine/geometry.rs
blob: 9cd1d906773dfd61647ace243f6fc334a1e2bbc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)
    }
}