-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
289 lines (267 loc) · 8.8 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
package main
import (
"embed"
"io"
"time"
resource "github.com/quasilyte/ebitengine-resource"
"github.com/quasilyte/ge"
"github.com/quasilyte/ge/input"
"github.com/quasilyte/ge/langs"
_ "image/png"
)
//go:embed assets/*
var gameAssets embed.FS
const (
ActionSectorLeft input.Action = iota
ActionSectorRight
ActionSectorUp
ActionSectorDown
ActionCancel
ActionOpenMenu
ActionConfirm
ActionNextItem
ActionPrevItem
ActionNextCategory
ActionPrevCategory
ActionFortify
ActionLeftClick
ActionExit
)
const (
ImageHullViper resource.ImageID = iota
ImageHullScout
ImageHullHunter
ImageHullFighter
ImageHullScorpion
ImageHullMammoth
ImageTurretBuilder
ImageTurretGatlingGun
ImageTurretLightCannon
ImageTurretDualCannon
ImageTurretHeavyCannon
ImageTurretRailgun
ImageTurretLancer
ImageTurretGauss
ImageTurretIon
ImageBattlePost
ImageAmmoGatlingGun
ImageAmmoLightCannon
ImageAmmoMediumCannon
ImageAmmoDualCannon
ImageAmmoLancer
ImageAmmoGauss
ImageAmmoIon
ImageExplosion
ImageBackgroundTiles
ImageSectorSelector
ImageUnitSelector
ImageGrid
ImageIronResourceIcon
ImageGoldResourceIcon
ImageOilResourceIcon
ImageCombinedResourceIcon
ImageResourceRow
ImagePopupBuildTank
ImageMenuBackground
ImageMenuButton
ImageMenuSelectButton
ImageMenuCheckboxButton
ImageMenuSlideLeft
)
const (
AudioGatlingGun resource.AudioID = iota
AudioLightCannon
AudioDualCannon
AudioHeavyCannon
AudioRailgun
AudioLancer
AudioGauss
AudioIon
AudioCueError
AudioCueSendUnits
AudioCueProductionStarted
AudioCueProductionCompleted
AudioCueConstructionCompleted
AudioCueScreenReset
AudioMusic
)
const (
FontSmall resource.FontID = iota
FontDescription
FontBig
)
const (
RawTilesJSON resource.RawID = iota
RawDictEng
RawDictRus
)
func main() {
ctx := ge.NewContext()
ctx.Rand.SetSeed(time.Now().Unix())
ctx.WindowTitle = "Tanks"
ctx.WindowWidth = 1920
ctx.WindowHeight = 1080
ctx.FullScreen = true
ctx.Loader.OpenAssetFunc = func(path string) io.ReadCloser {
f, err := gameAssets.Open("assets/" + path)
if err != nil {
ctx.OnCriticalError(err)
}
return f
}
state := &gameState{}
// Bind controls.
gamepadKeymap := input.Keymap{
ActionSectorLeft: {input.KeyGamepadLeft},
ActionSectorRight: {input.KeyGamepadRight},
ActionSectorDown: {input.KeyGamepadDown},
ActionSectorUp: {input.KeyGamepadUp},
ActionConfirm: {input.KeyGamepadA},
ActionOpenMenu: {input.KeyGamepadX},
ActionCancel: {input.KeyGamepadB},
ActionPrevItem: {input.KeyGamepadLeft},
ActionNextItem: {input.KeyGamepadRight},
ActionNextCategory: {input.KeyGamepadDown},
ActionPrevCategory: {input.KeyGamepadUp},
ActionFortify: {input.KeyGamepadY},
ActionExit: {input.KeyGamepadStart},
}
keyboardKeymap := input.Keymap{
ActionSectorLeft: {input.KeyA},
ActionSectorRight: {input.KeyD},
ActionSectorDown: {input.KeyS},
ActionSectorUp: {input.KeyW},
ActionConfirm: {input.KeySpace},
ActionOpenMenu: {input.KeyEnter},
ActionCancel: {input.KeyQ},
ActionPrevItem: {input.KeyA},
ActionNextItem: {input.KeyD},
ActionNextCategory: {input.KeyS},
ActionPrevCategory: {input.KeyW},
ActionFortify: {input.KeyE},
ActionExit: {input.KeyEscape},
ActionLeftClick: {input.KeyMouseLeft},
}
state.Player1keyboard = ctx.Input.NewHandler(0, keyboardKeymap)
state.Player1gamepad = ctx.Input.NewHandler(0, gamepadKeymap)
state.Player2gamepad = ctx.Input.NewHandler(1, gamepadKeymap)
state.Player3gamepad = ctx.Input.NewHandler(2, gamepadKeymap)
state.Player4gamepad = ctx.Input.NewHandler(3, gamepadKeymap)
state.MenuInput = ctx.Input.NewMultiHandler()
state.MenuInput.AddHandler(state.Player1gamepad)
state.MenuInput.AddHandler(state.Player1keyboard)
// Associate audio resources.
audioResources := map[resource.AudioID]resource.Audio{
AudioGatlingGun: {Path: "sounds/gatling_gun.wav", Volume: -0.5},
AudioLightCannon: {Path: "sounds/light_cannon.wav", Volume: -0.4},
AudioDualCannon: {Path: "sounds/dual_cannon.wav", Volume: -0.3},
AudioHeavyCannon: {Path: "sounds/heavy_cannon.wav", Volume: -0.75},
AudioRailgun: {Path: "sounds/railgun.wav", Volume: -0.5},
AudioLancer: {Path: "sounds/lancer.wav", Volume: -0.75},
AudioGauss: {Path: "sounds/gauss.wav", Volume: -0.5},
AudioIon: {Path: "sounds/ion.wav", Volume: -0.5},
AudioCueError: {Path: "sounds/error.wav", Volume: -0.45},
AudioCueSendUnits: {Path: "sounds/send_units.wav", Volume: -0.3},
AudioCueProductionStarted: {Path: "sounds/production_started.wav", Volume: -0.1},
AudioCueProductionCompleted: {Path: "sounds/production_completed.wav", Volume: +0.1},
AudioCueConstructionCompleted: {Path: "sounds/construction_completed.wav"},
AudioCueScreenReset: {Path: "sounds/screen_reset.wav"},
AudioMusic: {Path: "sounds/music.ogg"},
}
for id, res := range audioResources {
ctx.Loader.AudioRegistry.Set(id, res)
ctx.Loader.PreloadAudio(id)
}
// Associate image resources.
imageResources := map[resource.ImageID]resource.ImageInfo{
ImageHullViper: {Path: "hull_viper.png"},
ImageHullScout: {Path: "hull_scout.png"},
ImageHullHunter: {Path: "hull_hunter.png"},
ImageHullFighter: {Path: "hull_fighter.png"},
ImageHullScorpion: {Path: "hull_scorpion.png"},
ImageHullMammoth: {Path: "hull_mammoth.png"},
ImageTurretBuilder: {Path: "turret_builder.png"},
ImageTurretGatlingGun: {Path: "turret_gatling_gun.png"},
ImageTurretLightCannon: {Path: "turret_light_cannon.png"},
ImageTurretDualCannon: {Path: "turret_dual_cannon.png"},
ImageTurretHeavyCannon: {Path: "turret_heavy_cannon.png"},
ImageTurretRailgun: {Path: "turret_railgun.png"},
ImageTurretLancer: {Path: "turret_lancer.png"},
ImageTurretGauss: {Path: "turret_gauss.png"},
ImageTurretIon: {Path: "turret_ion.png"},
ImageBattlePost: {Path: "battle_post.png"},
ImageAmmoGatlingGun: {Path: "ammo_gatling_gun.png"},
ImageAmmoLightCannon: {Path: "ammo_light_cannon.png"},
ImageAmmoMediumCannon: {Path: "ammo_medium_cannon.png"},
ImageAmmoDualCannon: {Path: "ammo_dual_cannon.png"},
ImageAmmoLancer: {Path: "ammo_lancer.png"},
ImageAmmoGauss: {Path: "ammo_gauss.png"},
ImageAmmoIon: {Path: "ammo_ion.png"},
ImageExplosion: {Path: "explosion.png"},
ImageBackgroundTiles: {Path: "tiles.png"},
ImageSectorSelector: {Path: "sector_selector.png"},
ImageUnitSelector: {Path: "unit_selector.png"},
ImageGrid: {Path: "grid.png"},
ImageIronResourceIcon: {Path: "resource_iron.png"},
ImageGoldResourceIcon: {Path: "resource_gold.png"},
ImageOilResourceIcon: {Path: "resource_oil.png"},
ImageCombinedResourceIcon: {Path: "resource_combined.png"},
ImageResourceRow: {Path: "resource_row.png"},
ImagePopupBuildTank: {Path: "popup_build_tank.png"},
ImageMenuButton: {Path: "menu_button.png"},
ImageMenuSelectButton: {Path: "menu_select_button.png"},
ImageMenuCheckboxButton: {Path: "menu_checkbox_button.png"},
ImageMenuSlideLeft: {Path: "menu_slide_left.png"},
ImageMenuBackground: {Path: "menu_bg.png"},
}
for id, res := range imageResources {
ctx.Loader.ImageRegistry.Set(id, res)
ctx.Loader.PreloadImage(id)
}
// Associate font resources.
fontResources := map[resource.FontID]resource.Font{
FontSmall: {Path: "DejavuSansMono.ttf", Size: 12},
FontDescription: {Path: "DejavuSansMono.ttf", Size: 14, LineSpacing: 1.15},
FontBig: {Path: "DejavuSansMono.ttf", Size: 20},
}
for id, res := range fontResources {
ctx.Loader.FontRegistry.Set(id, res)
ctx.Loader.PreloadFont(id)
}
// Associate other resources.
rawResources := map[resource.RawID]resource.Raw{
RawTilesJSON: {Path: "tiles.json"},
RawDictEng: {Path: "langs/eng.txt"},
RawDictRus: {Path: "langs/rus.txt"},
}
for id, res := range rawResources {
ctx.Loader.RawRegistry.Set(id, res)
ctx.Loader.PreloadRaw(id)
}
languages := ge.InferLanguages()
preferredDict := RawDictEng
selectedLang := "en"
for _, l := range languages {
if l == "ru" {
preferredDict = RawDictRus
selectedLang = "ru"
break
}
}
dict, err := langs.ParseDictionary(selectedLang, ctx.Loader.LoadRaw(preferredDict))
if err != nil {
panic(err)
}
ctx.Dict = dict
if err := ge.RunGame(ctx, newTutorialController(state)); err != nil {
panic(err)
}
}
type gameState struct {
MenuInput *input.MultiHandler
Player1keyboard *input.Handler
Player1gamepad *input.Handler
Player2gamepad *input.Handler
Player3gamepad *input.Handler
Player4gamepad *input.Handler
}