/// <summary> /// 获得房间信息 /// </summary> /// <returns></returns> public GameMessage GetRoomInfo() { GameMessage message = new GameMessage(); message.type = BitConverter.GetBytes((int)Protocol.GetRoomInfo); List <RoomPlayer> roomPlayers = new List <RoomPlayer>(); //每个玩家的信息 foreach (Player p in playerDic.Values) { RoomPlayer roomPlayer = new RoomPlayer(); PlayerInfo playerinfo = new PlayerInfo(); playerinfo.id = p.id; playerinfo.win = p.data.win; playerinfo.defeat = p.data.defeat; roomPlayer.playerinfo = playerinfo; roomPlayer.team = p.tempData.team; int isOwner = p.tempData.isOwner ? 1 : 0; roomPlayer.isOwner = isOwner; roomPlayers.Add(roomPlayer); } message.data = ProtoTransfer.Serialize <List <RoomPlayer> >(roomPlayers); return(message); }
/// <summary> /// 开始战斗 /// </summary> public void StartFight() { scoreT1 = 0; scoreT2 = 0; status = Status.Fight; GameMessage msg = new GameMessage(); msg.type = BitConverter.GetBytes((int)Protocol.Fight); var battleinfos = new List <BattlePlayer>(); // 开始计时 battleTimer.Elapsed += (sender, e) => { int winTeam = scoreT1 >= scoreT2 ? 1 : 2; UpdateWin(winTeam); }; battleTimer.AutoReset = false; battleTimer.Enabled = true; int teamPos1 = 1; int teamPos2 = 1; lock (playerDic) { foreach (Player p in playerDic.Values) { BattlePlayer battlePlayer = new BattlePlayer(); p.tempData.hp = 100; battlePlayer.id = p.id; battlePlayer.team = p.tempData.team; if (p.tempData.team == 1) { p.tempData.swopId = teamPos1; battlePlayer.swopId = teamPos1++; } else { p.tempData.swopId = teamPos2; battlePlayer.swopId = teamPos2++; } battlePlayer.playerHp = (int)p.tempData.hp; p.tempData.status = PlayerTempData.Status.Fight; battleinfos.Add(battlePlayer); } // 对局时间 byte[] battlebytes = BitConverter.GetBytes(battleLimitTime); byte[] tempBytes = ProtoTransfer.Serialize(battleinfos); byte[] sendbytes = new byte[tempBytes.Length + battlebytes.Length]; Array.Copy(battlebytes, 0, sendbytes, 0, battlebytes.Length); Array.Copy(tempBytes, 0, sendbytes, battlebytes.Length, tempBytes.Length); msg.data = sendbytes; //广播Fight协议 BroadcastTcp(msg); // GameStar(); } }
/// <summary> /// 获得房间列表 /// </summary> /// <returns></returns> public GameMessage GetRoomList() { GameMessage msg = new GameMessage(); msg.type = BitConverter.GetBytes((int)Protocol.GetRoomList); int count = roomList.Count; //房间数量 List <RoomInfo> roomInfos = new List <RoomInfo>(); int cout = roomList.Count; for (int i = 0; i < count; i++) { Room room = roomList[i]; RoomInfo roominfo = new RoomInfo(); roominfo.playercount = room.playerDic.Count; roominfo.roomstatus = (int)room.status; roomInfos.Add(roominfo); } msg.data = ProtoTransfer.Serialize <List <RoomInfo> >(roomInfos); return(msg); }
/// <summary> /// 同步玩家 /// </summary> /// <param name="player"></param> /// <param name="protocolBase"></param> public void MsgUpdateUnitInfo(Session session, GameMessage message) { UnitInfo unitInfo = ProtoTransfer.Deserialize <UnitInfo>(message.data); Player player = session.player; //获取房间 if (player.tempData.status != PlayerTempData.Status.Fight) { return; } Room room = session.player.tempData.room; //作弊校验略 player.tempData.posX = unitInfo.posX; player.tempData.posY = unitInfo.posY; player.tempData.posZ = unitInfo.posZ; player.tempData.lastUpdateTime = Sys.GetTimeStamp(); //广播 unitInfo.id = player.id; message.data = ProtoTransfer.Serialize(unitInfo); room.BroadcastTcp(message); }
// 复活玩家 public void ReLifePlayer(Player player) { System.Timers.Timer relifeTimer = new System.Timers.Timer(5000); // 5秒后执行 relifeTimer.Elapsed += (sender, e) => { if (playerDic[player.id] != null) { GameMessage broadMsg = new GameMessage(); broadMsg.type = BitConverter.GetBytes((int)Protocol.RelifePlayer); player.tempData.hp = 100; BattlePlayer battlePlayer = new BattlePlayer(); battlePlayer.id = player.id; battlePlayer.team = player.tempData.team; battlePlayer.swopId = player.tempData.swopId; battlePlayer.playerHp = ((int)player.tempData.hp); broadMsg.data = ProtoTransfer.Serialize(battlePlayer); BroadcastTcp(broadMsg); } }; relifeTimer.AutoReset = false; relifeTimer.Enabled = true; }
// 中途退出战斗 public void ExitFight(Player player) { //摧毁坦克 if (playerDic[player.id] != null) { playerDic[player.id].tempData.hp = -1; } //广播消息 GameMessage broadMsg = new GameMessage(); broadMsg.type = BitConverter.GetBytes((int)Protocol.Hit); HitInfo retInfo = new HitInfo(); retInfo.attId = player.id; retInfo.defId = player.id; retInfo.damage = 999; broadMsg.data = ProtoTransfer.Serialize(retInfo); BroadcastTcp(broadMsg); //增加失败次数 if (IsWinByPlayerCount() == 0) { player.data.defeat++; } int isWin = IsWinByPlayerCount(); UpdateWin(isWin); }
private void SendUnitInfo() { GameMessage message = new GameMessage(); message.type = System.BitConverter.GetBytes((int)Protocol.UpdateUnitInfo); UnitInfo unitInfo = new UnitInfo(); unitInfo.id = GameMgr._instance.id; Vector3 pos = transform.position; unitInfo.posX = pos.x; unitInfo.posY = pos.y; unitInfo.posZ = pos.z; Vector3 rot = player.fPCamera.GetRotate(); unitInfo.rotateX = rot.x; unitInfo.rotateY = rot.y; unitInfo.rotateZ = rot.z; unitInfo.input_h = paramter.inputMoveVector.x; unitInfo.input_v = paramter.inputMoveVector.y; unitInfo.moveState = (uint)State; message.data = ProtoTransfer.Serialize(unitInfo); NetMgr.GetInstance().tcpSock.Send(message); }
public bool Send(GameMessage message) { if (status != Status.Connected) { Debug.LogError(" [COnnection] 需要先连接再发送数据"); return(true); } byte[] b = ProtoTransfer.Serialize(message); return(Send(b)); }
/// <summary> /// 发送伤害信息 /// </summary> /// <param name="id"></param> /// <param name="damage"></param> public void SendHit(string id, float damage) { GameMessage msg = new GameMessage(); msg.type = System.BitConverter.GetBytes((int)Protocol.Hit); HitInfo hitInfo = new HitInfo(); hitInfo.attId = id; hitInfo.damage = (int)Mathf.Round(damage); msg.data = ProtoTransfer.Serialize(hitInfo); NetMgr.GetInstance().tcpSock.Send(msg); }
// 发送 public void Send(Session session, GameMessage message) { byte[] bytes = ProtoTransfer.Serialize(message); // 消息长度 byte[] lengthByte = BitConverter.GetBytes(bytes.Length); // 拼接消息 byte[] sendbuff = lengthByte.Concat(bytes).ToArray(); try { session.socket.BeginSend(sendbuff, 0, sendbuff.Length, SocketFlags.None, null, null); } catch (Exception e) { Debug.Log(string.Format("[发送消息]{0} : {1}", session.GetAddress(), e.Message), ConsoleColor.Red); } }
/// <summary> /// 同步子弹 /// </summary> /// <param name="player"></param> /// <param name="protocolBase"></param> public void MsgShooting(Session session, GameMessage message) { Player player = session.player; //获取房间 if (player.tempData.status != PlayerTempData.Status.Fight) { return; } ShootInfo shootInfo = ProtoTransfer.Deserialize <ShootInfo>(message.data); shootInfo.id = player.id; message.data = ProtoTransfer.Serialize(shootInfo); player.tempData.room.BroadcastTcp(message); }
/// <summary> /// 获得玩家信息 /// </summary> /// <param name="player"></param> /// <param name="protoBase"></param> public void MsgGetAchieve(Session session, GameMessage message) { GameMessage retMsg = new GameMessage(); retMsg.type = BitConverter.GetBytes((int)Protocol.GetAchieve); PlayerInfo playerinfo = new PlayerInfo(); playerinfo.id = session.player.id; playerinfo.win = session.player.data.win; playerinfo.defeat = session.player.data.defeat; retMsg.data = ProtoTransfer.Serialize <PlayerInfo>(playerinfo); session.SendTcp(retMsg); Debug.Log(string.Format("MsgGetScore {0}:{1}", session.player.id, session.player.data.win)); }
// 发送子弹同步信息 public void SendShootInfo(Vector3 pos, Vector3 rot) { GameMessage message = new GameMessage(); message.type = System.BitConverter.GetBytes((int)Protocol.Shooting); ShootInfo shootInfo = new ShootInfo(); shootInfo.posX = pos.x; shootInfo.posY = pos.y; shootInfo.posZ = pos.z; shootInfo.rotateX = rot.x; shootInfo.rotateY = rot.y; shootInfo.rotateZ = rot.z; message.data = ProtoTransfer.Serialize(shootInfo); NetMgr.GetInstance().tcpSock.Send(message); }
void OnRegClick() { //用户名密码为空 if (string.IsNullOrEmpty(idInput.text) || string.IsNullOrEmpty(pwInput.text)) { PanelMgr._instance.OpenPanel <TipPanel>("", "用户名或密码不能为空"); return; } if (repInput.text != pwInput.text) { PanelMgr._instance.OpenPanel <TipPanel>("", "两次输入密码不同!"); return; } //进行注册 //如果尚未连接,则发起连接 if (NetMgr.GetInstance().tcpSock.status != TcpSocket.Status.Connected) { int port = 7000; if (!NetMgr.GetInstance().tcpSock.Connect(NetMgr.GetInstance().tcpHost, port)) { //生成提示面板 //连接失败 PanelMgr._instance.OpenPanel <TipPanel>("", "服务器连接失败\n请稍后再试"); return; } } //发送 GameMessage message = new GameMessage(); message.type = BitConverter.GetBytes((int)Protocol.Regist); ReqLogin reqlogin = new ReqLogin(); reqlogin.account = idInput.text; reqlogin.password = pwInput.text; message.data = ProtoTransfer.Serialize <ReqLogin>(reqlogin); //发送Login协议,并注册返回监听 NetMgr.GetInstance().tcpSock.Send(message, OnRegBack); }
/// <summary> /// 更新比分 /// </summary> public void UpdateScore(Player player) { if (player.tempData.team == 1) { scoreT1++; } else if (player.tempData.team == 2) { scoreT2++; } // 广播更新比分的消息 GameMessage broadMsg = new GameMessage(); broadMsg.type = BitConverter.GetBytes((int)Protocol.UpdateScore); BattleScore battleScore = new BattleScore(); battleScore.scoreT1 = scoreT1; battleScore.scoreT2 = scoreT2; broadMsg.data = ProtoTransfer.Serialize(battleScore); BroadcastTcp(broadMsg); }
// 发送 public void Send(BattleCommand battlecmd) { byte[] bytes = ProtoTransfer.Serialize(battlecmd); Send(bytes); }
/// <summary> /// 伤害处理 /// </summary> /// <param name="player"></param> /// <param name="protoBase"></param> public void MsgHit(Session session, GameMessage message) { Player player = session.player; HitInfo hitInfo = ProtoTransfer.Deserialize <HitInfo>(message.data); //解析协议 string attId = hitInfo.attId; int damage = hitInfo.damage; //作弊校验 //long lastShootTime = player.tempData.lastShootTime; //if (Sys.GetTimeStamp() - lastShootTime < 0.1f) { // Debug.Log("MsgHit开枪作弊 " + attId,ConsoleColor.Yellow); // return; //} //player.tempData.lastShootTime = Sys.GetTimeStamp(); //获取房间 if (player.tempData.status != PlayerTempData.Status.Fight) { return; } Room room = player.tempData.room; //扣除生命值 if (!room.playerDic.ContainsKey(attId)) { Debug.Log("MsgHit not Contains player " + attId, ConsoleColor.Red); return; } Player attPlayer = room.playerDic[attId]; if (attPlayer == null) { return; } if (player.tempData.hp <= 0) { return; } player.tempData.hp -= damage; Debug.Log("[MsgHit] shooter: " + attId + " hp:" + player.tempData.hp + " damage:" + damage, ConsoleColor.Green); //广播 GameMessage broadMsg = new GameMessage(); broadMsg.type = BitConverter.GetBytes((int)Protocol.Hit); HitInfo retInfo = new HitInfo(); retInfo.attId = attId; retInfo.defId = player.id; retInfo.damage = damage; broadMsg.data = ProtoTransfer.Serialize(retInfo); room.BroadcastTcp(broadMsg); // 判断是否死亡 if (player.tempData.hp <= 0) { // 更新比分 room.UpdateScore(attPlayer); // 判断是否胜利 int winTeam = room.IsWinByScore(); room.UpdateWin(winTeam); if (winTeam == 0) { // 隔一段时间复活玩家 room.ReLifePlayer(player); } } }