-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
285 lines (222 loc) · 7.56 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
package main
import (
"fmt"
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
)
type Game struct {
// デバッグのための変数
clickedPositionX, clickedPositionY int
clickedObject string
// ゲーム全編通して使うハンドラ
clickHandler *OnClickHandler
drawHandler *DrawHandler
updateHandler *UpdateHandler
// 以下はメインのゲームシーンで使う変数
// TODO: シーンごとに Game 構造体を分けるべきかもしれない
phase Phase
house *house
// 建物のリスト
buildings []Building
// 敵のリスト
enemies []Enemy
// 情報パネル
infoPanel *infoPanel
// 建築対象としていったん保持されているオブジェクト
buildCandidate Building
// panes
attackPane *attackPane
buildPane *buildPane
// ウェーブのコントローラ
waveCtrl *waveController
// 最初のウェーブが始まる前に表示するインストラクション
buildInstruction *instruction
// 攻撃のインストラクション
attackInstruction *instruction
credit int
}
type Phase int
const (
screenWidth = 1280
screenHeight = 960
)
const (
// 画面上にデバッグ情報を表示するかどうか
debugEnabled = false
)
const (
// 建築フェーズ
PhaseBuilding Phase = iota
// ウェーブフェーズ
PhaseWave
)
// コスト一覧
const (
CostBarricadeBuild = 50
CostTowerBuild = 150
CostRadioTowerBuild = 250
)
const (
// infoPanel の高さを計算
// infoPanel の高さの分だけ、ゲーム画面の中央座標が上にずれる
// 中央座標計算のためにあらかじめここで計算しておく
infoPanelHeight = screenHeight / 7
eScreenHeight = screenHeight - infoPanelHeight - 10
)
func (g *Game) Update() error {
// getClickPosition の戻り値を clickHandler.HandleClick に渡す
// これをやると登録された Clickable の OnClick が呼ばれる
if x, y, clicked := getClickedPosition(); clicked {
g.clickHandler.HandleClick(x, y)
}
// Updater を実行
g.updateHandler.HandleUpdate()
x, y, clicked := getClickedPosition()
if clicked {
g.clickedPositionX = x
g.clickedPositionY = y
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
g.drawHandler.HandleDraw(screen)
// 以下はデバッグ情報
if debugEnabled {
// 現在のフェーズを表示
switch g.phase {
case PhaseBuilding:
ebitenutil.DebugPrintAt(screen, "Phase: Building", 0, 40)
case PhaseWave:
ebitenutil.DebugPrintAt(screen, "Phase: Wave", 0, 40)
}
// クリックされた位置を表示
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Clicked Position: (%d, %d)", g.clickedPositionX, g.clickedPositionY), 0, 0)
// クリックされたオブジェクトを表示
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Clicked Object: %s", g.clickedObject), 0, 20)
// drawHandler の長さを表示
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("DrawHandler: %d", len(g.drawHandler.drawable)), 0, 60)
// clickHandler の長さを表示
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("ClickHandler: %d", len(g.clickHandler.clickableObjects)), 0, 80)
// updateHandler の長さを表示
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("UpdateHandler: %d", len(g.updateHandler.updaters)), 0, 100)
// 画面中央に点を表示 (debug)
vector.DrawFilledRect(screen, screenWidth/2, eScreenHeight/2, 1, 1, color.RGBA{255, 255, 255, 255}, true)
// 残りの敵の数を表示
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Enemies: %d", len(g.enemies)), 0, 120)
// FPS を表示
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("FPS: %0.2f", ebiten.ActualFPS()), 0, 140)
// active な audio の数を表示
var activeAudioNum int
for _, aplayer := range getAudioPlayer().players {
if aplayer.IsPlaying() {
activeAudioNum++
}
}
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Active Audio: %d", activeAudioNum), 0, 160)
// 画面右上にクレジットを表示
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Credit: %d", g.credit), screenWidth-100, 0)
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func (g *Game) SetBuildingPhase() {
g.phase = PhaseBuilding
// 情報パネルをいったんクリアする
g.infoPanel.unit = nil
g.infoPanel.ClearButtons()
// wave phase で追加したものを削除
g.attackPane.RemoveAll()
// Build phase に必要なものを追加
g.buildPane = newBuildPane(g)
g.clickHandler.Add(g.buildPane)
g.drawHandler.Add(g.buildPane)
}
func (g *Game) SetWavePhase() {
g.phase = PhaseWave
// 情報パネルをクリアする (Build のメニューなどが出ていたら消すため)
g.infoPanel.unit = nil
g.infoPanel.ClearButtons()
// building phase で追加したものを削除
g.buildPane.RemoveAll()
// 建築するつもりで持っているものも手放してもらう
// これをやっとかないと次の建築フェーズで開幕から建築物を持っている状態になってしまう
if g.buildCandidate != nil {
g.drawHandler.Remove(g.buildCandidate)
g.buildCandidate = nil
}
// Wave phase に必要なものを追加
g.attackPane = newAttackPane(g)
g.clickHandler.Add(g.attackPane)
g.updateHandler.Add(g.waveCtrl)
}
func (g *Game) initialize() {
// とりあえずいきなりゲームが始まるとする。
// TODO: まずタイトルバックを表示して、その後にゲーム画面に遷移するようにする
aplayer := getAudioPlayer()
aplayer.playBGM()
g.house = newHouse(g)
g.updateHandler.Add(g.house)
g.drawHandler.Add(g.house)
g.AddBuilding(g.house)
g.clickHandler.Add(g.house)
g.infoPanel = newInfoPanel(g, screenWidth-20, infoPanelHeight)
g.drawHandler.Add(g.infoPanel)
// 背景担当
bg := newBackground(g)
g.drawHandler.Add(bg)
// クレジットを初期化
g.credit = 100
// 敵が全滅したらウェーブを終了して建築フェーズに戻る
// 敵が全滅したことをコールバックする
waveEndFn := func() {
// 最初のウェーブが終了したら攻撃インストラクションを消す
if g.attackInstruction != nil {
g.drawHandler.Remove(g.attackInstruction)
g.attackInstruction = nil
}
// 建築 instruction を出す
g.buildInstruction = newInstruction(g, "CLICK ME TO OPEN BUILD MENU", screenWidth/2-80, eScreenHeight/2+50)
g.drawHandler.Add(g.buildInstruction)
// TODO: ウェーブが終わっておめでとう的なことを少しの間だけ表示する
// その後に建築フェーズに移行する
g.SetBuildingPhase()
// ウェーブ終了時に一定のクレジットを得る
g.credit += 120
}
// インストラクションを表示
// 家がクリックされたら消える
g.buildInstruction = newInstruction(g, "CLICK ME TO OPEN BUILD MENU", screenWidth/2-80, eScreenHeight/2+50)
g.drawHandler.Add(g.buildInstruction)
g.waveCtrl = newWaveController(g, waveEndFn)
}
func (g *Game) Reset() {
g.clickHandler.Clear()
g.drawHandler.Clear()
g.updateHandler.Clear()
g.buildings = []Building{}
g.enemies = []Enemy{}
g.initialize()
g.SetBuildingPhase()
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("House Defence Operation")
g := &Game{
clickHandler: &OnClickHandler{},
drawHandler: &DrawHandler{},
updateHandler: &UpdateHandler{},
}
g.initialize()
// 最初のシーンをセットアップする
g.SetBuildingPhase()
title := newTitle(g)
g.drawHandler.Add(title)
g.clickHandler.Add(title)
if err := ebiten.RunGame(g); err != nil {
log.Fatal(err)
}
}