From 159be740a8065c904f5a291d64728ee3857f6362 Mon Sep 17 00:00:00 2001 From: aceld Date: Mon, 15 Apr 2019 11:31:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=20=E4=BF=AE=E6=AD=A3=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zinx_app_demo/mmo_game/core/aoi.go | 2 +- zinx_app_demo/mmo_game/core/player.go | 104 +++++++++++++------------- znet/connection.go | 6 +- 3 files changed, 55 insertions(+), 57 deletions(-) diff --git a/zinx_app_demo/mmo_game/core/aoi.go b/zinx_app_demo/mmo_game/core/aoi.go index 5498dcc..38f99cd 100644 --- a/zinx_app_demo/mmo_game/core/aoi.go +++ b/zinx_app_demo/mmo_game/core/aoi.go @@ -143,7 +143,7 @@ func (m *AOIManager) GetPidsByPos(x, y float32) (playerIDs []int) { grids := m.GetSurroundGridsByGid(gID) for _, v := range grids { playerIDs = append(playerIDs, v.GetPlyerIDs()...) - fmt.Printf("===> grid ID : %d, pids : %v ====", v.GID, v.GetPlyerIDs()) + //fmt.Printf("===> grid ID : %d, pids : %v ====", v.GID, v.GetPlyerIDs()) } return diff --git a/zinx_app_demo/mmo_game/core/player.go b/zinx_app_demo/mmo_game/core/player.go index 4e12bf7..1e00379 100644 --- a/zinx_app_demo/mmo_game/core/player.go +++ b/zinx_app_demo/mmo_game/core/player.go @@ -11,35 +11,35 @@ import ( //玩家对象 type Player struct { - Pid int32 //玩家ID + Pid int32 //玩家ID Conn ziface.IConnection //当前玩家的连接 - X float32 //平面x坐标 - Y float32 //高度 - Z float32 //平面y坐标 (注意不是Y) - V float32 //旋转0-360度 + X float32 //平面x坐标 + Y float32 //高度 + Z float32 //平面y坐标 (注意不是Y) + V float32 //旋转0-360度 } /* Player ID 生成器 - */ -var PidGen int32 = 1 //用来生成玩家ID的计数器 -var IdLock sync.Mutex //保护PidGen的互斥机制 +*/ +var PidGen int32 = 1 //用来生成玩家ID的计数器 +var IdLock sync.Mutex //保护PidGen的互斥机制 //创建一个玩家对象 func NewPlayer(conn ziface.IConnection) *Player { //生成一个PID IdLock.Lock() id := PidGen - PidGen ++ + PidGen++ IdLock.Unlock() p := &Player{ - Pid : id, - Conn:conn, - X:float32(160 + rand.Intn(10)),//随机在160坐标点 基于X轴偏移若干坐标 - Y:0, //高度为0 - Z:float32(134 + rand.Intn(17)), //随机在134坐标点 基于Y轴偏移若干坐标 - V:0, //角度为0,尚未实现 + Pid: id, + Conn: conn, + X: float32(160 + rand.Intn(10)), //随机在160坐标点 基于X轴偏移若干坐标 + Y: 0, //高度为0 + Z: float32(134 + rand.Intn(17)), //随机在134坐标点 基于Y轴偏移若干坐标 + V: 0, //角度为0,尚未实现 } return p @@ -49,7 +49,7 @@ func NewPlayer(conn ziface.IConnection) *Player { func (p *Player) SyncPid() { //组建MsgId0 proto数据 data := &pb.SyncPid{ - Pid:p.Pid, + Pid: p.Pid, } //发送数据给客户端 @@ -61,14 +61,14 @@ func (p *Player) BroadCastStartPosition() { //组建MsgId200 proto数据 msg := &pb.BroadCast{ - Pid:p.Pid, - Tp:2,//TP2 代表广播坐标 + Pid: p.Pid, + Tp: 2, //TP2 代表广播坐标 Data: &pb.BroadCast_P{ - P:&pb.Position{ - X:p.X, - Y:p.Y, - Z:p.Z, - V:p.V, + P: &pb.Position{ + X: p.X, + Y: p.Y, + Z: p.Z, + V: p.V, }, }, } @@ -89,14 +89,14 @@ func (p *Player) SyncSurrounding() { } //3.1 组建MsgId200 proto数据 msg := &pb.BroadCast{ - Pid:p.Pid, - Tp:2,//TP2 代表广播坐标 + Pid: p.Pid, + Tp: 2, //TP2 代表广播坐标 Data: &pb.BroadCast_P{ - P:&pb.Position{ - X:p.X, - Y:p.Y, - Z:p.Z, - V:p.V, + P: &pb.Position{ + X: p.X, + Y: p.Y, + Z: p.Z, + V: p.V, }, }, } @@ -109,12 +109,12 @@ func (p *Player) SyncSurrounding() { playersData := make([]*pb.Player, 0, len(players)) for _, player := range players { p := &pb.Player{ - Pid:player.Pid, - P:&pb.Position{ - X:player.X, - Y:player.Y, - Z:player.Z, - V:player.V, + Pid: player.Pid, + P: &pb.Position{ + X: player.X, + Y: player.Y, + Z: player.Z, + V: player.V, }, } playersData = append(playersData, p) @@ -122,7 +122,7 @@ func (p *Player) SyncSurrounding() { //4.2 封装SyncPlayer protobuf数据 SyncPlayersMsg := &pb.SyncPlayers{ - Ps:playersData[:], + Ps: playersData[:], } //4.3 给当前玩家发送需要显示周围的全部玩家数据 @@ -133,8 +133,8 @@ func (p *Player) SyncSurrounding() { func (p *Player) Talk(content string) { //1. 组建MsgId200 proto数据 msg := &pb.BroadCast{ - Pid:p.Pid, - Tp:1,//TP 1 代表聊天广播 + Pid: p.Pid, + Tp: 1, //TP 1 代表聊天广播 Data: &pb.BroadCast_Content{ Content: content, }, @@ -149,7 +149,6 @@ func (p *Player) Talk(content string) { } } - //广播玩家位置移动 func (p *Player) UpdatePos(x float32, y float32, z float32, v float32) { //更新玩家的位置信息 @@ -160,14 +159,14 @@ func (p *Player) UpdatePos(x float32, y float32, z float32, v float32) { //组装protobuf协议,发送位置给周围玩家 msg := &pb.BroadCast{ - Pid:p.Pid, - Tp:4, //4- 移动之后的坐标信息 + 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, + P: &pb.Position{ + X: p.X, + Y: p.Y, + Z: p.Z, + V: p.V, }, }, } @@ -201,7 +200,7 @@ func (p *Player) LostConnection() { //2 封装MsgID:201消息 msg := &pb.SyncPid{ - Pid:p.Pid, + Pid: p.Pid, } //3 向周围玩家发送消息 @@ -214,20 +213,19 @@ func (p *Player) LostConnection() { WorldMgrObj.RemovePlayerByPid(p.Pid) } - /* 发送消息给客户端, 主要是将pb的protobuf数据序列化之后发送 - */ +*/ func (p *Player) SendMsg(msgId uint32, data proto.Message) { - fmt.Printf("before Marshal data = %+v\n", data) + //fmt.Printf("before Marshal data = %+v\n", data) //将proto Message结构体序列化 msg, err := proto.Marshal(data) if err != nil { fmt.Println("marshal msg err: ", err) return } - fmt.Printf("after Marshal data = %+v\n", msg) + //fmt.Printf("after Marshal data = %+v\n", msg) if p.Conn == nil { fmt.Println("connection in player is nil") @@ -241,4 +239,4 @@ func (p *Player) SendMsg(msgId uint32, data proto.Message) { } return -} \ No newline at end of file +} diff --git a/znet/connection.go b/znet/connection.go index 00a8278..ee772f0 100644 --- a/znet/connection.go +++ b/znet/connection.go @@ -29,7 +29,7 @@ type Connection struct { msgBuffChan chan []byte //链接属性 - property map[string]interface{} + property map[string]interface{} //保护链接属性修改的锁 propertyLock sync.RWMutex } @@ -69,7 +69,7 @@ func (c *Connection) StartWriter() { fmt.Println("Send Data error:, ", err, " Conn Writer exit") return } - fmt.Printf("Send data succ! data = %+v\n", data) + //fmt.Printf("Send data succ! data = %+v\n", data) case data, ok := <-c.msgBuffChan: if ok { //有数据要写给客户端 @@ -241,7 +241,7 @@ func (c *Connection) GetProperty(key string) (interface{}, error) { c.propertyLock.RLock() defer c.propertyLock.RUnlock() - if value, ok := c.property[key]; ok { + if value, ok := c.property[key]; ok { return value, nil } else { return nil, errors.New("no property found")