MCPcopy
hub / github.com/aceld/zinx / SyncSurrounding

Method SyncSurrounding

zinx_app_demo/mmo_game/core/player.go:87–150  ·  view source on GitHub ↗

SyncSurrounding Broadcast the player's position to the surrounding players in the same grid 给当前玩家周边的(九宫格内)玩家广播自己的位置,让他们显示自己

()

Source from the content-addressed store, hash-verified

85// SyncSurrounding Broadcast the player's position to the surrounding players in the same grid
86// 给当前玩家周边的(九宫格内)玩家广播自己的位置,让他们显示自己
87func (p *Player) SyncSurrounding() {
88 //1 Get pIDs of players in the surrounding nine grids based on the player's position
89 // 根据自己的位置,获取周围九宫格内的玩家pID
90 pIDs := WorldMgrObj.AoiMgr.GetPIDsByPos(p.X, p.Z)
91
92 // 2 Get all player objects based on the pIDs
93 // 根据pID得到所有玩家对象
94 players := make([]*Player, 0, len(pIDs))
95
96 // 3 Send MsgID:200 message to these players to display themselves in each other's views
97 // 给这些玩家发送MsgID:200消息,让自己出现在对方视野中
98 for _, pID := range pIDs {
99 players = append(players, WorldMgrObj.GetPlayerByPID(int32(pID)))
100 }
101
102 // 3.1 Assemble MsgID200 proto data
103 // 组建MsgID200 proto数据
104 msg := &pb.BroadCast{
105 PID: p.PID,
106 Tp: 2, //TP:2 represents broadcasting coordinates (广播坐标)
107 Data: &pb.BroadCast_P{
108 P: &pb.Position{
109 X: p.X,
110 Y: p.Y,
111 Z: p.Z,
112 V: p.V,
113 },
114 },
115 }
116
117 // 3.2 Send the 200 message to each player's client to display characters
118 // 每个玩家分别给对应的客户端发送200消息,显示人物
119 for _, player := range players {
120 player.SendMsg(200, msg)
121 }
122 // 4 Make surrounding players in the nine grids appear in the player's view
123 // 让周围九宫格内的玩家出现在自己的视野中
124
125 // 4.1 Create Message SyncPlayers data
126 // 制作Message SyncPlayers 数据
127 playersData := make([]*pb.Player, 0, len(players))
128 for _, player := range players {
129 p := &pb.Player{
130 PID: player.PID,
131 P: &pb.Position{
132 X: player.X,
133 Y: player.Y,
134 Z: player.Z,
135 V: player.V,
136 },
137 }
138 playersData = append(playersData, p)
139 }
140
141 // 4.2 Encapsulate SyncPlayers protobuf data
142 // 封装SyncPlayer protobuf数据
143 SyncPlayersMsg := &pb.SyncPlayers{
144 Ps: playersData[:],

Callers 1

OnConnectionAddFunction · 0.95

Calls 4

SendMsgMethod · 0.95
GetPIDsByPosMethod · 0.80
GetPlayerByPIDMethod · 0.80
SendMsgMethod · 0.65

Tested by

no test coverage detected