-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgrid.go
231 lines (202 loc) · 6.16 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package pacman
import (
"image/color"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/text"
"github.com/skatiyar/pacman/assets"
"github.com/skatiyar/pacman/spritetools"
)
const GridViewSize = 1024
var GrayColor = color.RGBA{236, 240, 241, 255.0}
func GridView(
characters *assets.Characters,
powers *assets.Powers,
arcadeFont *truetype.Font,
mazeView func(state gameState, data *Data) (*ebiten.Image, error),
) (func(state gameState, data *Data) (*ebiten.Image, error), error) {
fontface := truetype.NewFace(arcadeFont, &truetype.Options{
Size: 32,
DPI: 72,
Hinting: font.HintingFull,
})
limeAlpha := color.RGBA{250, 233, 8, 200}
dot, dotErr := ebiten.NewImage(8, 8, ebiten.FilterDefault)
if dotErr != nil {
return nil, dotErr
}
if fillErr := dot.Fill(limeAlpha); fillErr != nil {
return nil, fillErr
}
pacman, pacmanErr := spritetools.ScaleSprite(characters.Pacman, 0.5, 0.5)
if pacmanErr != nil {
return nil, pacmanErr
}
ghost1, ghost1Err := spritetools.ScaleSprite(characters.Ghost1, 0.5, 0.5)
if ghost1Err != nil {
return nil, ghost1Err
}
ghost2, ghost2Err := spritetools.ScaleSprite(characters.Ghost2, 0.5, 0.5)
if ghost2Err != nil {
return nil, ghost2Err
}
ghost3, ghost3Err := spritetools.ScaleSprite(characters.Ghost3, 0.5, 0.5)
if ghost3Err != nil {
return nil, ghost3Err
}
ghost4, ghost4Err := spritetools.ScaleSprite(characters.Ghost4, 0.5, 0.5)
if ghost4Err != nil {
return nil, ghost4Err
}
life, lifeErr := spritetools.ScaleSprite(powers.Life, 0.5, 0.5)
if lifeErr != nil {
return nil, lifeErr
}
invinci, invinciErr := spritetools.ScaleSprite(powers.Invincibility, 0.5, 0.5)
if invinciErr != nil {
return nil, invinciErr
}
view, viewErr := ebiten.NewImage(64*Columns, GridViewSize, ebiten.FilterDefault)
if viewErr != nil {
return nil, viewErr
}
return func(state gameState, data *Data) (*ebiten.Image, error) {
if clearErr := view.Clear(); clearErr != nil {
return nil, clearErr
}
if fillErr := view.Fill(color.Black); fillErr != nil {
return nil, fillErr
}
ops := &ebiten.DrawImageOptions{}
switch state {
case GameLoading:
text.Draw(view, "PRESS SPACE", fontface, 320-176, 512-(10+32), color.White)
text.Draw(view, "TO BEGIN", fontface, 320-128, 512+(10), color.White)
case GameStart, GamePause, GameOver:
mazeView, mazeViewErr := mazeView(state, data)
if mazeViewErr != nil {
return nil, mazeViewErr
}
ops.GeoM.Reset()
ops.GeoM.Translate(0,
-(float64(len(data.grid)*CellSize) - (GridViewSize + data.gridOffsetY)))
if drawErr := view.DrawImage(mazeView, ops); drawErr != nil {
return nil, drawErr
}
for i := 0; i < len(data.active); i++ {
for j := 0; j < Columns; j++ {
if !data.active[i][j] {
ops.GeoM.Reset()
ops.GeoM.Translate(
float64((j*CellSize)+30),
-(float64(((i*CellSize)+
(CellSize/2))+2) -
(GridViewSize + data.gridOffsetY)))
if drawErr := view.DrawImage(dot, ops); drawErr != nil {
return nil, drawErr
}
}
}
}
for i := 0; i < len(data.powers); i++ {
power := data.powers[i]
powerImg := life
if power.kind == Invincibility {
powerImg = invinci
}
pwidth, pheight := powerImg.Size()
ops.GeoM.Reset()
ops.GeoM.Translate(
float64((data.powers[i].cellX*CellSize)+pwidth/2),
-(float64(((data.powers[i].cellY*CellSize)+
(CellSize/2))+pheight/2) - (GridViewSize + data.gridOffsetY)))
if drawErr := view.DrawImage(powerImg, ops); drawErr != nil {
return nil, drawErr
}
}
ops.GeoM.Reset()
pwidth, pheight := pacman.Size()
switch data.pacman.direction {
case North:
ops.GeoM.Rotate(-1.5708)
ops.GeoM.Translate(
data.pacman.posX-float64(pwidth/2),
GridViewSize-(data.pacman.posY-float64(pheight-(pheight/2))))
case East:
ops.GeoM.Translate(
data.pacman.posX-float64(pwidth/2),
GridViewSize-(data.pacman.posY+float64(pheight/2)))
case South:
ops.GeoM.Rotate(1.5708)
ops.GeoM.Translate(
data.pacman.posX+float64(pwidth/2),
GridViewSize-(data.pacman.posY+float64(pheight/2)))
case West:
ops.GeoM.Rotate(3.14159)
ops.GeoM.Translate(
data.pacman.posX+float64(pwidth/2),
GridViewSize-(data.pacman.posY-float64(pheight-(pheight/2))))
}
if drawErr := view.DrawImage(pacman, ops); drawErr != nil {
return nil, drawErr
}
for i := 0; i < len(data.ghosts); i++ {
ghost := data.ghosts[i]
ghostImg := ghost1
switch ghost.kind {
case Ghost2:
ghostImg = ghost2
case Ghost3:
ghostImg = ghost3
case Ghost4:
ghostImg = ghost4
}
gwidth, gheight := ghostImg.Size()
ops.GeoM.Reset()
if data.invincible {
ops.ColorM.ChangeHSV(0, 0, 1)
}
ops.GeoM.Translate(
data.ghosts[i].posX-float64(gwidth/2),
(GridViewSize+data.gridOffsetY)-
(data.ghosts[i].posY+float64(gheight-(gheight/2))))
if drawErr := view.DrawImage(ghostImg, ops); drawErr != nil {
return nil, drawErr
}
}
if state == GamePause {
back, backErr := ebiten.NewImage(389, 130, ebiten.FilterDefault)
if backErr != nil {
return nil, backErr
}
if fillErr := back.Fill(color.Black); fillErr != nil {
return nil, fillErr
}
text.Draw(back, "GAME PAUSED", fontface, 24, 65-(10), color.White)
text.Draw(back, "PRESS SPACE", fontface, 24, 65+(10+31), color.White)
ops.GeoM.Reset()
ops.GeoM.Translate(320-(389/2), 512-(130/2))
if drawErr := view.DrawImage(back, ops); drawErr != nil {
return nil, drawErr
}
} else if state == GameOver {
back, backErr := ebiten.NewImage(389, 130, ebiten.FilterDefault)
if backErr != nil {
return nil, backErr
}
if fillErr := back.Fill(color.Black); fillErr != nil {
return nil, fillErr
}
text.Draw(back, "GAME OVER", fontface, 56, 65-(10), color.White)
text.Draw(back, "PRESS SPACE", fontface, 24, 65+(10+31), color.White)
ops.GeoM.Reset()
ops.GeoM.Translate(320-(389/2), 512-(130/2))
if drawErr := view.DrawImage(back, ops); drawErr != nil {
return nil, drawErr
}
}
}
return view, nil
}, nil
}