Browse Source

添加移动同步功能

master
aceld 6 years ago
parent
commit
ee7bd8dca2
  1. 42
      zinx_app_demo/mmo_game/api/move.go
  2. 45
      zinx_app_demo/mmo_game/core/player.go
  3. 2
      zinx_app_demo/mmo_game/pb/msg.proto
  4. 1
      zinx_app_demo/mmo_game/server.go

42
zinx_app_demo/mmo_game/api/move.go

@ -0,0 +1,42 @@
package api
import (
"fmt"
"github.com/golang/protobuf/proto"
"zinx/ziface"
"zinx/zinx_app_demo/mmo_game/core"
"zinx/zinx_app_demo/mmo_game/pb"
"zinx/znet"
)
//玩家移动
type MoveApi struct {
znet.BaseRouter
}
func (*MoveApi) Handle(request ziface.IRequest) {
//1. 将客户端传来的proto协议解码
msg := &pb.Position{}
err := proto.Unmarshal(request.GetData(), msg)
if err != nil {
fmt.Println("Move: Position Unmarshal error ", err)
return
}
//2. 得知当前的消息是从哪个玩家传递来的,从连接属性pid中获取
pid, err := request.GetConnection().GetProperty("pid")
if err != nil {
fmt.Println("GetProperty pid error", err)
request.GetConnection().Stop()
return
}
fmt.Printf("user pid = %d , move(%f,%f,%f,%f)", pid, msg.X, msg.Y, msg.Z, msg.V)
//3. 根据pid得到player对象
player := core.WorldMgrObj.GetPlayerByPid(pid.(int32))
//4. 让player对象发起移动位置信息广播
player.UpdatePos(msg.X, msg.Y, msg.Z, msg.V)
}

45
zinx_app_demo/mmo_game/core/player.go

@ -150,6 +150,51 @@ func (p *Player) Talk(content string) {
} }
//广播玩家位置移动
func (p *Player) UpdatePos(x float32, y float32, z float32, v float32) {
//更新玩家的位置信息
p.X = x
p.Y = y
p.Z = z
p.V = v
//组装protobuf协议,发送位置给周围玩家
msg := &pb.BroadCast{
Pid:p.Pid,
Tp:4, //4- 移动之后的坐标信息
Data: &pb.BroadCast_P{
P:&pb.Position{
X:p.X,
Y:p.Y,
Z:p.Z,
V:p.V,
},
},
}
//获取当前玩家周边全部玩家
players := p.GetSurroundingPlayers()
//向周边的每个玩家发送MsgID:200消息,移动位置更新消息
for _, player := range players {
player.SendMsg(200, msg)
}
}
//获得当前玩家的AOI周边玩家信息
func (p *Player) GetSurroundingPlayers() []*Player {
//得到当前AOI区域的所有pid
pids := WorldMgrObj.AoiMgr.GetPidsByPos(p.X, p.Z)
//将所有pid对应的Player放到Player切片中
players := make([]*Player, 0, len(pids))
for _, pid := range pids {
players = append(players, WorldMgrObj.GetPlayerByPid(int32(pid)))
}
return players
}
/* /*
发送消息给客户端 发送消息给客户端
主要是将pb的protobuf数据序列化之后发送 主要是将pb的protobuf数据序列化之后发送

2
zinx_app_demo/mmo_game/pb/msg.proto

@ -18,7 +18,7 @@ message Position{
//广 //广
message BroadCast{ message BroadCast{
int32 Pid=1; int32 Pid=1;
int32 Tp=2; //1- 2-
int32 Tp=2; //1- 2- 3 4
oneof Data { oneof Data {
string Content=3; // string Content=3; //
Position P=4; //广 Position P=4; //广

1
zinx_app_demo/mmo_game/server.go

@ -35,6 +35,7 @@ func main() {
//注册路由 //注册路由
s.AddRouter(2, &api.WorldChatApi{}) s.AddRouter(2, &api.WorldChatApi{})
s.AddRouter(3, &api.MoveApi{})
//启动服务 //启动服务
s.Serve() s.Serve()

Loading…
Cancel
Save