utiles_core/
sibling_relationship.rs

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
//! Child index as related to its parent/sibling-tiles
use crate::tile::Tile;

/// Sibling relationship for tiles
pub enum SiblingRelationship {
    /// `UpperLeft` sibling
    UpperLeft = 0,
    /// `UpperRight` sibling
    UpperRight = 1,
    /// `LowerLeft` sibling
    LowerLeft = 2,
    /// `LowerRight` sibling
    LowerRight = 3,
}

impl From<(u32, u32)> for SiblingRelationship {
    fn from(value: (u32, u32)) -> Self {
        let is_left = value.0 % 2 == 0;
        let is_top = value.1 % 2 == 0;
        match (is_left, is_top) {
            (true, true) => SiblingRelationship::UpperLeft,
            (true, false) => SiblingRelationship::LowerLeft,
            (false, true) => SiblingRelationship::UpperRight,
            (false, false) => SiblingRelationship::LowerRight,
        }
    }
}

impl From<Tile> for SiblingRelationship {
    fn from(value: Tile) -> Self {
        let is_left = value.x % 2 == 0;
        let is_top = value.y % 2 == 0;
        match (is_left, is_top) {
            (true, true) => SiblingRelationship::UpperLeft,
            (true, false) => SiblingRelationship::LowerLeft,
            (false, true) => SiblingRelationship::UpperRight,
            (false, false) => SiblingRelationship::LowerRight,
        }
    }
}