-
Notifications
You must be signed in to change notification settings - Fork 5
/
root_scene.go
101 lines (83 loc) · 2.07 KB
/
root_scene.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
package ge
import (
"github.com/quasilyte/ge/physics"
)
const zindexMax = 6
type RootScene struct {
context *Context
controller SceneController
objects []SceneObject
addedObjects []SceneObject
delayedFuncs []delayedFunc
collisionEngine physics.CollisionEngine
graphics [zindexMax][]SceneGraphics
subSceneArray [zindexMax]Scene
}
type SimulationRunner struct {
root *RootScene
}
func (r *SimulationRunner) Update(delta float64) {
r.root.update(delta)
}
func NewSimulatedScene(ctx *Context, controller SceneController) (*SimulationRunner, *Scene) {
root := newRootScene()
root.context = ctx
root.controller = controller
scene := &root.subSceneArray[1]
return &SimulationRunner{root: root}, scene
}
func newRootScene() *RootScene {
root := &RootScene{
objects: make([]SceneObject, 0, 32),
addedObjects: make([]SceneObject, 0, 8),
graphics: [zindexMax][]SceneGraphics{
make([]SceneGraphics, 0, 16),
make([]SceneGraphics, 0, 24),
make([]SceneGraphics, 0, 8),
},
}
for i := range root.subSceneArray {
root.subSceneArray[i].zindex = uint8(i)
root.subSceneArray[i].root = root
}
return root
}
func (scene *RootScene) addObject(o SceneObject, zindex uint) {
if zindex < zindexMax {
scene.addedObjects = append(scene.addedObjects, o)
o.Init(&scene.subSceneArray[zindex])
return
}
panic("z index overflow")
}
func (scene *RootScene) update(delta float64) {
if len(scene.delayedFuncs) != 0 {
funcs := scene.delayedFuncs[:0]
for _, fn := range scene.delayedFuncs {
fn.delay -= delta
if fn.delay <= 0 {
fn.action()
} else {
funcs = append(funcs, fn)
}
}
scene.delayedFuncs = funcs
}
scene.collisionEngine.CalculateFrame()
scene.controller.Update(delta)
liveObjects := scene.objects[:0]
for _, o := range scene.objects {
if o.IsDisposed() {
continue
}
o.Update(delta)
liveObjects = append(liveObjects, o)
}
scene.objects = liveObjects
scene.objects = append(scene.objects, scene.addedObjects...)
scene.addedObjects = scene.addedObjects[:0]
}
type delayedFunc struct {
delay float64
action func()
}