Exemplo n.º 1
0
    public void OnTrigger(Pb.SkillTrigger trigger)
    {
        _animator.SetTrigger("Attack");
        Vector3 position = this.transform.position + this.transform.up * GameConfig._bulletAdjust + this.transform.forward * GameConfig._bulletOffset;
        Vector3 velocity = this.transform.forward * GameConfig._bulletSpeed;

        BulletController bullet = Instantiate(_bullet, position, Quaternion.AngleAxis(90.0f, this.transform.right)).GetComponent <BulletController>();

        bullet._playerID = _playerID;
        bullet._bulletID = trigger.BulletId;
        bullet._skillID  = trigger.SkillId;
        bullet._origin   = position;
        bullet._velocity = velocity;
    }
Exemplo n.º 2
0
    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);
        }
    }