-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
433 lines (378 loc) · 13.4 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package main
import (
"bytes"
"embed"
"fmt"
"image"
"image/color"
_ "image/png"
"log"
"math/rand"
"pathfinding/pair"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/pkg/browser"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
)
// WINDOW CONSTANTS
const (
SCREEN_WIDTH = 1400
SCREEN_HEIGHT = 640
)
// TOOL STATUS
var (
activeTool Tool
drawing bool
)
// UI ELEMENTS
var (
canvasA, canvasB Canvas
buttonPencil, buttonEraser, buttonFlagStart, buttonFlagEnd Button
buttonClearPath, buttonClearCanvas Button
buttonGenerateTerrain Button
buttonTerrainSizeS, buttonTerrainSizeM, buttonTerrainSizeL Button
buttonPlay Button
buttonMsMinus, buttonMsPlus Button
buttonGithub Button
categoryTools, categoryClear, categoryTerrainSize, categoryCooldown string
)
// OTHERS
var (
canvasSize int
cellSize int
iconGithub *ebiten.Image
mononokiFFace, mononokiFFaceSmall font.Face
stopSignal chan struct{}
iterationCooldownMS int
)
// MOUSE TOOLS
type Tool string
const (
PENCIL Tool = "PENCIL"
ERASER Tool = "ERASER"
FLAG_START Tool = "FLAG_START"
FLAG_END Tool = "FLAG_END"
)
// CANVAS SIZES
const (
SIZE_S int = 22
SIZE_M int = 55
SIZE_L int = 110
)
type Game struct {
sc *ebiten.Image
}
func (g *Game) Update() error {
// GET MOUSE POSITION
posX, posY := ebiten.CursorPosition()
// BUTTON HOVER STATES
buttonPencil.hover(posX, posY)
buttonEraser.hover(posX, posY)
buttonFlagStart.hover(posX, posY)
buttonFlagEnd.hover(posX, posY)
buttonClearPath.hover(posX, posY)
buttonClearCanvas.hover(posX, posY)
buttonGenerateTerrain.hover(posX, posY)
buttonTerrainSizeS.hover(posX, posY)
buttonTerrainSizeM.hover(posX, posY)
buttonTerrainSizeL.hover(posX, posY)
buttonPlay.hover(posX, posY)
buttonMsMinus.hover(posX, posY)
buttonMsPlus.hover(posX, posY)
buttonGithub.hover(posX, posY)
// BUTTON SELECTION STATES
switch canvasSize {
case SIZE_S:
buttonTerrainSizeS.active = true
buttonTerrainSizeM.active = false
buttonTerrainSizeL.active = false
case SIZE_M:
buttonTerrainSizeS.active = false
buttonTerrainSizeM.active = true
buttonTerrainSizeL.active = false
case SIZE_L:
buttonTerrainSizeS.active = false
buttonTerrainSizeM.active = false
buttonTerrainSizeL.active = true
}
if canvasA.grid.Status == STATUS_PATHING || canvasB.grid.Status == STATUS_PATHING {
buttonPlay.active = true
buttonPlay.title = "Stop"
buttonPencil.disabled = true
buttonEraser.disabled = true
buttonFlagStart.disabled = true
buttonFlagEnd.disabled = true
buttonClearPath.disabled = true
buttonClearCanvas.disabled = true
buttonGenerateTerrain.disabled = true
buttonTerrainSizeS.disabled = true
buttonTerrainSizeM.disabled = true
buttonTerrainSizeL.disabled = true
} else if canvasA.grid.Status != STATUS_PATHING && canvasB.grid.Status != STATUS_PATHING {
buttonPlay.active = false
buttonPlay.title = "Play"
buttonPencil.disabled = false
buttonEraser.disabled = false
buttonFlagStart.disabled = false
buttonFlagEnd.disabled = false
buttonClearPath.disabled = false
buttonClearCanvas.disabled = false
buttonGenerateTerrain.disabled = false
buttonTerrainSizeS.disabled = false
buttonTerrainSizeM.disabled = false
buttonTerrainSizeL.disabled = false
}
// BUTTON CLICKS
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
if buttonPencil.hovered {
activeTool = PENCIL
buttonPencil.active = true
buttonEraser.active = false
buttonFlagStart.active = false
buttonFlagEnd.active = false
} else if buttonEraser.hovered {
activeTool = ERASER
buttonPencil.active = false
buttonEraser.active = true
buttonFlagStart.active = false
buttonFlagEnd.active = false
} else if buttonFlagStart.hovered {
activeTool = FLAG_START
buttonPencil.active = false
buttonEraser.active = false
buttonFlagStart.active = true
buttonFlagEnd.active = false
} else if buttonFlagEnd.hovered {
activeTool = FLAG_END
buttonPencil.active = false
buttonEraser.active = false
buttonFlagStart.active = false
buttonFlagEnd.active = true
} else if buttonClearPath.hovered {
if canvasA.grid.Status != STATUS_PATHING && canvasB.grid.Status != STATUS_PATHING {
canvasA.grid.Restart(true)
canvasB.grid.Restart(true)
}
} else if buttonClearCanvas.hovered {
if canvasA.grid.Status != STATUS_PATHING && canvasB.grid.Status != STATUS_PATHING {
canvasA.grid.Restart(false)
canvasB.grid.Restart(false)
}
} else if buttonGenerateTerrain.hovered {
if canvasA.grid.Status != STATUS_PATHING && canvasB.grid.Status != STATUS_PATHING {
canvasA.grid.Restart(false)
canvasB.grid.Restart(false)
for i, row := range canvasA.grid.Cells {
for j := range row {
nodeA := &canvasA.grid.Cells[i][j]
nodeB := &canvasB.grid.Cells[i][j]
if !canvasA.grid.Start.Coord.Eq(pair.New(i, j)) && !canvasA.grid.End.Coord.Eq(pair.New(i, j)) {
isWall := rand.Intn(100) < 20
nodeA.IsWall = isWall
nodeB.IsWall = isWall
}
}
}
}
} else if buttonTerrainSizeS.hovered {
canvasA.SetGrid(NewGrid(SIZE_S, pair.New(SIZE_S-1, 0), pair.New(0, SIZE_S-1)))
canvasB.SetGrid(NewGrid(SIZE_S, pair.New(SIZE_S-1, 0), pair.New(0, SIZE_S-1)))
} else if buttonTerrainSizeM.hovered {
canvasA.SetGrid(NewGrid(SIZE_M, pair.New(SIZE_M-1, 0), pair.New(0, SIZE_M-1)))
canvasB.SetGrid(NewGrid(SIZE_M, pair.New(SIZE_M-1, 0), pair.New(0, SIZE_M-1)))
} else if buttonTerrainSizeL.hovered {
canvasA.SetGrid(NewGrid(SIZE_L, pair.New(SIZE_L-1, 0), pair.New(0, SIZE_L-1)))
canvasB.SetGrid(NewGrid(SIZE_L, pair.New(SIZE_L-1, 0), pair.New(0, SIZE_L-1)))
} else if buttonPlay.hovered {
if canvasA.grid.Status != STATUS_PATHING && canvasB.grid.Status != STATUS_PATHING {
canvasA.grid.Restart(true)
canvasB.grid.Restart(true)
go canvasA.grid.DoDijkstra()
go canvasB.grid.DoAStar()
stopSignal = make(chan struct{})
} else {
func() {
close(stopSignal)
for canvasA.grid.Status == STATUS_PATHING || canvasB.grid.Status == STATUS_PATHING {
// Wait until both algorithms have stopped to prevent closing the closed channel.
// Could happen in high cooldown setting and/or when pressing the stop button multiple times.
}
}()
}
} else if buttonMsMinus.hovered {
if iterationCooldownMS <= 10 {
if iterationCooldownMS > 0 {
iterationCooldownMS--
}
} else if iterationCooldownMS <= 20 {
iterationCooldownMS -= 5
} else if iterationCooldownMS > 20 && iterationCooldownMS <= 100 {
iterationCooldownMS -= 10
} else if iterationCooldownMS > 100 {
iterationCooldownMS -= 100
}
} else if buttonMsPlus.hovered {
if iterationCooldownMS < 10 {
iterationCooldownMS++
} else if iterationCooldownMS < 20 {
iterationCooldownMS += 5
} else if iterationCooldownMS >= 20 && iterationCooldownMS < 100 {
iterationCooldownMS += 10
} else if iterationCooldownMS >= 100 && iterationCooldownMS < 1000 {
iterationCooldownMS += 100
}
} else if buttonGithub.hovered {
browser.OpenURL("https://github.com/keelus/pathfinding")
}
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) &&
canvasA.grid.Status != STATUS_PATHING && canvasB.grid.Status != STATUS_PATHING {
if i, j, canvas := mousePosCoords(&canvasA, &canvasB, posX, posY); canvas != nil {
switch activeTool {
case PENCIL, ERASER:
drawing = true
case FLAG_START:
if !canvas.grid.Cells[i][j].IsWall && !canvas.grid.Start.Coord.Eq(pair.New(i, j)) {
canvasA.grid.Start = &canvasA.grid.Cells[i][j]
canvasB.grid.Start = &canvasB.grid.Cells[i][j]
}
case FLAG_END:
if !canvas.grid.Cells[i][j].IsWall && !canvas.grid.Start.Coord.Eq(pair.New(i, j)) {
canvasA.grid.End = &canvasA.grid.Cells[i][j]
canvasB.grid.End = &canvasB.grid.Cells[i][j]
}
}
}
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
if activeTool == PENCIL || activeTool == ERASER {
drawing = false
}
}
if drawing {
if i, j, canvas := mousePosCoords(&canvasA, &canvasB, posX, posY); canvas != nil {
if !canvas.grid.Start.Coord.Eq(pair.New(i, j)) && !canvas.grid.End.Coord.Eq(pair.New(i, j)) {
canvasA.grid.Cells[i][j].IsWall = activeTool == PENCIL
canvasB.grid.Cells[i][j].IsWall = activeTool == PENCIL
}
}
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
if g.sc == nil {
g.sc = screen
}
// BUTTON DRAWING
buttonPencil.Draw(screen)
buttonEraser.Draw(screen)
buttonFlagStart.Draw(screen)
buttonFlagEnd.Draw(screen)
buttonClearPath.Draw(screen)
buttonClearCanvas.Draw(screen)
buttonGenerateTerrain.Draw(screen)
buttonTerrainSizeS.Draw(screen)
buttonTerrainSizeM.Draw(screen)
buttonTerrainSizeL.Draw(screen)
buttonPlay.Draw(screen)
buttonMsMinus.Draw(screen)
buttonMsPlus.Draw(screen)
buttonGithub.Draw(screen)
// LEFT TEXTS DRAWING
textColor := color.RGBA{255, 255, 255, 255}
if canvasA.grid.Status == STATUS_PATHING || canvasB.grid.Status == STATUS_PATHING {
textColor = color.RGBA{0x4b, 0x4b, 0x4b, 255}
}
text.Draw(screen, categoryTools, mononokiFFace, 15, 45, textColor)
text.Draw(screen, categoryClear, mononokiFFace, 15, 240, textColor)
text.Draw(screen, "Canvas size", mononokiFFace, 15, 360, textColor)
text.Draw(screen, categoryCooldown, mononokiFFace, 15, SCREEN_HEIGHT-115, color.White)
text.Draw(screen, fmt.Sprintf("%dms", iterationCooldownMS), mononokiFFace, 80, SCREEN_HEIGHT-85, color.White)
// CANVAS DRAWING
canvasA.Draw(screen)
canvasB.Draw(screen)
iconGithubOp := &ebiten.DrawImageOptions{}
iconGithubOp.GeoM.Translate(SCREEN_WIDTH/2-25, 10)
screen.DrawImage(iconGithub, iconGithubOp)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return SCREEN_WIDTH, SCREEN_HEIGHT
}
//go:embed all:assets/**
var assets embed.FS
func main() {
ebiten.SetWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT)
ebiten.SetWindowTitle("pathfinding - keelus")
ebiten.SetWindowIcon([]image.Image{loadImage("assets/icons/greenFlag.png")})
mononokiFFace = getFont("assets/fonts/mononoki.ttf", 18)
mononokiFFaceSmall = getFont("assets/fonts/mononoki.ttf", 14)
activeTool = PENCIL
iterationCooldownMS = 10
// CREATE CANVAS & SET GRID (default: Medium)
canvasA = NewCanvas(550, 550, 200, 40, "Dijkstra")
canvasB = NewCanvas(550, 550, 800, 40, "A*")
canvasA.SetGrid(NewGrid(SIZE_M, pair.New(SIZE_M-1, 0), pair.New(0, SIZE_M-1)))
canvasB.SetGrid(NewGrid(SIZE_M, pair.New(SIZE_M-1, 0), pair.New(0, SIZE_M-1)))
// LEFT BUTTONS
buttonPencil = NewButton(50, 50, 50, 55, "P", true, getImage("assets/icons/pencil.png"), mononokiFFace)
buttonEraser = NewButton(50, 50, 100, 55, "E", false, getImage("assets/icons/eraser.png"), mononokiFFace)
buttonFlagStart = NewButton(50, 50, 50, 105, "F1", false, getImage("assets/icons/greenFlag.png"), mononokiFFace)
buttonFlagEnd = NewButton(50, 50, 100, 105, "F2", false, getImage("assets/icons/redFlag.png"), mononokiFFace)
buttonClearPath = NewButton(150, 40, 25, 250, "Clear path", false, nil, mononokiFFace)
buttonClearCanvas = NewButton(150, 40, 25, 290, "Clear canvas", false, nil, mononokiFFace)
buttonGenerateTerrain = NewButton(170, 40, 15, 170, "Generate terrain", false, nil, mononokiFFace)
buttonTerrainSizeS = NewButton(40, 40, 25, 370, "S", false, nil, mononokiFFace)
buttonTerrainSizeM = NewButton(40, 40, 80, 370, "M", false, nil, mononokiFFace)
buttonTerrainSizeL = NewButton(40, 40, 135, 370, "L", false, nil, mononokiFFace)
buttonPlay = NewButton(150, 40, 25, 460, "Play", false, nil, mononokiFFace)
buttonMsMinus = NewButton(30, 30, 25, SCREEN_HEIGHT-105, "-", false, nil, mononokiFFace)
buttonMsPlus = NewButton(30, 30, 145, SCREEN_HEIGHT-105, "+", false, nil, mononokiFFace)
// buttonGithub = NewButton(180, 30, (200-150)/2, SCREEN_HEIGHT-35, " /keelus/pathfinding", false, nil, mononokiFFaceSmall)
buttonGithub = NewButton(190, 30, SCREEN_WIDTH/2-30, 5, " /keelus/pathfinding", false, nil, mononokiFFaceSmall)
iconGithub = getImage("assets/icons/github.png")
// LEFT TEXTS
categoryTools = "Tools"
categoryClear = "Clear"
categoryCooldown = "Cooldown"
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}
// getFont returns the font located at fpath, read from embed 'assets'. Path should start with 'assets/...'.
func getFont(fpath string, size float64) font.Face {
fontData, err := assets.ReadFile("assets/fonts/mononoki.ttf")
if err != nil {
log.Fatalf("Error opening the font.")
}
tt, err := opentype.Parse(fontData)
if err != nil {
log.Fatal(err)
}
fontType, err := opentype.NewFace(tt, &opentype.FaceOptions{
Size: size,
DPI: 72,
Hinting: font.HintingVertical,
})
if err != nil {
log.Fatal(err)
}
return fontType
}
// getImage returns the *ebiten.Image located at fpath, read from embed 'assets'. Path should start with 'assets/...'.
func getImage(fpath string) *ebiten.Image {
return ebiten.NewImageFromImage(loadImage(fpath))
}
// getImage returns the *ebiten.Image located at fpath, read from embed 'assets'. Path should start with 'assets/...'.
func loadImage(fpath string) image.Image {
imgBytes, err := assets.ReadFile(fpath)
if err != nil {
log.Fatalf("Error when opening the icon.")
}
img, _, err := image.Decode(bytes.NewReader(imgBytes))
if err != nil {
log.Fatal(err)
}
return img
}