void OnCollisionEnter(Collision collision) { if ("Hero".Equals(collision.collider.tag)) { HeroController hero = collision.collider.GetComponent <HeroController>(); if (_playerID == hero._heroID) { Debug.Log("子弹打到自己了。。。"); } } if ("Player".Equals(collision.collider.tag)) { PlayerController player = collision.collider.GetComponent <PlayerController>(); if (_playerID != player._playerID) { Pb.Position position = new Pb.Position(); position.X = collision.contacts[0].point.x; position.Y = collision.contacts[0].point.y; position.Z = collision.contacts[0].point.z; Pb.SkillContact crash = new Pb.SkillContact(); crash.BulletId = _bulletID; crash.SkillId = _skillID; crash.SrcPid = _playerID; crash.TargetPid = player._playerID; crash.ContactPos = position; byte[] message = crash.ToByteArray(); NetworkHandler.Instance.SendMessage(Protocol.GAME_MSG_SKILL_CONTACT, message); } } Destroy(this.gameObject); }
public void OnHurt(Pb.SkillContact skill) { _playerBlood.value = skill.ContactPos.BloodValue * 1.0f / 1000.0f; }
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); } }