-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
293 lines (215 loc) · 5.38 KB
/
node.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
package gogonet
import (
"fmt"
"log"
"strings"
"sync/atomic"
"time"
"github.com/TheMrViper/gogonet/signals"
"github.com/TheMrViper/gogonet/utils"
)
type Node struct {
name string
instanceId uint32
parent INode
childs map[uint32]INode
nativeRpc map[string]INativeMethod
multiplayerAPI *MultiplayerAPI
}
//
// tree stuff
//
var tree INode
var lastInstanceId uint32
func init() {
tree = NewNode("/root")
tree.(*Node).multiplayerAPI = &MultiplayerAPI{
signals: signals.New(),
connectedPeers: make(map[uint32]bool),
recvPathCache: make(map[uint32]map[uint32]string),
sentPathCache: make(map[string]*SentPathCache),
}
}
type ITree interface {
INode
SetScene(scene INode)
SetNetworkPeer(peer INetworkPeer)
ListenAndServe()
}
func GetTree() ITree {
return tree.(*Node)
}
func (t *Node) SetScene(scene INode) {
t.childs = make(map[uint32]INode)
t.childs[scene.InstanceID()] = scene
}
func (t *Node) SetNetworkPeer(peer INetworkPeer) {
t.multiplayerAPI.SetNetworkPeer(peer)
}
func (t *Node) ListenAndServe() {
go t.multiplayerAPI.ListenAndServe()
fps := 60
prev := time.Now()
ticker := time.NewTicker(time.Second / time.Duration(fps))
for now := range ticker.C {
prev = now
}
fmt.Print(prev)
}
//
// scene stuff
//
var sceneRepo = NewNode("/scene_repo")
type IScene interface {
INode
Instance() INode
}
func GetScene(name string) IScene {
return sceneRepo.GetNode(name).(*Node)
}
func NewScene(name string) IScene {
return sceneRepo.NewNode(name).(*Node)
}
func GetOrNewScene(name string) IScene {
return sceneRepo.GetOrNewNode(name).(*Node)
}
// Lets think that, /root is scene tree, like in godot, so we can call instance() method
func (n *Node) Instance() INode {
// TODO
utils.Log(6, n.parent)
utils.IfPanic(n.parent != sceneRepo, "Cannot create instance of this scene, maybe its node?")
node := n.clone()
node.instanceId = nodeGenerateId()
return node
}
func (n *Node) clone() *Node {
node := NewNode(n.name).node()
node.nativeRpc = n.nativeRpc
for id, child := range n.childs {
node.childs[id] = child.node().clone()
}
return node
}
//
// node stuff
//
type INode interface {
node() *Node
Name() string
Path() string
InstanceID() uint32
NewNode(path string) INode
GetNode(path string) INode
GetOrNewNode(path string) INode
AppendChild(node INode)
AddNativeRPCMethod(method INativeMethod)
Rpc(procedureName string, params ...interface{})
RpcId(id int32, procedureName string, params ...interface{})
RpcUnreliable(procedureName string, params ...interface{})
RpcUnreliableId(id int32, procedureName string, params ...interface{})
}
func NewNode(name string) INode {
return &Node{
name: name,
instanceId: nodeGenerateId(),
childs: make(map[uint32]INode),
nativeRpc: make(map[string]INativeMethod),
}
}
func nodeGenerateId() uint32 {
atomic.AddUint32(&lastInstanceId, 1)
return lastInstanceId
}
func (n *Node) node() *Node {
return n
}
func (n *Node) Name() string {
return n.node().name
}
func (n *Node) Path() (result string) {
log.Println(n.parent)
for n.parent != nil {
result = "/" + n.name + result
n = n.parent.node()
}
return
}
func (n *Node) InstanceID() uint32 {
return n.instanceId
}
// Create child node, or if path is absolute, create node for this path
func (n *Node) NewNode(path string) (r INode) {
paths := strings.Split(path, "/")
utils.IfPanic(len(paths) < 1, "Cannot create new node, invalid path")
if paths[0] == "" {
// absolute node, go to root and make path relative
utils.IfPanic(len(paths) < 2, "Cannot create new node, invalid path")
if len(paths[1:]) > 1 && GetTree().Name() == paths[1] {
paths = paths[1:]
}
return GetTree().NewNode(strings.Join(paths[1:], "/"))
}
for len(paths) > 0 {
utils.IfPanic(len(paths[0]) < 1, "Cannot create new node, invalid path")
r = NewNode(paths[0])
r.node().parent = n
n.AppendChild(r)
n = r.node()
paths = paths[1:]
}
return
}
func (n *Node) GetNode(path string) INode {
paths := strings.Split(path, "/")
if len(paths) <= 0 {
panic("Invalid path " + path)
}
// If path is absolute, go to top, and find childs
if paths[0] == "" {
// also skip root node
// so this paths will be the same
// /root/Button
// /Button
if len(paths[1:]) > 1 && GetTree().Name() == paths[1] {
paths = paths[1:]
}
return GetTree().GetNode(strings.Join(paths[1:], "/"))
}
// If path is relative, search right here
for _, node := range n.childs {
if node.Name() == paths[0] {
if len(paths) > 1 {
return node.GetNode(strings.Join(paths[1:], "/"))
}
return node
}
}
return nil
}
func (n *Node) GetOrNewNode(path string) INode {
if node := n.GetNode(path); node != nil {
return node
}
return n.NewNode(path)
}
func (n *Node) AppendChild(node INode) {
if node == nil {
panic("Node cant be nil")
}
if _, ok := n.childs[node.InstanceID()]; ok {
return
}
node.node().parent = n
node.node().multiplayerAPI = n.multiplayerAPI
n.childs[node.InstanceID()] = node
}
//
// rpc stuff
//
func (n *Node) AddNativeRPCMethod(method INativeMethod) {
n.nativeRpc[method.Name()] = method
}
func (n *Node) Rpc(procedureName string, params ...interface{}) {}
func (n *Node) RpcId(id int32, procedureName string, params ...interface{}) {}
func (n *Node) RpcUnreliable(procedureName string, params ...interface{}) {}
func (n *Node) RpcUnreliableId(id int32, procedureName string, params ...interface{}) {}