aceld
6 years ago
7 changed files with 389 additions and 38 deletions
-
38zinx_app_demo/mmo_game/api/world_chat.go
-
27zinx_app_demo/mmo_game/core/aoi.go
-
92zinx_app_demo/mmo_game/core/player.go
-
68zinx_app_demo/mmo_game/core/world_manager.go
-
165zinx_app_demo/mmo_game/pb/msg.pb.go
-
22zinx_app_demo/mmo_game/pb/msg.proto
-
15zinx_app_demo/mmo_game/server.go
@ -0,0 +1,38 @@ |
|||||
|
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 WorldChatApi struct { |
||||
|
znet.BaseRouter |
||||
|
} |
||||
|
|
||||
|
func (*WorldChatApi) Handle(request ziface.IRequest) { |
||||
|
//1. 将客户端传来的proto协议解码
|
||||
|
msg := &pb.Talk{} |
||||
|
err := proto.Unmarshal(request.GetData(), msg) |
||||
|
if err != nil { |
||||
|
fmt.Println("Talk 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 |
||||
|
} |
||||
|
//3. 根据pid得到player对象
|
||||
|
player := core.WorldMgrObj.GetPlayerByPid(pid.(int32)) |
||||
|
|
||||
|
//4. 让player对象发起聊天广播请求
|
||||
|
player.Talk(msg.Content) |
||||
|
} |
@ -0,0 +1,68 @@ |
|||||
|
package core |
||||
|
|
||||
|
import ( |
||||
|
"sync" |
||||
|
) |
||||
|
|
||||
|
/* |
||||
|
当前游戏世界的总管理模块 |
||||
|
*/ |
||||
|
type WorldManager struct { |
||||
|
AoiMgr *AOIManager //当前世界地图的AOI规划管理器
|
||||
|
Players map[int32]*Player //当前在线的玩家集合
|
||||
|
pLock sync.RWMutex //保护Players的互斥读写机制
|
||||
|
} |
||||
|
|
||||
|
//提供一个对外的世界管理模块句柄
|
||||
|
var WorldMgrObj *WorldManager |
||||
|
|
||||
|
//提供WorldManager 初始化方法
|
||||
|
func init() { |
||||
|
WorldMgrObj = &WorldManager{ |
||||
|
Players: make(map[int32]*Player), |
||||
|
AoiMgr: NewAOIManager(AOI_MIN_X, AOI_MAX_X, AOI_CNTS_X, AOI_MIN_Y, AOI_MAX_Y, AOI_CNTS_Y), |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//提供添加一个玩家的的功能,将玩家添加进玩家信息表Players
|
||||
|
func (wm *WorldManager) AddPlayer(player *Player) { |
||||
|
//将player添加到 世界管理器中
|
||||
|
wm.pLock.Lock() |
||||
|
wm.Players[player.Pid] = player |
||||
|
wm.pLock.Unlock() |
||||
|
|
||||
|
//将player 添加到AOI网络规划中
|
||||
|
wm.AoiMgr.AddToGridByPos(int(player.Pid), player.X, player.Z) |
||||
|
} |
||||
|
|
||||
|
//从玩家信息表中移除一个玩家
|
||||
|
func (wm *WorldManager) RemovePlayerByPid(pid int32) { |
||||
|
wm.pLock.Lock() |
||||
|
delete(wm.Players, pid) |
||||
|
wm.pLock.Unlock() |
||||
|
} |
||||
|
|
||||
|
//通过玩家ID 获取对应玩家信息
|
||||
|
func (wm *WorldManager) GetPlayerByPid(pid int32) *Player { |
||||
|
wm.pLock.RLock() |
||||
|
defer wm.pLock.RUnlock() |
||||
|
|
||||
|
return wm.Players[pid] |
||||
|
} |
||||
|
|
||||
|
//获取所有玩家的信息
|
||||
|
func (wm *WorldManager) GetAllPlayers() []*Player { |
||||
|
wm.pLock.RLock() |
||||
|
defer wm.pLock.RUnlock() |
||||
|
|
||||
|
//创建返回的player集合切片
|
||||
|
players := make([]*Player, 0) |
||||
|
|
||||
|
//添加切片
|
||||
|
for _, v := range wm.Players { |
||||
|
players = append(players, v) |
||||
|
} |
||||
|
|
||||
|
//返回
|
||||
|
return players |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue