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
use rand::Rng;
use std::fmt::Debug;
use serde::{Serialize, Deserialize};

use super::hexagon::*;
use super::square::*;
use super::triangle::*;

pub trait DirectionTrait
    : PartialEq + Eq + Copy + Serialize + Deserialize + Clone + Debug {
    fn variants() -> &'static [Self];
}

pub trait VectorTrait
    : PartialEq + Eq + Copy + Serialize + Deserialize + Clone + Debug {
    type Direction: DirectionTrait;

    fn distance(&self, other: &Self) -> usize;
    fn neighbour(&self, direction: &Self::Direction) -> Self;
    fn neighbours(&self) -> Vec<Self>;
}

pub trait GridTrait
    : PartialEq + Eq + Copy + Serialize + Deserialize + Clone + Debug {
    type Vector: VectorTrait;

    fn dimensions(&self) -> Vec<isize>;
    fn is_within_bounds(&self, v: Self::Vector) -> bool;
    fn cells(&self) -> Vec<Self::Vector>;
    fn random_cell<R: Rng>(&self, rng: &mut R) -> Self::Vector;
}

#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "tiling", rename_all = "snake_case")]
pub enum GridEnum {
    Hexagon(HexagonGrid),
    Square(SquareGrid),
    Triangle(TriangleGrid),
}

impl From<HexagonGrid> for GridEnum {
    fn from(grid: HexagonGrid) -> GridEnum {
        GridEnum::Hexagon(grid)
    }
}

impl From<SquareGrid> for GridEnum {
    fn from(grid: SquareGrid) -> GridEnum {
        GridEnum::Square(grid)
    }
}

impl From<TriangleGrid> for GridEnum {
    fn from(grid: TriangleGrid) -> GridEnum {
        GridEnum::Triangle(grid)
    }
}