-
Notifications
You must be signed in to change notification settings - Fork 3
/
scene.go
286 lines (230 loc) · 6 KB
/
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
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
package gozmo
import (
"encoding/json"
"fmt"
"io/ioutil"
)
// A Scene is a group of resources (textures, animations, sounds) and
// instantiated gameObjects, akin to levels in games.
//
// When a scene is destroyed, all of the allocated resources and gameObjects
// are destroyed too.
type Scene struct {
// TODO: is it a good idea to expose Name?
Name string
gameObjects map[string]*GameObject
textures map[string]*Texture
animations map[string]*Animation
// The last timestamp of the engine.
lastTime float64
orderedGameObjects map[int][]*GameObject
orderedKeys []int
}
func (scene *Scene) Update(now float64) {
deltaTime := float32(now - scene.lastTime)
scene.lastTime = now
for _, order := range scene.orderedKeys {
for _, gameObject := range scene.orderedGameObjects[order] {
if !gameObject.enabled {
continue
}
gameObject.DeltaTime = deltaTime
// consume enqueued events
gameObject.ManageEvents()
// call Update() on components
gameObject.Update()
}
}
for _, updater := range Engine.registeredUpdaters {
updater(scene, deltaTime)
}
UpdatePerFrameStats()
}
func NewScene(name string) *Scene {
scene := Scene{Name: name}
scene.gameObjects = make(map[string]*GameObject)
scene.textures = make(map[string]*Texture)
scene.animations = make(map[string]*Animation)
scene.orderedGameObjects = make(map[int][]*GameObject)
if Engine.scenes == nil {
Engine.scenes = make(map[string]*Scene)
}
Engine.scenes[name] = &scene
return &scene
}
func loadTextures(scene *Scene, textures []interface{}) {
for _, texture := range textures {
texMap := texture.(map[string]interface{})
name, ok := texMap["name"]
if !ok {
panic("texture requires a name")
}
filename, hasFilename := texMap["filename"]
rows, hasRows := texMap["rows"]
cols, hasCols := texMap["cols"]
var tex *Texture
var err error
if hasFilename {
tex, err = scene.NewTextureFromFilename(name.(string), filename.(string))
if err != nil {
panic(err)
}
}
if tex == nil {
continue
}
if hasRows {
tex.SetRows(uint32(rows.(float64)))
}
if hasCols {
tex.SetCols(uint32(cols.(float64)))
}
}
}
func addComponents(gameObject *GameObject, components []interface{}) {
for _, component := range components {
componentMap := component.(map[string]interface{})
componentName, ok := componentMap["name"]
if !ok {
panic("component requires a name")
}
componentType, ok := componentMap["type"]
if !ok {
panic("component requires a type")
}
var args []interface{} = nil
argsItem, ok := componentMap["args"]
if ok {
argsList, ok := argsItem.([]interface{})
if ok {
args = argsList
}
}
gameObject.AddComponentByName(componentName.(string), componentType.(string), args)
}
}
func setAttrs(gameObject *GameObject, attrs []interface{}) {
for _, attr := range attrs {
attrMap := attr.(map[string]interface{})
component, ok := attrMap["component"]
if !ok {
panic("attr requires a component")
}
key, ok := attrMap["key"]
if !ok {
panic("attr requires a key")
}
value, ok := attrMap["value"]
if !ok {
panic("attr requires a value")
}
err := gameObject.SetAttr(component.(string), key.(string), value)
if err != nil {
fmt.Println(err)
}
}
}
func loadObjects(scene *Scene, objects []interface{}) {
for _, obj := range objects {
objMap := obj.(map[string]interface{})
name, ok := objMap["name"]
if !ok {
panic("object requires a name")
}
components, hasComponents := objMap["components"]
attrs, hasAttrs := objMap["attrs"]
gameObject := scene.NewGameObject(name.(string))
if hasComponents {
addComponents(gameObject, components.([]interface{}))
}
if hasAttrs {
setAttrs(gameObject, attrs.([]interface{}))
}
}
}
func loadAnimations(scene *Scene, animations []interface{}) {
for _, anim := range animations {
animMap := anim.(map[string]interface{})
name, ok := animMap["name"]
if !ok {
panic("animation requires a name")
}
fps, hasFps := animMap["fps"]
loop, hasLoop := animMap["loop"]
frames, hasFrames := animMap["frames"]
animation := scene.AddAnimation(name.(string), 0, false)
if hasFps {
animation.Fps = int(fps.(float64))
}
if hasLoop {
animation.Loop = loop.(bool)
}
if hasFrames {
var actionsList []*AnimationAction
for _, frame := range frames.([]interface{}) {
actions := frame.([]interface{})
for _, action := range actions {
actionMap := action.(map[string]interface{})
component, ok := actionMap["component"]
if !ok {
panic("animation action requires a component")
}
key, ok := actionMap["key"]
if !ok {
panic("animation action requires a key")
}
value, ok := actionMap["value"]
if !ok {
panic("animation action requires a value")
}
actionsList = append(actionsList, &AnimationAction{ComponentName: component.(string), Attr: key.(string), Value: value})
}
animation.AddFrame(actionsList)
}
}
}
}
func NewSceneFromFilename(fileName string) *Scene {
data, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
var parsed map[string]interface{}
err = json.Unmarshal(data, &parsed)
if err != nil {
panic(err)
}
name, ok := parsed["name"]
if !ok {
panic("a scene requires a name")
}
scene := NewScene(name.(string))
for key, value := range parsed {
switch key {
case "name":
scene.Name = value.(string)
case "textures":
textures := value.([]interface{})
loadTextures(scene, textures)
case "objects":
objects := value.([]interface{})
loadObjects(scene, objects)
case "animations":
animations := value.([]interface{})
loadAnimations(scene, animations)
}
}
return scene
}
func (scene *Scene) Destroy() {
// Destroy all objects.
for _, gameObject := range scene.gameObjects {
gameObject.Destroy()
}
// Destroy all textures.
for _, texture := range scene.textures {
texture.Destroy()
}
// Remove the scene from the map, so that the GC can reclaim it.
delete(Engine.scenes, scene.Name)
}