-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.go
186 lines (162 loc) · 3.81 KB
/
update.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
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
func (g *Game) Update() error {
if g.splashScreenCountdown > 0 {
g.splashScreenCountdown -= 1
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
g.splashScreenCountdown = 0
}
} else if g.menu {
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) && menuSelected < len(flags)-1 {
menuSelected += 1
menuImage = generateMenuImage()
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) && menuSelected > 0 {
menuSelected -= 1
menuImage = generateMenuImage()
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowRight) && !flags[menuSelected].completed {
g.menu = false
g.level = 0
level = flags[menuSelected].levelGenerator(g.level)
initializeLevel()
}
return nil
} else if handleKeyPress(g) {
updatePieceLocations()
checkForPiecesOnTarget()
checkForLevelComplete(g)
}
return nil
}
func handleKeyPress(g *Game) bool {
redraw := false
var keypress ebiten.Key
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
popState(1)
return true
}
if inpututil.IsKeyJustPressed(ebiten.KeySlash) {
g.splashScreenImage = helpSplash
g.splashScreenCountdown = 1200
}
if level.moves == 0 {
return false
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowRight) {
for _, p := range level.pieces {
if p.magnetic {
movePiece(p, 1, 0)
keypress = ebiten.KeyArrowRight
redraw = true
}
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowLeft) {
for _, p := range level.pieces {
if p.magnetic {
movePiece(p, -1, 0)
keypress = ebiten.KeyArrowLeft
redraw = true
}
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) {
for _, p := range level.pieces {
if p.magnetic {
movePiece(p, 0, 1)
keypress = ebiten.KeyArrowDown
redraw = true
}
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) {
for _, p := range level.pieces {
if p.magnetic {
movePiece(p, 0, -1)
keypress = ebiten.KeyArrowUp
redraw = true
}
}
}
// TODO: if no pieces have actually moved as a result
// of the keypress, don't count it as a move.
if redraw {
pushState(keypress)
level.moves -= 1
}
return redraw
}
func movePiece(p *Piece, x, y int) bool {
if !p.moveable {
return false
}
if p.x = p.currentX + x; p.x < 0 || p.x >= level.cells {
p.x = p.currentX
return false
}
if p.y = p.currentY + y; p.y < 0 || p.y >= level.cells {
p.y = p.currentY
return false
}
for _, q := range level.pieces {
if p != q {
if !q.tile {
if p.x == q.x && p.y == q.y {
if movePiece(q, x, y) {
return true
} else {
p.x = p.currentX
p.y = p.currentY
return false
}
}
}
}
}
return true
}
func updatePieceLocations() {
for _, p := range level.pieces {
p.currentX = p.x
p.currentY = p.y
p.opts.GeoM.Reset()
p.opts.GeoM.Translate(float64((w-bs)/2+p.currentX*bs/level.cells), float64((h-bs)/2+p.currentY*bs/level.cells))
}
}
func checkForPiecesOnTarget() {
for _, p := range level.pieces {
for _, q := range level.pieces {
if p != q && p.currentX == q.currentX && p.currentY == q.currentY && p.color == q.color {
p.moveable = false
}
}
}
}
func checkForLevelComplete(g *Game) {
for _, p := range level.pieces {
if p.tile { // For now, this means it's a target
for _, q := range level.pieces {
if p != q && p.color == q.color { // it's the matching gem
if p.currentX != q.currentX || p.currentY != q.currentY {
return // At least one target doesn't have the matching gem on it
}
}
}
}
}
g.splashScreenImage = level.flag
g.splashScreenCountdown = 300
g.level += 1
level = flags[menuSelected].levelGenerator(g.level)
if level.final {
g.menu = true
flags[menuSelected].completed = true
menuImage = generateMenuImage()
return
}
initializeLevel()
}