-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathgame.go
More file actions
295 lines (241 loc) · 7.12 KB
/
game.go
File metadata and controls
295 lines (241 loc) · 7.12 KB
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
/*
* Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved.
*
* 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 spx
import (
"reflect"
"sync"
"time"
spxfs "github.com/goplus/spx/v2/fs"
_ "github.com/goplus/spx/v2/fs/asset"
_ "github.com/goplus/spx/v2/fs/zip"
"github.com/goplus/spx/v2/internal/audio"
"github.com/goplus/spx/v2/internal/base/collision"
corestate "github.com/goplus/spx/v2/internal/core/state"
"github.com/goplus/spx/v2/internal/coroutine"
"github.com/goplus/spx/v2/internal/engine"
spxlog "github.com/goplus/spx/v2/internal/log"
itime "github.com/goplus/spx/v2/internal/time"
"github.com/goplus/spx/v2/internal/ui"
)
const (
XGoPackage = true
)
const (
Gop_sched = "Sched,SchedNow"
)
var (
gco *coroutine.Coroutines
tySprite = reflect.TypeFor[Sprite]()
)
var runtimeStateMgr corestate.RuntimeManager
type dbgFlags int
const (
DbgFlagLoad dbgFlags = 1 << iota
DbgFlagInstr
DbgFlagEvent
DbgFlagPerf
DbgFlagAll = DbgFlagLoad | DbgFlagInstr | DbgFlagEvent | DbgFlagPerf
)
const (
MOUSE_BUTTON_LEFT int64 = 1
MOUSE_BUTTON_RIGHT int64 = 2
MOUSE_BUTTON_MIDDLE int64 = 3
)
const (
eventBufferSize = 16
schedTimeoutMs = 3000
mainExecTimeoutSec = 3
mouseMovementThreshold = 1.0
initialSpriteSyncBufferSize = 100
baseScreenWidth = 480
baseScreenHeight = 360
)
// Game represents the main game instance with all core systems.
type Game struct {
baseObj
scriptEventBindings
fs spxfs.Dir
lifecycleState corestate.GameLifecycleState
displayState corestate.GameDisplayState
dialogState corestate.GameDialogState
debugState corestate.GameDebugState
gameRuntimeState corestate.GameRuntimeState
pathfindingState corestate.GamePathfindingState
audioState corestate.GameAudioState
Camera Camera
camera *cameraImpl
typs map[string]reflect.Type
sprs map[string]Sprite
sounds map[string]sound
events chan event
scriptEvents scriptEventRegistry
gamer Gamer
bootstrapMu sync.Mutex
bootstrapGen uint64
bootstrapStarted bool
pendingBootstrap []func()
sprCollisionInfos map[string]*spriteCollisionInfo
sprCollisionData []*spriteCollisionData
isCollisionByPixel bool
isAutoSetCollisionLayer bool
engineMgr engineManagers
inputMgr inputManager
soundMgr audio.Manager
shapeMgr shapeManager
tilemapMgr gameTilemapMgr
syncBuffer *engine.SpriteSyncBuffer
triggerEvents []engine.TriggerEvent
spatialHash *collision.SpatialHash[*SpriteImpl]
}
func activeGame() *Game {
game, _ := engine.GetGame().(*Game)
return game
}
func (p *Game) initRuntimeState() {
runtimeStateMgr.Init(&p.debugState, &p.gameRuntimeState)
syncCoroutinePerfDebug(p.debugState.DebugPerf)
p.initEventQueueState()
}
func setDefaultDebugFlags(instr, event, perf bool) {
runtimeStateMgr.SetDefaultDebugFlags(instr, event, perf)
syncCoroutinePerfDebug(perf)
}
func (p *Game) setDebugFlags(instr, event, perf bool) {
runtimeStateMgr.ApplyDebugFlags(&p.debugState, instr, event, perf)
syncCoroutinePerfDebug(perf)
}
// syncCoroutinePerfDebug keeps the process-wide coroutine scheduler aligned with
// the most recently applied perf-debug setting. The scheduler is global, so this
// flag is intentionally global as well.
func syncCoroutinePerfDebug(enabled bool) {
gco.SetPerfDebug(enabled)
}
func isDebugInstrEnabled() bool {
return runtimeStateMgr.DebugInstrEnabled(activeGameDebugState())
}
func isDebugEventEnabled() bool {
return runtimeStateMgr.DebugEventEnabled(activeGameDebugState())
}
func isDebugPerfEnabled() bool {
return runtimeStateMgr.DebugPerfEnabled(activeGameDebugState())
}
func (p *Game) setPhysicsEnabled(enabled bool) {
runtimeStateMgr.SetPhysicsEnabled(&p.gameRuntimeState, enabled)
}
func isPhysicsEnabled() bool {
return runtimeStateMgr.PhysicsEnabled(activeGameRuntimeState())
}
func (p *Game) resetImageSizeCache() {
runtimeStateMgr.ResetImageSizeCache(&p.gameRuntimeState)
}
func imageSizeCacheRef() *sync.Map {
return runtimeStateMgr.ImageSizeCacheRef(activeGameRuntimeState())
}
func setSchedInMain(inMain bool) {
runtimeStateMgr.SetSchedInMain(activeGameRuntimeState(), inMain)
}
func isSchedInMainState() bool {
return runtimeStateMgr.IsSchedInMain(activeGameRuntimeState())
}
func setMainSchedTime(t time.Time) {
runtimeStateMgr.SetMainSchedTime(activeGameRuntimeState(), t)
}
func mainSchedTime() time.Time {
return runtimeStateMgr.MainSchedTime(activeGameRuntimeState())
}
func activeGameDebugState() *corestate.GameDebugState {
if g := activeGame(); g != nil {
return &g.debugState
}
return nil
}
func activeGameRuntimeState() *corestate.GameRuntimeState {
return runtimeStateOfGame(activeGame())
}
func runtimeStateOfGame(g *Game) *corestate.GameRuntimeState {
if g == nil {
return nil
}
return &g.gameRuntimeState
}
func (p *Game) newSpriteAndLoad(name string, tySpr reflect.Type, g reflect.Value) Sprite {
spr := reflect.New(tySpr).Interface().(Sprite)
if err := p.loadSprite(spr, name, g); err != nil {
panic(err)
}
return spr
}
func (p *Game) getSpriteProto(tySpr reflect.Type, g reflect.Value) Sprite {
name := tySpr.Name()
spr, ok := p.sprs[name]
if !ok {
spr = p.newSpriteAndLoad(name, tySpr, g)
}
return spr
}
func (p *Game) getSpriteProtoByName(name string, g reflect.Value) Sprite {
spr, ok := p.sprs[name]
if !ok {
tySpr, ok := p.typs[name]
if !ok {
spxlog.Panicf("sprite %s is not defined", name)
}
spr = p.newSpriteAndLoad(name, tySpr, g)
}
return spr
}
func (p *Game) reset() {
p.lifecycleState.IsRunned.Store(false)
p.releaseGameAudio()
p.EraseAll()
p.scriptEvents.Reset()
p.shapeMgr.reset()
p.debugState.DebugPanel = nil
p.dialogState.AskPanel = nil
p.Stop(AllOtherScripts)
p.lifecycleState.StartFlag = sync.Once{}
p.lifecycleState.RunOnce = sync.Once{}
p.lifecycleState.OncePathFinder = sync.Once{}
p.lifecycleState.StartDispatched.Store(false)
p.sprs = make(map[string]Sprite)
p.resetBootstrapState()
p.resetCollisionLayerState()
p.resetImageSizeCache()
p.resetEventQueueStats()
close(p.events)
itime.OnReload()
}
func (p *Game) initGame(sprites []Sprite) *Game {
engine.SetGame(p)
p.initShapeMgr()
p.initRuntimeState()
p.scriptEventBindings.init(&p.scriptEvents, p)
p.engineMgr = engineManagers{}
engine.SetManagers(&p.engineMgr)
ui.Init(&p.engineMgr)
p.sprs = make(map[string]Sprite)
p.sounds = make(map[string]sound)
p.typs = make(map[string]reflect.Type)
p.syncBuffer = engine.NewSpriteSyncBuffer(initialSpriteSyncBufferSize)
for _, spr := range sprites {
tySpr := reflect.TypeOf(spr).Elem()
p.typs[tySpr.Name()] = tySpr
}
return p
}
func (p *Game) initShapeMgr() {
p.shapeMgr.init()
}