//MakeMove 0 public async Task MakeMove(int id, CGPlayerCmd cmd) //有玩家走了一步,更新数据,广播给所有玩家,开始下一手 { await _messageQueue; var pos = Convert.ToInt32(cmd.Cmd.Split(' ')[1]); chessState[pos] = id; curTurn++; var playerWho = curTurn % 2; var p = players[playerWho]; var cmd2 = GCPlayerCmd.CreateBuilder(); cmd2.Result = string.Format("MakeMove {0} {1}", id, pos); BroadcastToAll(cmd2); if (CheckWin()) { await GameOver(); } else { var cmd3 = GCPlayerCmd.CreateBuilder(); cmd3.Result = string.Format("NewTurn {0} {1}", curTurn, p.Id); BroadcastToAll(cmd3); } }
public async Task GameStart() //游戏开始 { await _messageQueue; Console.WriteLine($"GameStart RoomID : {Id}"); var cmd = GCPlayerCmd.CreateBuilder(); //发送GameStart消息给所有玩家 cmd.Result = "GameStart"; BroadcastToAll(cmd); var cmd1 = GCPlayerCmd.CreateBuilder(); //第一手,放入玩家的唯一ID cmd1.Result = string.Format("NewTurn {0} {1}", curTurn, players[0].Id); BroadcastToAll(cmd1); //try //{ // var cmd1 = GCPlayerCmd.CreateBuilder(); // string str = "Position"; // foreach (var p in players) // { // str = $"{str} {p.Id} {p.playerState.px} {p.playerState.py} {p.playerState.hp}"; // } // cmd1.Result = str; // BroadcastToAll(cmd1); //} //catch (Exception e) //{ // Console.WriteLine(e); //} //RunTask(UpdateWorld); }
public override void InitSync(AvatarInfo info) { if (info != null) { var gc = GCPlayerCmd.CreateBuilder(); gc.AvatarInfo = info; var cmd = gc.Build(); NetworkAttribute(cmd); } }
private void InitPlayerId() //初始化客户端Player的ID,为每个Actor的专属ID,由ActorManager统一管理 { var cmd = GCPlayerCmd.CreateBuilder(); cmd.Result = string.Format("Init {0}", Id); ServerBundle bundle; var bytes = ServerBundle.sendImmediateError(cmd, 0, 0, out bundle); ServerBundle.ReturnBundle(bundle); agent.SendBytes(bytes); }
private async Task GameOver() { await _messageQueue; if (!isGameOver) { isGameOver = true; var cmd = GCPlayerCmd.CreateBuilder(); cmd.Result = "GameOver"; BroadcastToAll(cmd); foreach (var p in players) { await p.SetRoom(null); } ActorManager.Instance.RemoveActor(Id); //删除该房间 } }
private void SyncWorldState()//同步状态给所有玩家: { //玩家位置同步更新 var cmd1 = GCPlayerCmd.CreateBuilder(); string str = "Position"; foreach (var p in players) { str = $"{str} {p.Id} {p.playerState.px} {p.playerState.py} {p.playerState.hp}"; } cmd1.Result = str; BroadcastToAll(cmd1); //技能位置同步 var cmd2 = GCPlayerCmd.CreateBuilder(); var str2 = "Bullet"; foreach (var bulletPair in bulletMap) { str2 = $"{str2} {bulletPair.Value.id} {bulletPair.Value.px} {bulletPair.Value.py}"; } cmd2.Result = str2; BroadcastToAll(cmd2); }