forked from xiaonanln/goworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goworld.go
203 lines (170 loc) · 6.13 KB
/
goworld.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
package goworld
import (
"time"
"github.com/xiaonanln/goTimer"
"github.com/xiaonanln/goworld/components/game"
"github.com/xiaonanln/goworld/engine/common"
"github.com/xiaonanln/goworld/engine/config"
"github.com/xiaonanln/goworld/engine/entity"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/kvdb"
"github.com/xiaonanln/goworld/engine/post"
"github.com/xiaonanln/goworld/engine/service"
"github.com/xiaonanln/goworld/engine/storage"
)
// Export useful types
type Vector3 = entity.Vector3
type Entity = entity.Entity
type Space = entity.Space
type EntityID = common.EntityID
// Run runs the server endless loop
//
// This is the main routine for the server and all entity logic,
// and this function never quit
func Run() {
game.Run()
}
// RegisterEntity registers the entity type so that entities can be created or loaded
//
// returns the entity type description object which can be used to define more properties
// of entity type
func RegisterEntity(typeName string, entityPtr entity.IEntity) *entity.EntityTypeDesc {
return entity.RegisterEntity(typeName, entityPtr, false)
}
// RegisterService registeres an service type
// After registeration, the service entity will be created automatically on some game
func RegisterService(typeName string, entityPtr entity.IEntity) {
service.RegisterService(typeName, entityPtr)
}
// CreateSpaceAnywhere creates a space with specified kind in any game server
func CreateSpaceAnywhere(kind int) EntityID {
if kind == 0 {
gwlog.Panicf("Can not create nil space with kind=0. Game will create 1 nil space automatically.")
}
return entity.CreateSpaceAnywhere(kind)
}
// CreateSpaceLocally creates a space with specified kind in the local game server
//
// returns the space EntityID
func CreateSpaceLocally(kind int) *Space {
if kind == 0 {
gwlog.Panicf("Can not create nil space with kind=0. Game will create 1 nil space automatically.")
}
return entity.CreateSpaceLocally(kind)
}
// CreateEntityLocally creates a entity on the local server
//
// returns EntityID
func CreateEntityLocally(typeName string) *Entity {
return entity.CreateEntityLocally(typeName, nil)
}
// CreateEntityAnywhere creates a entity on any server
func CreateEntityAnywhere(typeName string) EntityID {
return entity.CreateEntityAnywhere(typeName)
}
// LoadEntityAnywhere loads the specified entity from entity storage
func LoadEntityAnywhere(typeName string, entityID EntityID) {
entity.LoadEntityAnywhere(typeName, entityID)
}
// ListEntityIDs gets all saved entity ids in storage, may take long time and block the main routine
//
// returns result in callback
func ListEntityIDs(typeName string, callback storage.ListCallbackFunc) {
storage.ListEntityIDs(typeName, callback)
}
// Exists checks if entityID exists in entity storage
//
// returns result in callback
func Exists(typeName string, entityID EntityID, callback storage.ExistsCallbackFunc) {
storage.Exists(typeName, entityID, callback)
}
// GetEntity gets the entity by EntityID
func GetEntity(id EntityID) *Entity {
return entity.GetEntity(id)
}
// GetSpace gets the space by ID
func GetSpace(id EntityID) *Space {
return entity.GetSpace(id)
}
// GetGameID gets the local server ID
//
// server ID is a uint16 number starts from 1, which should be different for each servers
// server ID is also in the game config section name of goworld.ini
func GetGameID() uint16 {
return game.GetGameID()
}
// MapAttr creates a new MapAttr
func MapAttr() *entity.MapAttr {
return entity.NewMapAttr()
}
// ListAttr creates a new ListAttr
func ListAttr() *entity.ListAttr {
return entity.NewListAttr()
}
// RegisterSpace registers the space entity type.
//
// All spaces will be created as an instance of this type
func RegisterSpace(spacePtr entity.ISpace) {
entity.RegisterSpace(spacePtr)
}
// Entities gets all entities as an EntityMap (do not modify it!)
func Entities() entity.EntityMap {
return entity.Entities()
}
// Call other entities
func Call(id EntityID, method string, args ...interface{}) {
entity.Call(id, method, args)
}
// CallService calls a service entity
func CallService(serviceName string, method string, args ...interface{}) {
service.CallService(serviceName, method, args)
}
// GetServiceEntityID returns the entityid of the service
func GetServiceEntityID(serviceName string) common.EntityID {
return service.GetServiceEntityID(serviceName)
}
// CallNilSpaces calls methods of all nil spaces on all games
func CallNilSpaces(method string, args ...interface{}) {
entity.CallNilSpaces(method, args, game.GetGameID())
}
// GetNilSpaceID returns the Entity ID of nil space on the specified game
func GetNilSpaceID(gameid uint16) EntityID {
return entity.GetNilSpaceID(gameid)
}
// GetNilSpace returns the nil space on this game
// Nil space is a special space with Kind = 0. Nil space is the default space for all created entities.
// Each game has one nil space with fixed EntityID for each game, which can be acquired by calling `GetNilSpaceID`
//
// Since nil game exists on each game with fixed EntityID, an entity can migrate to target game by calling `e.EnterSpace(GetNilSpaceID(gameid), Vector3{})`
func GetNilSpace() *Space {
return entity.GetNilSpace()
}
// GetKVDB gets value of key from KVDB
func GetKVDB(key string, callback kvdb.KVDBGetCallback) {
kvdb.Get(key, callback)
}
// PutKVDB puts key-value to KVDB
func PutKVDB(key string, val string, callback kvdb.KVDBPutCallback) {
kvdb.Put(key, val, callback)
}
// GetOrPut gets value of key from KVDB, if val not exists or is "", put key-value to KVDB.
func GetOrPutKVDB(key string, val string, callback kvdb.KVDBGetOrPutCallback) {
kvdb.GetOrPut(key, val, callback)
}
// ListGameIDs returns all game IDs
func ListGameIDs() []uint16 {
return config.GetGameIDs()
}
// AddTimer adds a timer to be executed after specified duration
func AddCallback(d time.Duration, callback func()) {
timer.AddCallback(d, callback)
}
// AddTimer adds a repeat timer to be executed every specified duration
func AddTimer(d time.Duration, callback func()) {
timer.AddTimer(d, callback)
}
// Post posts a callback to be executed
// It is almost same as AddCallback(0, callback)
func Post(callback post.PostCallback) {
post.Post(callback)
}