-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
216 lines (181 loc) · 5.03 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"math"
"github.com/go-gl/gl/v2.1/gl"
)
const (
// gridMargin controls the distance between grid points.
gridMargin = 30
// gridDestructionForce controls the amount of ripple created from entity
// destruction.
gridDestructionForce = 50
// gridMoveForce controls the amount of ripple created from entity
// movement.
gridMoveForce = 10
)
// grid provides a visual grid with water rippling effects.
type grid struct {
uidGenerator
points [][]gridPoint
}
func newGrid(width, height float64) *grid {
g := &grid{}
// Make a new 2D array of points
g.points = make([][]gridPoint, int(math.Ceil(width/gridMargin)))
for i := range g.points {
g.points[i] = make([]gridPoint, int(math.Ceil(height/gridMargin)))
}
// Place all points
for x := range g.points {
for y := range g.points[x] {
g.points[x][y] = newGridPoint(vertex{
(-width / 2.0) + float64(x)*gridMargin,
(-height / 2.0) + float64(y)*gridMargin,
0.0})
}
}
return g
}
func (g *grid) tick() []entity {
// Update every point
for x := range g.points {
for y := range g.points[x] {
g.points[x][y].tick()
}
}
return []entity{}
}
func (g *grid) draw() {
gl.PushMatrix()
// Give the grid some generic off white material
matAmbient := []float32{0.6, 0.6, 0.6, 1.0}
matDiffuse := []float32{0.6, 0.6, 0.6, 1.0}
matSpecular := []float32{0.0, 0.0, 0.0, 1.0}
gl.Materialfv(gl.FRONT, gl.AMBIENT, fPtr(matAmbient))
gl.Materialfv(gl.FRONT, gl.DIFFUSE, fPtr(matDiffuse))
gl.Materialfv(gl.FRONT, gl.SPECULAR, fPtr(matSpecular))
gl.Materialf(gl.FRONT, gl.SHININESS, 0)
// Draw lines between every point
gl.Begin(gl.LINES)
for x := 0; x < len(g.points); x++ {
for y := 0; y < len(g.points[x]); y++ {
if x+1 < len(g.points) {
gl.Normal3d(0, 0, 1)
gl.Vertex3d(
g.points[x][y].loc.x,
g.points[x][y].loc.y,
gridPointZVal)
gl.Normal3d(0, 0, 1)
gl.Vertex3d(
g.points[x+1][y].loc.x,
g.points[x+1][y].loc.y,
gridPointZVal)
}
if y+1 < len(g.points[x]) {
gl.Normal3d(0, 0, 1)
gl.Vertex3d(
g.points[x][y].loc.x,
g.points[x][y].loc.y,
gridPointZVal)
gl.Normal3d(0, 0, 1)
gl.Vertex3d(
g.points[x][y+1].loc.x,
g.points[x][y+1].loc.y,
gridPointZVal)
}
}
}
gl.End()
gl.PopMatrix()
}
func (g *grid) entityMove(e physicalEntity, loc vertex) {
// Create a ripple around moving enemies
// TODO(velovix): Refactor this into a method
for x := range g.points {
for y := range g.points[x] {
dist := g.points[x][y].loc.distance(loc)
// TODO(velovix): assign a constant to this 25 value
magnitude := (gridMoveForce / (dist / 25.0)) * e.mass()
if magnitude > gridMoveForce {
magnitude = gridMoveForce
}
direction := normalize(loc.subtract(g.points[x][y].loc))
g.points[x][y].applyForce(vertex{
-direction.x * magnitude,
-direction.y * magnitude,
0})
}
}
}
func (g *grid) entityDestruction(e entity) {
// Create a ripple around destroyed enemies
// TODO(velovix): Refactor this into a method
if pe, ok := e.(physicalEntity); ok {
for x := range g.points {
for y := range g.points[x] {
dist := g.points[x][y].loc.distance(pe.location())
baseForce := gridDestructionForce * pe.mass()
// TODO(velovix): assign a constnant to this 50 value
magnitude := baseForce / (dist / (50.0 * pe.mass()))
if magnitude > gridDestructionForce*pe.mass() {
magnitude = baseForce
}
direction := normalize(pe.location().subtract(g.points[x][y].loc))
g.points[x][y].applyForce(vertex{
-direction.x * magnitude,
-direction.y * magnitude,
0})
}
}
}
}
func (g *grid) location() vertex {
return vertex{0, 0, 0}
}
func (g *grid) collisions() []collision {
return []collision{}
}
func (g *grid) collision(yours, other collision) {
}
func (g *grid) mass() float64 {
return 0
}
func (g *grid) deletable() bool {
return false
}
const (
// gridPointMass is an arbitrary mass value.
gridPointMass = 3
// gridPointSpringConst controls how quickly a point snaps back to the
// direction of its original location.
gridPointSpringConst = 1.0
// gridPointSprintDampening dampens the point's movement so it eventually
// stops.
gridPointSpringDampening = 0.9
// gridPointZVal is the Z value of all grid points.
gridPointZVal = -3
)
// gridPoint is a single point on the grid.
type gridPoint struct {
loc, origLoc, accel vertex
}
func newGridPoint(loc vertex) gridPoint {
return gridPoint{
loc: loc,
origLoc: loc}
}
func (gp *gridPoint) tick() {
// Apply the current acceleration
gp.loc.x += gp.accel.x * mainWindow.delta
gp.loc.y += gp.accel.y * mainWindow.delta
// Affect acceleration using Hooke's law
gp.accel.x += ((gridPointSpringConst * (gp.origLoc.x - gp.loc.x)) / gridPointMass)
gp.accel.y += ((gridPointSpringConst * (gp.origLoc.y - gp.loc.y)) / gridPointMass)
// Dampen the acceleration
gp.accel.x *= (gridPointSpringDampening)
gp.accel.y *= (gridPointSpringDampening)
}
func (gp *gridPoint) applyForce(force vertex) {
gp.accel.x += force.x / gridPointMass
gp.accel.y += force.y / gridPointMass
}