/
GameClient.go
121 lines (103 loc) · 4.07 KB
/
GameClient.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
package entity
import (
"fmt"
"github.com/xiaonanln/goworld/engine/common"
"github.com/xiaonanln/goworld/engine/consts"
"github.com/xiaonanln/goworld/engine/dispatchercluster"
"github.com/xiaonanln/goworld/engine/dispatchercluster/dispatcherclient"
"github.com/xiaonanln/goworld/engine/gwlog"
)
// GameClient represents the game Client of entity
//
// Each entity can have at most one GameClient, and GameClient can be given to other entities
type GameClient struct {
clientid common.ClientID
gateid uint16
ownerid common.EntityID
}
// MakeGameClient creates a GameClient object using Client ID and Game ID
func MakeGameClient(clientid common.ClientID, gateid uint16) *GameClient {
return &GameClient{
clientid: clientid,
gateid: gateid,
}
}
func (client *GameClient) String() string {
if client == nil {
return "GameClient<nil>"
}
return fmt.Sprintf("GameClient<%s@%d>", client.clientid, client.gateid)
}
func (client *GameClient) sendCreateEntity(entity *Entity, isPlayer bool) {
if client == nil {
return
}
var clientData map[string]interface{}
if !isPlayer {
clientData = entity.getAllClientData()
} else {
clientData = entity.getClientData()
}
pos := entity.Position
yaw := entity.yaw
client.selectDispatcher().SendCreateEntityOnClient(client.gateid, client.clientid, entity.TypeName, entity.ID, isPlayer,
clientData, float32(pos.X), float32(pos.Y), float32(pos.Z), float32(yaw))
}
func (client *GameClient) sendDestroyEntity(entity *Entity) {
if client != nil {
client.selectDispatcher().SendDestroyEntityOnClient(client.gateid, client.clientid, entity.TypeName, entity.ID)
}
}
func (client *GameClient) call(entityID common.EntityID, method string, args []interface{}) {
if client != nil {
client.selectDispatcher().SendCallEntityMethodOnClient(client.gateid, client.clientid, entityID, method, args)
}
}
// sendNotifyMapAttrChange updates MapAttr change to Client entity
func (client *GameClient) sendNotifyMapAttrChange(entityID common.EntityID, path []interface{}, key string, val interface{}) {
if client != nil {
client.selectDispatcher().SendNotifyMapAttrChangeOnClient(client.gateid, client.clientid, entityID, path, key, val)
}
}
// sendNotifyMapAttrDel updates MapAttr delete to Client entity
func (client *GameClient) sendNotifyMapAttrDel(entityID common.EntityID, path []interface{}, key string) {
if client != nil {
client.selectDispatcher().SendNotifyMapAttrDelOnClient(client.gateid, client.clientid, entityID, path, key)
}
}
func (client *GameClient) sendNotifyMapAttrClear(entityID common.EntityID, path []interface{}) {
if client != nil {
client.selectDispatcher().SendNotifyMapAttrClearOnClient(client.gateid, client.clientid, entityID, path)
}
}
// sendNotifyListAttrChange notifies Client of ListAttr item changing
func (client *GameClient) sendNotifyListAttrChange(entityID common.EntityID, path []interface{}, index uint32, val interface{}) {
if client != nil {
client.selectDispatcher().SendNotifyListAttrChangeOnClient(client.gateid, client.clientid, entityID, path, index, val)
}
}
// sendNotifyListAttrPop notify Client of ListAttr popping
func (client *GameClient) sendNotifyListAttrPop(entityID common.EntityID, path []interface{}) {
if client != nil {
client.selectDispatcher().SendNotifyListAttrPopOnClient(client.gateid, client.clientid, entityID, path)
}
}
// sendNotifyListAttrAppend notify entity of ListAttr appending
func (client *GameClient) sendNotifyListAttrAppend(entityID common.EntityID, path []interface{}, val interface{}) {
if client != nil {
client.selectDispatcher().SendNotifyListAttrAppendOnClient(client.gateid, client.clientid, entityID, path, val)
}
}
func (client *GameClient) sendSetClientFilterProp(key, val string) {
if client != nil {
client.selectDispatcher().SendSetClientFilterProp(client.gateid, client.clientid, key, val)
}
}
func (client *GameClient) selectDispatcher() *dispatcherclient.DispatcherClient {
if consts.DEBUG_MODE {
if client.ownerid == "" {
gwlog.Panicf("%s select dispatcher failed: ownerid is nil", client)
}
}
return dispatchercluster.SelectByEntityID(client.ownerid)
}