/
core.go
200 lines (160 loc) · 3.75 KB
/
core.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
package core
import (
"fmt"
"log"
"reflect"
"time"
"github.com/jmorgan1321/golang-game/lib/engine/support"
"github.com/jmorgan1321/golang-games/lib/engine/debug"
)
var (
g_gameCore *core
)
type core struct {
RootSpace JGameObjectHandle
managers []JManager
spaces []JSpace
// Data
TimeStamp time.Time
}
func New() *core {
return nil
}
// JGameObjectHandle wraps a game object and makes sure that the object is valid
// before making any calls into it.
//
type JGameObjectHandle struct {
obj JGameObject
id int
}
// func (o JGameObjectHandle) TriggerEvent(...) JGameObject {
// if o.IsValid() {
// o.Get().TriggerEvent(...)
// }
// }
func (o JGameObjectHandle) Get() JGameObject {
// return factory.GetById(o.id)
}
func (o JGameObjectHandle) ID() int {
return o.id
}
func LoadConfig(file string) JGameObjectHandle {
return JGameObjectHandle{}
}
func (c *core) StartUp(config JGameObjectHandle) {
defer debug.Trace().UnTrace()
for _, m := range c.managers {
m.StartUp(config)
}
for _, s := range c.spaces {
s.Init()
}
}
func (c *core) ShutDown() {
defer debug.Trace().UnTrace()
for i := len(c.spaces) - 1; i >= 0; i-- {
c.spaces[i].DeInit()
}
for i := len(c.managers) - 1; i >= 0; i-- {
c.managers[i].ShutDown()
}
}
func (c *core) Step() {
defer debug.Trace().UnTrace()
if c.TimeStamp.IsZero() {
c.TimeStamp = time.Now()
}
if dt := time.Since(c.TimeStamp); dt >= 16*time.Millisecond {
fmt.Println("frame:", c.CurrFrame)
c.CurrFrame++
for _, mgr := range c.managers {
mgr.BeginFrame()
defer mgr.EndFrame()
}
for _, spc := range c.spaces {
spc.Update(float32(dt))
}
fmt.Println()
}
}
func (c *Core) RegisterSpaces(spaces ...JSpace) {
c.spaces = append(c.spaces, spaces...)
}
func (c *Core) RegisterManagers(mgrs ...JManager) {
c.managers = append(c.managers, mgrs...)
}
func (c *Core) Manager(name string) JManager {
for _, mgr := range c.managers {
// TODO: possibly switch this to use package.Name
mtype := reflect.ValueOf(mgr).Type().Elem()
if mtype.Name() == name {
return mgr
}
}
// TODO: better error message when component isn't found
return nil
}
type Factory struct {
NameMap map[string]JGameObjectHandle
ObjList []JGameObject
}
func (f *Factory) StartUp(config JGameObjectHandle) {
}
func (f *Factory) ShutDown() {
}
func (f *Factory) BeginFrame() {
}
func (f *Factory) EndFrame() {
endOfFrameCleanup()
}
func (f *Factory) endOfFrameCleanup() {
}
func (f *Factory) MarkObjectForDeletion(JGameObjectHandle) {
}
type FactoryOpt func(JGameObject)
// s := f.Create(f1, Name("space"))
// goc1 := f.Create(f2, Owner(s), Name("goc1"))
// goc2 := f.Create(f2, Owner(s), Dispatcher(d))
func (f *Factory) Create(file string, opts ...FactoryOpt) JGameObjectHandle {
defer debug.Trace().UnTrace()
// read in game object data
data, err := support.OpenFile(file)
if err != nil {
support.LogFatal("Failed to open Config file: " + file)
}
holder, err := support.ReadData(data)
if err != nil {
support.LogFatal("Failed to read in Config file: " + file)
return nil
}
m := v.(map[string]interface{})
typename, err := m["Type"]
if err != nil {
log.Panic(err)
}
obj := factoryFunc(typename.(string))
SerializeInPlace(obj, holder)
// apply options to game object
for _, opt := range opts {
opt(obj)
}
// check for transform, check for dispatcher, name etc
if obj.Name() == "" {
obj.SetName(fmt.Sprint("obj", len(f.ObjList)))
}
// add game object to factory
f.ObjList = append(f.ObjList, obj)
h := JGameObjectHandle{id: len(f.ObjList)}
f.NameMap[obj.Name()] = h
return h
}
func Name(name string) FactoryOpt {
return func(obj JGameObject) {
obj.SetName(name)
}
}
// func AddComp(comp JComponent) FactoryOpt {
// return func(obj JGameObject) {
// obj.AddComps(comp)
// }
// }