public void OnUpdate(Pb.BroadCast broadcast) { _destination = new Vector3(broadcast.P.X, broadcast.P.Y, broadcast.P.Z); _playerBlood.value = broadcast.P.BloodValue * 1.0f / 1000.0f; this.transform.localEulerAngles = new Vector3(this.transform.localEulerAngles.x, broadcast.P.V, this.transform.localEulerAngles.z); _state = State.Update; }
public void OnLogon(Pb.BroadCast broadcast) { if (_state == State.None) { Init(); } _playerID = broadcast.Pid; _playerName.text = broadcast.Username; _playerBlood.value = broadcast.P.BloodValue; this.transform.position = new Vector3(broadcast.P.X, broadcast.P.Y, broadcast.P.Z); GameConfig._players.Add(_playerID, this); _state = State.Logon; }
public void OnLogon(Pb.BroadCast broadcast) { if (_state == State.None) { Init(); } // Debug.LogError("_heroID = " + broadcast.Pid + ", _heroName = " + broadcast.Username); _heroID = broadcast.Pid; _heroName.text = broadcast.Username; _heroBlood.value = broadcast.P.BloodValue * 1.0f / 1000.0f; this.transform.position = new Vector3(broadcast.P.X, broadcast.P.Y, broadcast.P.Z); Info(); GameConfig._heros[_heroID] = this; _state = State.Logon; }
public void OnChat(Pb.BroadCast broadcast) { string content = string.Format("{0}: {1}\n", broadcast.Username, broadcast.Content); _messageQueue.Enqueue(content); while (_messageQueue.Count > GameConfig._chatLinesMax) { _messageQueue.Dequeue(); } string display = ""; foreach (var message in _messageQueue) { display += message; } _chatText.text = display; }
public void HandleMessage() { Message message = null; lock (_messageQueue) { if (_messageQueue.Count > 0) { message = _messageQueue.Dequeue(); } else { return; } } Protocol messageID = (Protocol)message._messageID; byte[] messageBody = message._messageBody; string str = "Receive: "; for (int i = 0; i < messageBody.Length; ++i) { str += string.Format("{0:X2} ", messageBody[i]); } Debug.Log(str); try { switch (messageID) { case Protocol.GAME_MSG_LOGON_SYNCPID: { Pb.SyncPid syncpid = Pb.SyncPid.Parser.ParseFrom(messageBody); HeroController hero = GameObject.Find("Hero").GetComponent <HeroController>(); hero.OnLogon(syncpid); break; } case Protocol.GAME_MSG_BROADCAST: { Pb.BroadCast broadcast = Pb.BroadCast.Parser.ParseFrom(messageBody); if (broadcast.Tp == 1) { // 聊天的广播 ChatController chat = GameObject.Find("Canvas").GetComponent <ChatController>(); chat.OnChat(broadcast); } else if (broadcast.Tp == 2) { // 玩家的初始位置 if (!GameConfig._heros.ContainsKey(broadcast.Pid) && !GameConfig._players.ContainsKey(broadcast.Pid)) { try { PlayerController player = GameObject.Instantiate(Resources.Load <GameObject>("Player")).GetComponent <PlayerController>(); player.OnLogon(broadcast); player.OnUpdate(broadcast); } catch (Exception e) { Debug.LogError(e); } } } else if (broadcast.Tp == 4) { // 广播某个玩家的新移动位置 if (GameConfig._players.ContainsKey(broadcast.Pid)) { PlayerController player = GameConfig._players[broadcast.Pid]; player.OnUpdate(broadcast); } } break; } case Protocol.GAME_MSG_LOGOFF_SYNCPID: { Pb.SyncPid syncpid = Pb.SyncPid.Parser.ParseFrom(messageBody); if (GameConfig._players.ContainsKey(syncpid.Pid)) { PlayerController player = GameConfig._players[syncpid.Pid]; player.OnLogoff(syncpid); } break; } case Protocol.GAME_MSG_SUR_PLAYER: { Pb.SyncPlayers players = Pb.SyncPlayers.Parser.ParseFrom(messageBody); foreach (Pb.Player player in players.Ps) { Debug.Log("playerID = " + player.Pid + ", playerName = " + player.Username); if (!GameConfig._heros.ContainsKey(player.Pid) && !GameConfig._players.ContainsKey(player.Pid)) { PlayerController controller = GameObject.Instantiate(Resources.Load <GameObject>("Player")).GetComponent <PlayerController>(); controller.OnLogon(player); } } break; } case Protocol.GAME_MSG_CHANGE_WORLD_RESPONSE: { ChangeWorldResponse change = ChangeWorldResponse.Parser.ParseFrom(messageBody); SceneHandler.Instance.OnChangeScene(change); break; } case Protocol.GAME_MSG_SKILL_BROAD: { Pb.SkillTrigger trigger = SkillTrigger.Parser.ParseFrom(messageBody); if (GameConfig._players.ContainsKey(trigger.Pid)) { PlayerController player = GameConfig._players[trigger.Pid]; player.OnTrigger(trigger); } break; } case Protocol.GAME_MSG_SKILL_CONTACT_BROAD: { Pb.SkillContact skill = SkillContact.Parser.ParseFrom(messageBody); if (GameConfig._heros.ContainsKey(skill.TargetPid)) { HeroController hero = GameConfig._heros[skill.TargetPid]; hero.OnHurt(skill); } else if (GameConfig._players.ContainsKey(skill.TargetPid)) { PlayerController player = GameConfig._players[skill.TargetPid]; player.OnHurt(skill); } break; } default: { Debug.LogError("Unknown Protocol ID..."); break; } } } catch (Exception e) { Debug.LogError(e); } }