Skip to content

Commit

Permalink
同步玩家位置
Browse files Browse the repository at this point in the history
  • Loading branch information
liuligames committed Sep 1, 2021
1 parent cd7985e commit 461731a
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 13 deletions.
32 changes: 32 additions & 0 deletions mmo/api/Move.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package api

import (
"fmt"
"github.com/golang/protobuf/proto"
"wukong/iface"
"wukong/mmo/core"
"wukong/mmo/pb/msg"
"wukong/net"
)

type MoveApi struct {
net.BaseRouter
}

func (m *MoveApi) Handle(request iface.IRequest) {
protoMag := &msg.Position{}
err := proto.Unmarshal(request.GetData(), protoMag)
if err != nil {
fmt.Println("Move : Position Unmarshal error", err)
return
}

pId, err := request.GetConnection().GetProperty("PId")
if err != nil {
fmt.Println("GetProperty pId error", err)
return
}

player := core.WorldManagerObj.GetPlayerByPId(pId.(int32))
player.UpdatePos(protoMag.X, protoMag.Y, protoMag.Z, protoMag.V)
}
83 changes: 83 additions & 0 deletions mmo/core/Player.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,86 @@ func (p *Player) Talk(content string) {
}

}

func (p *Player) SyncSurrounding() {
pIds := WorldManagerObj.AoiManager.GetPIdsByPos(p.X, p.Z)
players := make([]*Player, 0, len(pIds))
for _, pId := range pIds {
players = append(players, WorldManagerObj.GetPlayerByPId(int32(pId)))
}

protoMsg := &msg.BroadCast{
PId: p.PId,
Tp: 2,
Data: &msg.BroadCast_P{
P: &msg.Position{
X: p.X,
Y: p.Y,
Z: p.Z,
V: p.V,
},
},
}
for _, player := range players {
player.SendMsg(200, protoMsg)
}

playersProtoMsg := make([]*msg.Player, 0, len(players))
for _, player := range players {
p := &msg.Player{
PId: player.PId,
P: &msg.Position{
X: player.X,
Y: player.Y,
Z: player.Z,
V: player.V,
},
}

playersProtoMsg = append(playersProtoMsg, p)
}

syncPlayersProtoMsg := &msg.SyncPlayers{
Ps: playersProtoMsg,
}

p.SendMsg(202, syncPlayersProtoMsg)

}

func (p *Player) UpdatePos(x float32, y float32, z float32, v float32) {
p.X = x
p.Y = y
p.Z = z
p.V = v

protoMsg := &msg.BroadCast{
PId: p.PId,
Tp: 4,
Data: &msg.BroadCast_P{
P: &msg.Position{
X: x,
Y: y,
Z: z,
V: v,
},
},
}

players := p.GetSurroundingPlayers()

for _, Player := range players {
Player.SendMsg(200, protoMsg)
}
}

func (p Player) GetSurroundingPlayers() []*Player {
pIds := WorldManagerObj.AoiManager.GetPIdsByPos(p.X, p.Z)

players := make([]*Player, 0, len(pIds))
for _, pid := range pIds {
players = append(players, WorldManagerObj.GetPlayerByPId(int32(pid)))
}

return players
}
3 changes: 3 additions & 0 deletions mmo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func OnConnectionAdd(conn iface.IConnection) {

conn.SetProperty("PId", player.PId)

player.SyncSurrounding()

fmt.Println("===========》 Player id = ", player.PId, "is arrived ========")
}

Expand All @@ -28,6 +30,7 @@ func main() {
s.SetOnConnStart(OnConnectionAdd)

s.AddRouter(2, &api.WorldChatApi{})
s.AddRouter(3, &api.MoveApi{})

s.Serve()
}
11 changes: 11 additions & 0 deletions mmo/pb/msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,15 @@ message Talk{
string name = 2;
}

//同步玩家显示数据
message SyncPlayers{
repeated Player ps = 1;
}

//玩家信息
message Player{
int32 PId = 1;
Position P = 2;
}


165 changes: 152 additions & 13 deletions mmo/pb/msg/msg.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 461731a

Please sign in to comment.