-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare.go
72 lines (65 loc) · 1.78 KB
/
square.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
68
69
70
71
72
package marchingsquares
// Square ...
type Square struct {
topLeft, topRight, bottomRight, bottomLeft *ControlNode
topMid, midRight, bottomMid, midLeft *Node
config int
}
// NewSquare ...
func NewSquare(topLeft, topRight, bottomRight, bottomLeft *ControlNode) *Square {
config := 0
if topLeft.active {
config++
}
if topRight.active {
config += 2
}
if bottomRight.active {
config += 4
}
if bottomLeft.active {
config += 8
}
return &Square{
topLeft: topLeft,
topMid: topLeft.right,
topRight: topRight,
midLeft: bottomLeft.above,
midRight: bottomRight.above,
bottomLeft: bottomLeft,
bottomMid: bottomLeft.right,
bottomRight: bottomRight,
config: config,
}
}
// SquareGrid ...
type SquareGrid struct {
squares [][]*Square
squareSize float32
}
// NewSquareGrid ...
func NewSquareGrid(atlas [][]int, squareSize float32) *SquareGrid {
nodeCountX := len(atlas)
nodeCountY := len(atlas[0])
mapWidth := float32(nodeCountX) * squareSize
mapHeight := float32(nodeCountY) * squareSize
controlNodes := make([][]*ControlNode, nodeCountX)
for x := range controlNodes {
controlNodes[x] = make([]*ControlNode, nodeCountY)
for y := range controlNodes[x] {
pos := Vector2{
-mapWidth/2 + float32(x)*squareSize + squareSize/2,
-mapHeight/2 + float32(y)*squareSize + squareSize/2,
}
controlNodes[x][y] = NewControlNode(pos, atlas[x][y] == 1, squareSize)
}
}
squares := make([][]*Square, nodeCountX-1)
for x := range squares {
squares[x] = make([]*Square, nodeCountY-1)
for y := range squares[x] {
squares[x][y] = NewSquare(controlNodes[x][y], controlNodes[x+1][y], controlNodes[x+1][y+1], controlNodes[x][y+1])
}
}
return &SquareGrid{squares: squares, squareSize: squareSize}
}