-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
67 lines (55 loc) · 1.31 KB
/
grid.go
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
package sirpent
import (
"encoding/json"
"errors"
)
type Vector [3]int
func (v Vector) Eq(v2 Vector) bool {
if len(v) != len(v2) {
return false
}
for i := range v {
if i >= len(v2) || v[i] != v2[i] {
return false
}
}
return true
}
type GridKind int
const (
hex_grid_hexagonal GridKind = iota
)
var gridKingHandlers = map[GridKind]func() Grid{
hex_grid_hexagonal: func() Grid { return &HexGridHexagonal{} },
}
type Grid interface {
// Error allows for grids with an unbounded number of cells.
Cells() ([]Vector, error)
CryptoRandomCell() (Vector, error)
Directions() []Direction
// Error if direction invalid. Makes up for being unable to typecheck grid-specific directions.
ValidateDirection(d Direction) error
CellNeighbour(v Vector, d Direction) Vector
CellNeighbours(v Vector) []Vector
IsCellWithinBounds(v Vector) bool
DistanceBetweenCells(v1, v2 Vector) int
}
func ParseGridJSON(b []byte) (Grid, error) {
g_for_json := struct {
GridType string `json:"grid_type"`
Rings int `json:"rings"`
}{}
err := json.Unmarshal(b, &g_for_json)
if err != nil {
return nil, err
}
var grid Grid
switch g_for_json.GridType {
case "hex_grid_hexagonal":
grid = &HexGridHexagonal{Rings: g_for_json.Rings}
}
if grid == nil {
return nil, errors.New("Unknown Grid Type.")
}
return grid, nil
}