-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
259 lines (214 loc) Β· 6.61 KB
/
main.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"math"
"strconv"
"syscall/js"
"github.com/acifani/vita/lib/game"
)
const (
toggleAction = iota
gliderAction = iota
pulsarAction = iota
)
const (
cellSize = 10
borderSize = 1
)
var (
universe *game.Universe
ctx js.Value
lastTick float64
animationID = -1
clickAction = toggleAction
width, height uint32 = 64, 64
livePopulation = 50
renderingSpeed = 50
)
func main() {
done := make(chan bool)
universe = game.NewUniverse(width, height)
universe.Randomize(livePopulation)
window := js.Global()
document := window.Get("document")
canvas := setupCanvas()
ctx = canvas.Call("getContext", "2d")
gps := document.Call("getElementById", "gps")
ticks := float64(0)
renderingLoops := 0
// Rendering loop
var draw js.Func
draw = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
renderingLoops = renderingLoops + 1
if renderingLoops > (5 - renderingSpeed) {
universe.Tick()
renderingLoops = 0
ticks = ticks + 1
if ticks >= 20 {
now := window.Get("performance").Call("now").Float()
elapsedMs := now - lastTick
lastTick = now
// Number of frames divided by the seconds that have passed
generationsPerSecond := ticks / (elapsedMs / 1000)
gps.Set("innerText", int(generationsPerSecond))
ticks = 0
}
}
drawCanvas()
animationID = window.Call("requestAnimationFrame", draw).Int()
return nil
})
defer draw.Release()
// Add event listeners
addEventListener("live-population", "change", func(this js.Value, args []js.Value) interface{} {
newValue := args[0].Get("target").Get("value").String()
livePopulation, _ = strconv.Atoi(newValue)
universe.Randomize(livePopulation)
return nil
})
addEventListener("rendering-speed", "change", func(this js.Value, args []js.Value) interface{} {
newValue := args[0].Get("target").Get("value").String()
renderingSpeed, _ = strconv.Atoi(newValue)
drawCanvas()
return nil
})
addEventListener("canvas", "click", func(this js.Value, args []js.Value) interface{} {
boundingRect := canvas.Call("getBoundingClientRect")
widthScale := canvas.Get("width").Int() / boundingRect.Get("width").Int()
heightScale := canvas.Get("height").Int() / boundingRect.Get("height").Int()
canvasX := (args[0].Get("clientX").Int() - boundingRect.Get("left").Int()) * widthScale
canvasY := (args[0].Get("clientY").Int() - boundingRect.Get("top").Int()) * heightScale
row := uint32(math.Floor(float64(canvasY) / (cellSize + borderSize)))
col := uint32(math.Floor(float64(canvasX) / (cellSize + borderSize)))
switch clickAction {
case gliderAction:
figure := game.Glider()
universe.SetRectangle(row-figure.DeltaX(), col-figure.DeltaY(), figure.Values())
case pulsarAction:
figure := game.Pulsar()
universe.SetRectangle(row-figure.DeltaX(), col-figure.DeltaY(), figure.Values())
default:
universe.ToggleCellAt(row, col)
}
drawCanvas()
return nil
})
addEventListener("toggle", "click", func(this js.Value, args []js.Value) interface{} {
clickAction = toggleAction
return nil
})
addEventListener("glider", "click", func(this js.Value, args []js.Value) interface{} {
clickAction = gliderAction
return nil
})
addEventListener("pulsar", "click", func(this js.Value, args []js.Value) interface{} {
clickAction = pulsarAction
return nil
})
addEventListener("conway", "click", func(this js.Value, args []js.Value) interface{} {
universe.Rules = universe.ConwayRules
return nil
})
addEventListener("seeds", "click", func(this js.Value, args []js.Value) interface{} {
universe.Rules = universe.SeedsRules
return nil
})
addEventListener("daynight", "click", func(this js.Value, args []js.Value) interface{} {
universe.Rules = universe.DayAndNightRules
return nil
})
playPauseButton := document.Call("getElementById", "play-pause")
addEventListener("play-pause", "click", func(this js.Value, args []js.Value) interface{} {
if animationID != -1 {
playPauseButton.Set("textContent", "Play")
window.Call("cancelAnimationFrame", animationID)
animationID = -1
} else {
playPauseButton.Set("textContent", "Pause")
window.Call("requestAnimationFrame", draw)
}
return nil
})
addEventListener("reset", "click", func(this js.Value, args []js.Value) interface{} {
universe.Reset()
drawCanvas()
return nil
})
addEventListener("randomize", "click", func(this js.Value, args []js.Value) interface{} {
universe.Randomize(livePopulation)
drawCanvas()
return nil
})
// Start rendering
lastTick = window.Get("performance").Call("now").Float()
window.Call("requestAnimationFrame", draw)
// Block and wait for event listeners
<-done
}
func setupCanvas() js.Value {
document := js.Global().Get("document")
canvas := document.Call("getElementById", "canvas")
canvas.Set("height", (cellSize+borderSize)*universe.Height()+borderSize)
canvas.Set("width", (cellSize+borderSize)*universe.Width()+borderSize)
return canvas
}
func drawCanvas() {
drawGrid()
drawCells()
}
func drawGrid() {
height := int(universe.Height())
width := int(universe.Width())
ctx.Call("beginPath")
ctx.Set("strokeStyle", "#e0e1e4")
for i := 0; i <= width; i++ {
ctx.Call("moveTo", i*(cellSize+borderSize)+borderSize, 0)
ctx.Call("lineTo", i*(cellSize+borderSize)+borderSize, (cellSize+borderSize)*height+borderSize)
}
for j := 0; j <= height; j++ {
ctx.Call("moveTo", 0, j*(cellSize+borderSize)+borderSize)
ctx.Call("lineTo", (cellSize+borderSize)*width+borderSize, j*(cellSize+borderSize)+borderSize)
}
ctx.Call("stroke")
}
func drawCells() {
height := int(universe.Height())
width := int(universe.Width())
ctx.Call("beginPath")
// Live cells
ctx.Set("fillStyle", "#3c4257")
for row := 0; row < height; row++ {
for col := 0; col < width; col++ {
idx := universe.GetIndex(uint32(row), uint32(col))
if universe.Cell(idx) == game.Alive {
ctx.Call("fillRect",
col*(cellSize+borderSize)+borderSize,
row*(cellSize+borderSize)+borderSize,
cellSize,
cellSize,
)
}
}
}
// Dead cells
ctx.Set("fillStyle", "#fff")
for row := 0; row < height; row++ {
for col := 0; col < width; col++ {
idx := universe.GetIndex(uint32(row), uint32(col))
if universe.Cell(idx) == game.Dead {
ctx.Call("fillRect",
col*(cellSize+borderSize)+borderSize,
row*(cellSize+borderSize)+borderSize,
cellSize,
cellSize,
)
}
}
}
ctx.Call("stroke")
}
func addEventListener(elementID string, eventName string, callback func(this js.Value, args []js.Value) interface{}) {
js.Global().
Get("document").
Call("getElementById", elementID).
Call("addEventListener", eventName, js.FuncOf(callback))
}