Browse Source

代码格式化

修正日志
master
aceld 6 years ago
parent
commit
159be740a8
  1. 2
      zinx_app_demo/mmo_game/core/aoi.go
  2. 104
      zinx_app_demo/mmo_game/core/player.go
  3. 6
      znet/connection.go

2
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) grids := m.GetSurroundGridsByGid(gID)
for _, v := range grids { for _, v := range grids {
playerIDs = append(playerIDs, v.GetPlyerIDs()...) 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 return

104
zinx_app_demo/mmo_game/core/player.go

@ -11,35 +11,35 @@ import (
//玩家对象 //玩家对象
type Player struct { type Player struct {
Pid int32 //玩家ID
Pid int32 //玩家ID
Conn ziface.IConnection //当前玩家的连接 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 生成器 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 { func NewPlayer(conn ziface.IConnection) *Player {
//生成一个PID //生成一个PID
IdLock.Lock() IdLock.Lock()
id := PidGen id := PidGen
PidGen ++
PidGen++
IdLock.Unlock() IdLock.Unlock()
p := &Player{ 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 return p
@ -49,7 +49,7 @@ func NewPlayer(conn ziface.IConnection) *Player {
func (p *Player) SyncPid() { func (p *Player) SyncPid() {
//组建MsgId0 proto数据 //组建MsgId0 proto数据
data := &pb.SyncPid{ data := &pb.SyncPid{
Pid:p.Pid,
Pid: p.Pid,
} }
//发送数据给客户端 //发送数据给客户端
@ -61,14 +61,14 @@ func (p *Player) BroadCastStartPosition() {
//组建MsgId200 proto数据 //组建MsgId200 proto数据
msg := &pb.BroadCast{ msg := &pb.BroadCast{
Pid:p.Pid,
Tp:2,//TP2 代表广播坐标
Pid: p.Pid,
Tp: 2, //TP2 代表广播坐标
Data: &pb.BroadCast_P{ 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数据 //3.1 组建MsgId200 proto数据
msg := &pb.BroadCast{ msg := &pb.BroadCast{
Pid:p.Pid,
Tp:2,//TP2 代表广播坐标
Pid: p.Pid,
Tp: 2, //TP2 代表广播坐标
Data: &pb.BroadCast_P{ 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)) playersData := make([]*pb.Player, 0, len(players))
for _, player := range players { for _, player := range players {
p := &pb.Player{ 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) playersData = append(playersData, p)
@ -122,7 +122,7 @@ func (p *Player) SyncSurrounding() {
//4.2 封装SyncPlayer protobuf数据 //4.2 封装SyncPlayer protobuf数据
SyncPlayersMsg := &pb.SyncPlayers{ SyncPlayersMsg := &pb.SyncPlayers{
Ps:playersData[:],
Ps: playersData[:],
} }
//4.3 给当前玩家发送需要显示周围的全部玩家数据 //4.3 给当前玩家发送需要显示周围的全部玩家数据
@ -133,8 +133,8 @@ func (p *Player) SyncSurrounding() {
func (p *Player) Talk(content string) { func (p *Player) Talk(content string) {
//1. 组建MsgId200 proto数据 //1. 组建MsgId200 proto数据
msg := &pb.BroadCast{ msg := &pb.BroadCast{
Pid:p.Pid,
Tp:1,//TP 1 代表聊天广播
Pid: p.Pid,
Tp: 1, //TP 1 代表聊天广播
Data: &pb.BroadCast_Content{ Data: &pb.BroadCast_Content{
Content: 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) { 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协议,发送位置给周围玩家 //组装protobuf协议,发送位置给周围玩家
msg := &pb.BroadCast{ msg := &pb.BroadCast{
Pid:p.Pid,
Tp:4, //4- 移动之后的坐标信息
Pid: p.Pid,
Tp: 4, //4- 移动之后的坐标信息
Data: &pb.BroadCast_P{ 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消息 //2 封装MsgID:201消息
msg := &pb.SyncPid{ msg := &pb.SyncPid{
Pid:p.Pid,
Pid: p.Pid,
} }
//3 向周围玩家发送消息 //3 向周围玩家发送消息
@ -214,20 +213,19 @@ func (p *Player) LostConnection() {
WorldMgrObj.RemovePlayerByPid(p.Pid) WorldMgrObj.RemovePlayerByPid(p.Pid)
} }
/* /*
发送消息给客户端 发送消息给客户端
主要是将pb的protobuf数据序列化之后发送 主要是将pb的protobuf数据序列化之后发送
*/
*/
func (p *Player) SendMsg(msgId uint32, data proto.Message) { 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结构体序列化 //将proto Message结构体序列化
msg, err := proto.Marshal(data) msg, err := proto.Marshal(data)
if err != nil { if err != nil {
fmt.Println("marshal msg err: ", err) fmt.Println("marshal msg err: ", err)
return return
} }
fmt.Printf("after Marshal data = %+v\n", msg)
//fmt.Printf("after Marshal data = %+v\n", msg)
if p.Conn == nil { if p.Conn == nil {
fmt.Println("connection in player is nil") fmt.Println("connection in player is nil")
@ -241,4 +239,4 @@ func (p *Player) SendMsg(msgId uint32, data proto.Message) {
} }
return return
}
}

6
znet/connection.go

@ -29,7 +29,7 @@ type Connection struct {
msgBuffChan chan []byte msgBuffChan chan []byte
//链接属性 //链接属性
property map[string]interface{}
property map[string]interface{}
//保护链接属性修改的锁 //保护链接属性修改的锁
propertyLock sync.RWMutex propertyLock sync.RWMutex
} }
@ -69,7 +69,7 @@ func (c *Connection) StartWriter() {
fmt.Println("Send Data error:, ", err, " Conn Writer exit") fmt.Println("Send Data error:, ", err, " Conn Writer exit")
return return
} }
fmt.Printf("Send data succ! data = %+v\n", data)
//fmt.Printf("Send data succ! data = %+v\n", data)
case data, ok := <-c.msgBuffChan: case data, ok := <-c.msgBuffChan:
if ok { if ok {
//有数据要写给客户端 //有数据要写给客户端
@ -241,7 +241,7 @@ func (c *Connection) GetProperty(key string) (interface{}, error) {
c.propertyLock.RLock() c.propertyLock.RLock()
defer c.propertyLock.RUnlock() defer c.propertyLock.RUnlock()
if value, ok := c.property[key]; ok {
if value, ok := c.property[key]; ok {
return value, nil return value, nil
} else { } else {
return nil, errors.New("no property found") return nil, errors.New("no property found")

Loading…
Cancel
Save