-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhouse.go
436 lines (364 loc) · 14.7 KB
/
house.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
434
435
436
package main
import (
"bytes"
"fmt"
"image"
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
_ "embed"
_ "image/png"
)
//go:embed assets/house.png
var houseImageData []byte
type house struct {
game *Game
x, y int // 画面中央に配置するので初期化時に値をもらう必要はない
width, height int // 画像サイズをそのまま使うので初期化時に値をもらう必要はない
zindex int // これも適当に調整するので初期化時に値をもらう必要はない
image *ebiten.Image
health int
// 死亡時のアニメーションを管理するための変数
deadAnimationDuration int
// 画像の拡大率。
// TODO: 本当は画像のサイズそのものを変更したほうが見た目も処理効率も良くなる。余裕があれば後々やろう。
scale float64
// health が 0 になったときに呼ばれる関数
onDestroy func(h *house)
}
func newHouse(game *Game) *house {
img, _, err := image.Decode(bytes.NewReader(houseImageData))
if err != nil {
log.Fatal(err)
}
h := &house{
game: game,
width: img.Bounds().Dx(),
height: img.Bounds().Dy(),
scale: 0.5,
health: 100,
image: ebiten.NewImageFromImage(img),
onDestroy: func(h *house) {
// TODO: 爆発したり消えたりする処理を書く
// ここでフラグを設定しといて、Update() や Draw で続きの処理を行うのもあり
// いったんシンプルに消す
h.game.updateHandler.Remove(h)
h.game.RemoveBuilding(h)
h.game.drawHandler.Remove(h)
},
}
h.x = screenWidth / 2
h.y = eScreenHeight / 2
return h
}
func (h *house) Update() {
if h.health <= 0 {
h.deadAnimationDuration++
if h.deadAnimationDuration >= deadAnimationTotalFrame {
h.onDestroy(h)
}
return
}
}
// 画面中央に配置
func (h *house) Draw(screen *ebiten.Image) {
// 画像を描画
opts := &ebiten.DrawImageOptions{}
if h.health <= 0 {
// 死亡時のアニメーションを行う
// ぺちゃんこになるように縮小する
// TODO: ちょっとアニメーションが怪しいので調整する
scale := h.scale * (1.0 - float64(h.deadAnimationDuration)/deadAnimationTotalFrame)
if scale < 0 {
scale = 0
}
opts.GeoM.Translate(0, h.scale*float64(-h.height)/2)
opts.GeoM.Scale(h.scale, scale)
opts.GeoM.Translate(0, h.scale*float64(h.height)/2)
} else {
opts.GeoM.Scale(h.scale, h.scale)
}
opts.GeoM.Translate(float64(h.x)-float64(h.width)*h.scale/2, float64(h.y)-float64(h.height)*h.scale/2)
screen.DrawImage(h.image, opts)
}
func (h *house) ZIndex() int {
return h.zindex
}
func (h *house) Position() (int, int) {
// 中央の座標を返す
return h.x, h.y
}
func (h *house) SetPosition(x, y int) {
h.x = x
h.y = y
}
func (h *house) Size() (int, int) {
return int(float64(h.width) * h.scale), int(float64(h.height) * h.scale)
}
func (h *house) Name() string {
return "House"
}
func (h *house) Damage(d int) {
if h.health <= 0 {
return
}
h.health -= d
if h.health <= 0 {
h.health = 0
}
}
// house implements Clickable interface
func (h *house) OnClick(x, y int) bool {
// 建築 instruction を消す
if h.game.buildInstruction != nil {
h.game.drawHandler.Remove(h.game.buildInstruction)
h.game.buildInstruction = nil
}
if h.game.buildCandidate != nil {
// 建築予定のものを持っているときには何もしない
return false
}
getAudioPlayer().play(soundChoice)
h.game.clickedObject = "House"
// infoPanel に情報を表示する
h.game.infoPanel.ClearButtons()
icon := newHouseIcon(80, eScreenHeight+70)
h.game.infoPanel.setIcon(icon)
h.game.infoPanel.setUnit(h)
switch h.game.phase {
case PhaseBuilding:
// infoPanel にバリケード建築ボタンを表示
buildBarricadeButton := newButton(h.game,
225, eScreenHeight, infoPanelHeight, infoPanelHeight, 1,
func(x, y int) bool {
if h.game.credit < CostBarricadeBuild {
// お金が足りない場合は建築できない
return false
}
getAudioPlayer().play(soundChoice)
// buildCandidate を持っているときにバリケードボタンを押したときの振る舞い
// 選択肢なおしということ、いったん手放す
if h.game.buildCandidate != nil {
h.game.drawHandler.Remove(h.game.buildCandidate)
}
barricadeOnDestroyFn := func(b *barricade) {
b.game.drawHandler.Remove(b)
b.game.clickHandler.Remove(b)
b.game.RemoveBuilding(b)
b.game.infoPanel.Remove(b)
}
b := newBarricade(h.game, 0, 0, barricadeOnDestroyFn)
h.game.buildCandidate = b
h.game.infoPanel.drawDescriptionFn = func(screen *ebiten.Image, x, y int) {
x = x - 150
y = y + 10
var scale float64 = 2
drawText(screen, "I am Barricade!", x, y-15, scale, scale, color.RGBA{0xff, 0xff, 0xff, 0xff})
drawText(screen, fmt.Sprintf("Cost: $%d", b.Cost()), x, y+15, scale, scale, color.RGBA{0xff, 0xff, 0xff, 0xff})
// 敵の進行を邪魔するという説明を記載する
drawText(screen, "Blocks enemy's advance!", x, y+45, scale, scale, color.RGBA{0xff, 0xff, 0xff, 0xff})
}
return false
},
func(screen *ebiten.Image, x, y, width, height int) {
drawRect(screen, x, y, width, height)
barricadeIcon := newBarricadeIcon(x+width/2, y+height/2-10)
barricadeIcon.Draw(screen)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("BUILD ($%d)", CostBarricadeBuild), x+width/2-30, y+height/2+40)
// 選択中であればボタンをハイライト表示する
if h.game.buildCandidate != nil && h.game.buildCandidate.Name() == "Barricade" {
drawYellowRect(screen, x, y, width, height)
}
// お金が足りないときはボタン全体をグレーアウトする
if h.game.credit < CostBarricadeBuild {
overlay := ebiten.NewImage(width, height)
overlay.Fill(color.RGBA{128, 128, 128, 128})
overlayOpts := &ebiten.DrawImageOptions{}
overlayOpts.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(overlay, overlayOpts)
}
})
h.game.infoPanel.AddButton(buildBarricadeButton)
buildTowerButton := newButton(h.game,
225+infoPanelHeight, eScreenHeight, infoPanelHeight, infoPanelHeight, 1,
func(x, y int) bool {
if h.game.credit < CostTowerBuild {
// お金が足りない場合は建築できない
return false
}
getAudioPlayer().play(soundChoice)
// buildCandidate を持っているときにバリケードボタンを押したときの振る舞い
// 選択肢なおしということ、いったん手放す
if h.game.buildCandidate != nil {
h.game.drawHandler.Remove(h.game.buildCandidate)
}
towerOnDestroyFn := func(b *tower) {
b.game.updateHandler.Remove(b)
b.game.drawHandler.Remove(b)
b.game.clickHandler.Remove(b)
b.game.RemoveBuilding(b)
b.game.infoPanel.Remove(b)
}
t := newTower(h.game, 0, 0, towerOnDestroyFn)
h.game.buildCandidate = t
t.game.infoPanel.drawDescriptionFn = func(screen *ebiten.Image, x, y int) {
x = x - 150
y = y + 10
var scale float64 = 2
// 敵を一匹ずつ攻撃するという説明を記載する
drawText(screen, "I am Beam Tower!", x, y-15, scale, scale, color.RGBA{0xff, 0xff, 0xff, 0xff})
drawText(screen, fmt.Sprintf("Cost: $%d", t.Cost()), x, y+15, scale, scale, color.RGBA{0xff, 0xff, 0xff, 0xff})
drawText(screen, "Attack single bug by laser beam!", x, y+45, scale, scale, color.RGBA{0xff, 0xff, 0xff, 0xff})
}
return false
},
func(screen *ebiten.Image, x, y, width, height int) {
drawRect(screen, x, y, width, height)
towerIcon := newTowerIcon(x+width/2, y+height/2-10)
towerIcon.Draw(screen)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("BUILD ($%d)", CostTowerBuild), x+width/2-30, y+height/2+40)
// 選択中であればボタンをハイライト表示する
if h.game.buildCandidate != nil && h.game.buildCandidate.Name() == "Tower" {
drawYellowRect(screen, x, y, width, height)
}
// お金が足りないときはボタン全体をグレーアウトする
if h.game.credit < CostTowerBuild {
overlay := ebiten.NewImage(width, height)
overlay.Fill(color.RGBA{128, 128, 128, 128})
overlayOpts := &ebiten.DrawImageOptions{}
overlayOpts.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(overlay, overlayOpts)
}
})
h.game.infoPanel.AddButton(buildTowerButton)
buildRadioTowerButton := newButton(h.game,
225+infoPanelHeight*2, eScreenHeight, infoPanelHeight, infoPanelHeight, 1,
func(x, y int) bool {
if h.game.credit < CostRadioTowerBuild {
// お金が足りない場合は建築できない
return false
}
getAudioPlayer().play(soundChoice)
// buildCandidate を持っているときにバリケードボタンを押したときの振る舞い
// 選択肢なおしということ、いったん手放す
if h.game.buildCandidate != nil {
h.game.drawHandler.Remove(h.game.buildCandidate)
}
radioTowerOnDestroyFn := func(b *radioTower) {
b.game.updateHandler.Remove(b)
b.game.drawHandler.Remove(b)
b.game.clickHandler.Remove(b)
b.game.RemoveBuilding(b)
b.game.infoPanel.Remove(b)
}
rt := newRadioTower(h.game, 0, 0, radioTowerOnDestroyFn)
h.game.buildCandidate = rt
h.game.infoPanel.drawDescriptionFn = func(screen *ebiten.Image, x, y int) {
x = x - 150
y = y + 10
var scale float64 = 2
// 範囲攻撃するしレンジも広いが、近くは攻撃できない
drawText(screen, "I am Radio Tower!", x, y-15, scale, scale, color.RGBA{0xff, 0xff, 0xff, 0xff})
drawText(screen, fmt.Sprintf("Cost: $%d", rt.Cost()), x, y+15, scale, scale, color.RGBA{0xff, 0xff, 0xff, 0xff})
drawText(screen, "Attacks in area, cannot hit near bugs!", x, y+45, scale, scale, color.RGBA{0xff, 0xff, 0xff, 0xff})
}
return false
},
func(screen *ebiten.Image, x, y, width, height int) {
drawRect(screen, x, y, width, height)
radioTowerIcon := newRadioTowerIcon(x+width/2, y+height/2-10)
radioTowerIcon.Draw(screen)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("BUILD ($%d)", CostRadioTowerBuild), x+width/2-30, y+height/2+40)
// 選択中であればボタンをハイライト表示する
if h.game.buildCandidate != nil && h.game.buildCandidate.Name() == "RadioTower" {
drawYellowRect(screen, x, y, width, height)
}
// お金が足りないときはボタン全体をグレーアウトする
if h.game.credit < CostRadioTowerBuild {
overlay := ebiten.NewImage(width, height)
overlay.Fill(color.RGBA{128, 128, 128, 128})
overlayOpts := &ebiten.DrawImageOptions{}
overlayOpts.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(overlay, overlayOpts)
}
})
h.game.infoPanel.AddButton(buildRadioTowerButton)
nextWaveStartButton := newButton(h.game,
screenWidth-10-infoPanelHeight, eScreenHeight, infoPanelHeight, infoPanelHeight, 1,
func(x, y int) bool {
getAudioPlayer().play(soundKettei)
switch h.game.phase {
case PhaseBuilding:
// 最初のウェーブだったら攻撃方法に関する説明を表示する
if h.game.waveCtrl.currentBigWave == 0 {
h.game.attackInstruction = newInstruction(h.game, "CLICK BUGS TO ATTACK!", screenWidth/2-60, eScreenHeight/2+50)
h.game.drawHandler.Add(h.game.attackInstruction)
}
h.game.SetWavePhase()
case PhaseWave:
// never reach
fallthrough
default:
log.Fatalf("unexpected phase: %v", h.game.phase)
}
return false
},
func(screen *ebiten.Image, x, y, width, height int) {
drawRect(screen, x, y, width, height)
drawText(screen, "FINISH BUILDING!", x+width/2-45, y+height/2-40, 1, 1, color.RGBA{0xff, 0xff, 0xff, 0xff})
drawText(screen, "START NEXT WAVE!", x+width/2-45, y+height/2-8, 1, 1, color.RGBA{0xff, 0xff, 0xff, 0xff})
// 現在のウェーブとトータルウェーブ数を表示する
drawText(screen, fmt.Sprintf("CURRENT WAVE: %d/%d", h.game.waveCtrl.currentBigWave+1, len(waveList)), x+width/2-52, y+height/2+32, 1, 1, color.RGBA{0xff, 0xff, 0xff, 0xff})
},
)
h.game.infoPanel.AddButton(nextWaveStartButton)
case PhaseWave:
// TODO: implement
default:
log.Fatalf("unexpected phase: %v", h.game.phase)
}
return false
}
func drawYellowRect(screen *ebiten.Image, x, y, width, height int) {
strokeWidth := float32(10)
// 黄色っぽい線で描画
vector.StrokeLine(screen, float32(x), float32(y+5), float32(x+width), float32(y+5), strokeWidth, color.RGBA{0xff, 0xff, 0x00, 0xff}, true)
vector.StrokeLine(screen, float32(x+5), float32(y+5), float32(x+5), float32(y+height-5), strokeWidth, color.RGBA{0xff, 0xff, 0x00, 0xff}, true)
vector.StrokeLine(screen, float32(x+width-5), float32(y+5), float32(x+width-5), float32(y+height-5), strokeWidth, color.RGBA{0xff, 0xff, 0x00, 0xff}, true)
vector.StrokeLine(screen, float32(x), float32(y+height-5), float32(x+width), float32(y+height-5), strokeWidth, color.RGBA{0xff, 0xff, 0x00, 0xff}, true)
}
func (h *house) Health() int {
return h.health
}
// グレーアウトした drawRect を描画
func drawGrayRect(screen *ebiten.Image, x, y, width, height int) {
strokeWidth := float32(2)
vector.StrokeLine(screen, float32(x), float32(y), float32(x+width), float32(y), strokeWidth, color.Gray16{0x8888}, true)
vector.StrokeLine(screen, float32(x), float32(y), float32(x), float32(y+height), strokeWidth, color.Gray16{0x8888}, true)
vector.StrokeLine(screen, float32(x+width), float32(y), float32(x+width), float32(y+height), strokeWidth, color.Gray16{0x8888}, true)
vector.StrokeLine(screen, float32(x), float32(y+height), float32(x+width), float32(y+height), strokeWidth, color.Gray16{0x8888}, true)
}
func drawRect(screen *ebiten.Image, x, y, width, height int) {
strokeWidth := float32(2)
vector.StrokeLine(screen, float32(x), float32(y), float32(x+width), float32(y), strokeWidth, color.White, true)
vector.StrokeLine(screen, float32(x), float32(y), float32(x), float32(y+height), strokeWidth, color.White, true)
vector.StrokeLine(screen, float32(x+width), float32(y), float32(x+width), float32(y+height), strokeWidth, color.White, true)
vector.StrokeLine(screen, float32(x), float32(y+height), float32(x+width), float32(y+height), strokeWidth, color.White, true)
}
func (h *house) IsClicked(x, y int) bool {
width, height := h.Size()
return h.x-width/2 <= x && x <= h.x+width/2 && h.y-height/2 <= y && y <= h.y+height/2
}
func (h *house) SetOverlap(overlap bool) {
// 登場の機会はないので実装しない
}
func (h *house) IsOverlap() bool {
// 登場の機会はないので実装しない
return false
}
func (h *house) Cost() int {
// 登場の機会はないので実装しない
return 0
}