-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
406 lines (359 loc) · 9.94 KB
/
game.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
// Copyright 2016 Josh Deprez
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package awakengine
import (
"fmt"
"log"
"os"
"github.com/DrJosh9000/vec"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
)
const levelGeomDumpFmt = `// Copyright 2016 Josh Deprez
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package game
import "github.com/DrJosh9000/vec"
var (
precomputedObstacles = %#v
precomputedPaths = %#v
)`
var (
config *Config
game Game
scene *Scene
modelFrame int
displayFrame int
mouseDown bool
lastCursorPos vec.I2
terrain *Terrain
obstacles, paths *vec.Graph
dialogueStack []*DialogueLine
dialogue *DialogueDisplay
player Unit
playerSprite *Sprite
lastPlayerTile vec.I2
globalTriggers []*Trigger
triggersByName map[string]*Trigger
triggersByTile map[vec.I2][]*Trigger
)
type Config struct {
Debug bool
FramesPerUpdate int
LevelGeomDump string
LevelPreview bool
RecordingFile string
RecordingFrames int
}
// Handler handles events.
type Handler interface {
// Handle handles the event e and returns true if the event is "consumed" or
// false if the event "lives on" to be handled elsewhere.
Handle(e *Event) bool
}
// Unit can be given orders.
// Examples of units include the player character, NPCs, etc. Or it
// could be a unit in an RTS.
type Unit interface {
// GoIdle asks the unit to stop whatever it's doing ("at ease").
GoIdle()
// Footprint is the rectangle relative to the sprite position with the ground area of the unit.
Footprint() (ul, dr vec.I2)
// Path is the path the unit is currently following. The current position is
// implied as the first point.
Path() []vec.I2
//Pos() vec.I2
//Sprite() *Sprite
}
// Level describes things needed for a base terrain/level.
type Level struct {
Doodads []*Doodad // sparse terrain objects
MapSize vec.I2
TileMap, BlockMap []uint8
TileInfos, BlockInfos []TileInfo // dense terrain objects
TilesetKey, BlocksetKey string
TileSize, BlockHeight int
// Obstacles and Paths are optional but speed up game start time.
Obstacles, Paths *vec.Graph
}
// Game abstracts the non-engine parts of the game: the story, art, level design, etc.
type Game interface {
// BubbleKey returns the key for the bubble image, and inverse.
BubbleKey() (string, string)
// Font is the general/default typeface to use.
Font() Font
Handler
// Level provides the base level.
Level() (*Level, error)
// Player provides the player unit.
Player() (Unit, *Sprite)
Scene() *Scene
// Triggers provide some dynamic behaviour.
Triggers() []*Trigger
// Viewport is the size of the window and the pixels in the window.
Viewport() (pixelSize int, title string)
}
// load prepares assets for use by the game.
func load(g Game) error {
game = g
scene = game.Scene()
if err := loadAllImages(); err != nil {
return fmt.Errorf("loading images: %v", err)
}
player, playerSprite = game.Player()
trigs := game.Triggers()
triggersByName = make(map[string]*Trigger, len(trigs))
triggersByTile = make(map[vec.I2][]*Trigger, len(trigs))
for i, t := range trigs {
if t.Name == "" {
return fmt.Errorf("trigger %d has no name", i)
}
triggersByName[t.Name] = t
if len(t.Tiles) == 0 {
globalTriggers = append(globalTriggers, t)
continue
}
for _, p := range t.Tiles {
triggersByTile[p] = append(triggersByTile[p], t)
}
}
if config.Debug {
log.Printf("processed %d triggers, %d global, %d interesting tiles", len(trigs), len(globalTriggers), len(triggersByTile))
}
l, err := game.Level()
if err != nil {
return fmt.Errorf("loading level: %v", err)
}
t, err := loadTerrain(l, scene.World)
if err != nil {
return fmt.Errorf("loading terrain: %v", err)
}
terrain = t
obstacles, paths = l.Obstacles, l.Paths
if obstacles == nil || paths == nil || config.LevelGeomDump != "" {
// TODO: compute unfattened static obstacles and fully dynamic paths to support
// multiple units.
// Invert the footprint to fatten the obstacles with.
ul, dr := player.Footprint()
ul = ul.Mul(-1)
dr = dr.Mul(-1)
obstacles, paths = t.ObstaclesAndPaths(dr, ul, scene.View.Size())
if config.LevelGeomDump != "" {
f, err := os.Create(config.LevelGeomDump)
if err != nil {
return err
}
defer f.Close()
_, err = fmt.Fprintf(f, levelGeomDumpFmt, obstacles, paths)
err = f.Close()
if err != nil {
return err
}
}
}
//scene.CameraFocus(player.Pos())
terrain.AddToScene(scene)
if config.LevelPreview {
t.MakeAllVisible()
}
scene.sortFixedIfNeeded()
return nil
}
// Run runs the game (ebiten.Run) in addition to setting up any necessary GIF recording.
func Run(g Game, cfg *Config) error {
config = cfg
if err := load(g); err != nil {
return err
}
up := update
if cfg.RecordingFile != "" {
f, err := os.Create(cfg.RecordingFile)
if err != nil {
return fmt.Errorf("creating recording file: %v", err)
}
defer f.Close()
up = ebitenutil.RecordScreenAsGIF(up, f, cfg.RecordingFrames)
}
cs := g.Scene().View.Size()
ps, t := g.Viewport()
return ebiten.Run(up, cs.X, cs.Y, ps, t)
}
/*
// drawDebug draws debugging graphics onto the screen if Debug is true.
func drawDebug(screen *ebiten.Image) error {
if !Debug {
return nil
}
obsView := GraphView{
edges: obstacles.Edges(),
edgeColour: color.RGBA{0xff, 0, 0, 0xff},
normalColour: color.RGBA{0, 0xff, 0, 0xff},
}
if err := screen.DrawLines(obsView); err != nil {
return err
}
pathsView := GraphView{
edges: paths.Edges(),
edgeColour: color.RGBA{0, 0, 0xff, 0xff},
normalColour: color.Transparent,
}
if err := screen.DrawLines(pathsView); err != nil {
return err
}
if len(player.Path()) > 0 {
u := player.Pos().Sub(camPos)
for _, v := range player.Path() {
v = v.Sub(camPos)
if err := screen.DrawLine(u.X, u.Y, v.X, v.Y, color.RGBA{0, 0xff, 0xff, 0xff}); err != nil {
return err
}
u = v
}
}
return nil
}
*/
func evaluateTriggers(triggers []*Trigger) bool {
trigLoop:
for _, trig := range triggers {
if trig.fired && !trig.Repeat {
continue
}
if trig.Active != nil && !trig.Active(modelFrame) {
continue
}
// All dependencies fired?
for _, dep := range trig.Depends {
if !triggersByName[dep].fired {
continue trigLoop
}
}
if config.Debug {
log.Printf("firing %q", trig.Name)
}
if trig.Fire != nil {
trig.Fire(modelFrame)
}
//dialogueStack = trig.Dialogues
trig.fired = true
return true
}
return false
}
func clientUpdate(e *Event) {
// Is it game time yet?
if dialogue != nil {
return
}
for _, o := range scene.loose {
if u, ok := o.Part.(*Sprite); ok {
u.Update(modelFrame)
}
}
game.Handle(e)
}
// modelUpdate does update stuff, but no drawing. It is called once per config.FramesPerUpdate.
func modelUpdate() {
// Read inputs
md := ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft)
if md {
lastCursorPos = vec.NewI2(ebiten.CursorPosition())
}
tt := ebiten.Touches()
if len(tt) > 0 {
md = true
lastCursorPos = vec.NewI2(tt[0].Position())
}
e := &Event{
Time: modelFrame,
ScreenPos: lastCursorPos,
WorldPos: lastCursorPos.Sub(scene.World.Position()),
MouseDown: md,
}
switch {
case md && !mouseDown:
e.Type = EventMouseDown
case !md && mouseDown:
e.Type = EventMouseUp
}
mouseDown = md
// TODO: propagate events along the view hierarchy...
// Do we proceed with the game, or with the dialogue display?
if dialogue == nil {
// Got any triggers?
evaluateTriggers(globalTriggers)
clientUpdate(e)
if pt := terrain.TileCoord(playerSprite.Pos.I2()); pt != lastPlayerTile {
evaluateTriggers(triggersByTile[pt])
lastPlayerTile = pt
}
if len(dialogueStack) > 0 {
//player.GoIdle() now in PushDialogue{,ToBack}
playNextDialogue()
}
modelFrame++
terrain.UpdatePartVisibility(playerSprite.Pos.I2(), 5)
} else if dialogue.Handle(e) {
if len(dialogueStack) == 0 {
evaluateTriggers(globalTriggers)
}
playNextDialogue()
}
scene.Update() // Reorganise draw lists
}
// update is the main update function.
func update(screen *ebiten.Image) error {
displayFrame++
if displayFrame%config.FramesPerUpdate == 0 {
modelUpdate()
}
return scene.Draw(screen)
}
// Navigate attempts to construct a path within the terrain.
func Navigate(from, to vec.I2) []vec.I2 {
limits := scene.View.Bounds().Translate(scene.World.Position().Mul(-1))
path, err := vec.FindPath(obstacles, paths, from, to, limits)
if err != nil {
// Go near to the cursor position.
e, q := obstacles.NearestPoint(to)
if config.Debug {
log.Printf("nearest edge: %#v to point: %#v", e, q)
}
q = q.Add(e.V.Sub(e.U).Normal().Sgn()) // Adjust it slightly...
path2, err2 := vec.FindPath(obstacles, paths, from, q, limits)
if err2 != nil {
// Ok... Go as far as we can go.
p2, y := obstacles.NearestBlock(from, to)
if y {
to = p2.Sub(p2.Sub(from).Sgn())
}
path2 = []vec.I2{to}
}
path = path2
}
if config.Debug {
log.Printf("path: %#v", path)
}
return path
}