This repository was archived by the owner on Dec 6, 2022. It is now read-only.
forked from webview/webview
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
279 lines (245 loc) · 5.55 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"bytes"
"fmt"
"github.com/phillvancejr/webview"
"log"
"math"
"math/rand"
"text/template"
"time"
)
var (
// webview instance
wv webview.WebView
// game settings
game = struct {
Title string
Width int
Height int
// ball speed
bSpeed float64
// ball speed
pSpeed float64
}{
Title: "Pong",
Width: 500,
Height: 300,
bSpeed: 150.0,
pSpeed: 200.0,
}
// html content
content = `
<body style="margin:0px;overflow:hidden;">
<canvas></canvas>
</body>
<script>
// the {{ .Width }} is using Go's templating format
const width = {{ .Width }}
const height = {{ .Height }}
let started = false
let lastTime
c = document.querySelector('canvas')
c.width = width
c.height = height
ctx = c.getContext('2d')
function clear() {
ctx.fillStyle='black'
ctx.fillRect(0,0,width,height)
}
function rect(x, y, w, h) {
ctx.fillStyle='white'
ctx.fillRect(x,y,w,h)
}
// key handers
window.onkeydown = e => {
e.preventDefault()
// start game on space key
if (e.keyCode == 32 && !started) {
started = true
start()
lastTime = Date.now()
loop()
}
// pass keyCode and pressed bool to Go onKey function
onKey(e.keyCode, true)
}
window.onkeyup = e => {
e.preventDefault()
// pass keyCode and pressed bool to Go onKey function
onKey(e.keyCode, false)
}
// render loop
function loop() {
requestAnimationFrame(loop)
let now = Date.now()
// calculate delta time
let dt = (now - lastTime) / 1000
lastTime = now
// call update with delta time
update(dt)
// call draw function
draw()
}
// draw first frame so we have something before pressing space
resetBall()
draw()
</script>
`
)
const (
// paddle width
pW = 10.0
// paddle height
pH = 50.0
// ball size
bSize = pW
// arrow keys
up = 38
down = 40
)
var (
// entity variables
// player X
pX = pW
// player Y
pY = float64(game.Height/2 - pH/2)
// cpu X
cpuX = float64(game.Width - pW*2)
// cpu Y
cpuY = float64(game.Height/2 - pH/2)
// center ball at beginning
// ball X
bX = 0.0
// ball Y
bY = 0.0
// ball X Direction
bXD = 0.0
// ball Y Direction
bYD = 0.0
// movement variables
mUp = false
mDown = false
)
func main() {
fmt.Println("Click on the Window and press Space to Start\nuse up and down arrow to move")
// seed the random engine
rand.Seed(time.Now().Unix())
// insert game settings into html string using Golang's text/template library
t, e := template.New("").Parse(content)
if e != nil {
log.Fatal(e)
}
// byte buffer to store new content string with game settings inserted
var buf bytes.Buffer
// execute the template
e = t.Execute(&buf, game)
if e != nil {
log.Fatal(e)
}
// assign content to the updated html with the game settings inserted
content = buf.String()
// create webview window
wv = webview.New()
wv.SetSize(game.Width, game.Height, webview.HintFixed)
wv.Center()
wv.NoCtx()
wv.SetTitle(game.Title)
// expose functions via Bind
wv.Bind("start", start)
wv.Bind("resetBall", resetBall)
wv.Bind("update", update)
wv.Bind("draw", draw)
wv.Bind("onKey", onKey)
// navigate to content string
wv.Navigate("data:text/html," + content)
wv.Run()
}
func onKey(key int, pressed bool) {
move := true
if !pressed {
move = false
}
if key == up {
mUp = move
}
if key == down {
mDown = move
}
}
// helper function for keeping things in bounds
func clamp(val, min, max float64) float64 {
return math.Min(math.Max(val, min), max)
}
// put the ball center
func resetBall() {
// center ball
bX = float64(game.Width/2 - bSize/2)
bY = float64(game.Height/2 - bSize/2)
}
// start the game
func start() {
// random dir
dirs := [2]float64{-1.0, 1.0}
bXD = dirs[rand.Intn(2)]
bYD = dirs[rand.Intn(2)]
}
// these functions are exposed to and called from JS
func update(dt float64) {
// update ball
bX += bXD * game.bSpeed * dt
bY += bYD * game.bSpeed * dt
//fmt.Printf("bSpeed: %f bXD: %f bX: %f\n", bSpeed, bXD,bX)
// update cpu
cpuY = bY - pH/2
// update player
if mUp {
pY -= game.pSpeed * dt
}
if mDown {
pY += game.pSpeed * dt
}
// collision and clamping
// ball with wall
ballXMax := float64(game.Width - bSize)
ballYMax := float64(game.Height - bSize)
bX = clamp(bX, 0, ballXMax)
bY = clamp(bY, 0, ballYMax)
// reset the ball if it hits the left or right wall
if bX == 0 || bX == ballXMax {
resetBall()
}
// reverse its direction if it hits a ceiling or floor
if bY == 0 || bY == ballYMax {
bYD *= -1.0
}
// ball with paddle
// player
if bX <= pX+pW && bY >= pY && bY <= pY+pH && bXD == -1 {
bXD *= -1
}
// cpu
if bX+bSize >= cpuX && bY >= cpuY && bY <= cpuY+pH && bXD == 1 {
bXD *= -1
}
// paddle with ceiling/floor
pY = clamp(pY, 0, float64(game.Height-pH))
cpuY = clamp(cpuY, 0, float64(game.Height-pH))
}
func draw() {
// clear screen to black
clear()
// draw ball
rect(bX, bY, bSize, bSize)
// draw player
rect(pX, pY, pW, pH)
// draw cpu
rect(cpuX, cpuY, pW, pH)
}
// these functions wrap JS functions to make it easier to use them from Go
func clear() {
wv.Eval("clear()")
}
func rect(x, y, w, h float64) {
wv.Eval(fmt.Sprintf("rect(%f,%f,%f,%f)", x, y, w, h))
}