forked from xiaonanln/goworld
/
MySpace.go
140 lines (116 loc) · 3.69 KB
/
MySpace.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
package main
import (
"time"
"github.com/xiaonanln/goTimer"
"github.com/xiaonanln/goworld"
"github.com/xiaonanln/goworld/engine/entity"
"github.com/xiaonanln/goworld/engine/gwlog"
)
const (
_SPACE_DESTROY_CHECK_INTERVAL = time.Minute * 5
)
// MySpace is the custom space type
type MySpace struct {
goworld.Space // Space type should always inherit from entity.Space
destroyCheckTimer entity.EntityTimerID
}
// OnSpaceCreated is called when the space is created
func (space *MySpace) OnSpaceCreated() {
// notify the SpaceService that it's ok
space.EnableAOI()
goworld.CallService("SpaceService", "NotifySpaceLoaded", space.Kind, space.ID)
space.AddTimer(time.Second*5, "DumpEntityStatus")
space.AddTimer(time.Second*5, "SummonMonsters")
//M := 10
//for i := 0; i < M; i++ {
// space.CreateEntity("Monster", entity.Vector3{})
//}
}
func (space *MySpace) DumpEntityStatus() {
space.ForEachEntity(func(e *entity.Entity) {
gwlog.Debugf(">>> %s @ position %s, neighbors=%d", e, e.GetPosition(), len(e.Neighbors))
})
}
func (space *MySpace) SummonMonsters() {
if space.CountEntities("Monster") < space.CountEntities("Player")*2 {
space.CreateEntity("Monster", entity.Vector3{})
}
}
// OnEntityEnterSpace is called when entity enters space
func (space *MySpace) OnEntityEnterSpace(entity *entity.Entity) {
if entity.TypeName == "Player" {
space.onPlayerEnterSpace(entity)
}
}
func (space *MySpace) onPlayerEnterSpace(entity *entity.Entity) {
gwlog.Debugf("Player %s enter space %s, total avatar count %d", entity, space, space.CountEntities("Player"))
space.clearDestroyCheckTimer()
}
// OnEntityLeaveSpace is called when entity leaves space
func (space *MySpace) OnEntityLeaveSpace(entity *entity.Entity) {
if entity.TypeName == "Player" {
space.onPlayerLeaveSpace(entity)
}
}
func (space *MySpace) onPlayerLeaveSpace(entity *entity.Entity) {
gwlog.Infof("Player %s leave space %s, left avatar count %d", entity, space, space.CountEntities("Player"))
if space.CountEntities("Player") == 0 {
// no avatar left, start destroying space
space.setDestroyCheckTimer()
}
}
func (space *MySpace) setDestroyCheckTimer() {
if space.destroyCheckTimer != 0 {
return
}
space.destroyCheckTimer = space.AddTimer(_SPACE_DESTROY_CHECK_INTERVAL, "CheckForDestroy")
}
// CheckForDestroy checks if the space should be destroyed
func (space *MySpace) CheckForDestroy() {
avatarCount := space.CountEntities("Player")
if avatarCount != 0 {
gwlog.Panicf("Player count should be 0, but is %d", avatarCount)
}
goworld.CallService("SpaceService", "RequestDestroy", space.Kind, space.ID)
}
func (space *MySpace) clearDestroyCheckTimer() {
if space.destroyCheckTimer == 0 {
return
}
space.CancelTimer(space.destroyCheckTimer)
space.destroyCheckTimer = 0
}
// ConfirmRequestDestroy is called by SpaceService to confirm that the space
func (space *MySpace) ConfirmRequestDestroy(ok bool) {
if ok {
if space.CountEntities("Player") != 0 {
gwlog.Panicf("%s ConfirmRequestDestroy: avatar count is %d", space, space.CountEntities("Player"))
}
space.Destroy()
}
}
// OnGameReady is called when the game server is ready
func (space *MySpace) OnGameReady() {
timer.AddCallback(time.Millisecond*1000, checkServerStarted)
}
func checkServerStarted() {
ok := isAllServicesReady()
gwlog.Infof("checkServerStarted: %v", ok)
if ok {
onAllServicesReady()
} else {
timer.AddCallback(time.Millisecond*1000, checkServerStarted)
}
}
func isAllServicesReady() bool {
for _, serviceName := range _SERVICE_NAMES {
if goworld.GetServiceEntityID(serviceName).IsNil() {
gwlog.Infof("%s is not ready ...", serviceName)
return false
}
}
return true
}
func onAllServicesReady() {
gwlog.Infof("All services are ready!")
}