/// <summary> 非操作的人同步的行為 </summary>
    void SyncCharData()
    {
        if (IsInitPoint == false)
        {
            return;
        }

        switch (ModelType)
        {
        case PlayerType.Cat:
            switch (NowControlType)
            {
            case ControlType.Move:
                CatMove();
                break;

            case ControlType.Rotate:
                CatRotate();
                break;

            case ControlType.Run:                   //衝刺加速
                SpeedUp.Attach(gameObject);
                NowControlType = ControlType.Move;
                break;
            }
            break;

        case PlayerType.Ghost:
            switch (NowControlType)
            {
            case ControlType.Move:
                GhostMove();
                break;

            case ControlType.Rotate:

                break;

            case ControlType.ReadyRun:
                GhostReadyRun();
                break;

            case ControlType.Run:
                GhostRun();
                break;
            }
            break;
        }
    }
Example #2
0
    void ServerEvent(ClientSyncEvent _event)
    {
        //Debug.Log("animId : " + _event.animId + "  送出的 ID : " + _event.connectionId + " MYID : " + MyID + " : " + AllPlayerControl.Count);
        if (_event.animId == 0)
        {
            #region 建立角色
            PlayerControl control = null;

            if (AllPlayerControl.ContainsKey(_event.connectionId))
            {
                control = AllPlayerControl[_event.connectionId];
            }
            else
            {
                control = Instantiate <PlayerControl>(PlayerControlPrefab);
                control.gameObject.name = "PlayerID : " + _event.connectionId;

                if (_event.connectionId == MyID)
                {
                    control.InitInput();
                }

                control.ModelType = _event.isLucky ? PlayerControl.PlayerType.Cat : PlayerControl.PlayerType.Ghost;

                control.InitPoint(_event.mapping);

                AllPlayerControl.Add(_event.connectionId, control);
            }

            //建立 場上還沒出來的角色
            GameObject model = null;

            if (AllPlayer.ContainsKey(_event.connectionId))
            {
                model = AllPlayer[_event.connectionId].gameObject;
            }
            else
            {
                model = PlayerLoader.Instance.CreatePlayer(_event.catId);
            }

            if (model != null && control != null)
            {
                control.ConnectionId     = _event.connectionId;
                control.ControlModel     = model;
                control.ModelType        = _event.isLucky ? PlayerControl.PlayerType.Cat : PlayerControl.PlayerType.Ghost;
                control.gameObject.layer = LayerMask.NameToLayer("Ignore Raycast");

                model.transform.SetParent(control.transform, false);
                model.transform.localPosition = Vector3.zero;

                var player = model.GetComponent <Player>();
                control.ModelControl   = player;
                control.NowControlType = PlayerControl.ControlType.Move;

                if (AllPlayer.ContainsKey(_event.connectionId) == false)
                {
                    AllPlayer.Add(_event.connectionId, player);
                }

                player.ChangeModel(control.ModelType);
            }
            #endregion
        }

        //每次 Send 都會檢查
        if (_event.animId != 0 && AllPlayerControl.ContainsKey(_event.connectionId) && AllPlayer.ContainsKey(_event.connectionId))
        {
            int nowCatID = -1;

            #region 檢查 交替
            //尋找目前場上的貓
            foreach (var player in AllPlayerControl)
            {
                if (player.Value.ModelType == PlayerControl.PlayerType.Cat)
                {
                    nowCatID = player.Key;
                    break;
                }
            }

            int newCatID = _event.isLucky ? _event.connectionId : nowCatID;

            bool isChange = nowCatID != newCatID;

            //if (isChange)
            //    Debug.Log("目前的貓ID : " + nowCatID + "  新貓ID : " + _event.connectionId + " MYID : " + MyID);

            if (isChange)
            {
                PlayerControl newCatControl = AllPlayerControl[_event.connectionId];
                PlayerControl nowCatControl = AllPlayerControl[nowCatID];

                {
                    #region 鬼 => 貓
                    //讓貓繼承 原本的路徑方向 繼續跑
                    Vector3 catPos     = nowCatControl.transform.position;
                    Vector3 catforward = nowCatControl.transform.forward;

                    if (nowCatControl.IsAI && nowCatControl.gameObject.activeInHierarchy)
                    {
                        nowCatControl.gameObject.SetActive(false);
                        AllPlayerControl.Remove(-1);
                    }

                    //讓鬼 接著貓的路徑繼續奔跑
                    newCatControl.ModelType          = PlayerControl.PlayerType.Cat;
                    newCatControl.transform.position = catPos;
                    newCatControl.transform.forward  = catforward;
                    newCatControl.ChangeCD           = Time.time + 3f;

                    AllPlayer[newCatID].ChangeModel(PlayerControl.PlayerType.Cat);

                    ShowDieEffect.Instance.CatShowDieEffect(newCatControl.transform);
                    #endregion
                }

                {
                    #region 貓 => 鬼
                    if (nowCatControl.IsAI == false)
                    {
                        nowCatControl.transform.position = _event.running;
                        nowCatControl.transform.forward  = _event.runningForward;

                        nowCatControl.ModelType = PlayerControl.PlayerType.Ghost;
                        nowCatControl.InitPoint(_event.mapping - 1);
                        nowCatControl.NowControlType = PlayerControl.ControlType.Move;

                        AllPlayer[nowCatID].ChangeModel(PlayerControl.PlayerType.Ghost);
                    }
                    #endregion
                }

                #endregion
            }
        }

        if (_event.animId == 1)
        {
            if (AllPlayerControl.ContainsKey(_event.connectionId) && AllPlayer.ContainsKey(_event.connectionId))
            {
                AllPlayer[_event.connectionId].Turn();

                AllPlayerControl[_event.connectionId].NowControlType = PlayerControl.ControlType.Move;
                AllPlayerControl[_event.connectionId].Clockwise      = _event.mapping == 1;

                if (_event.runningForward != Vector3.zero)
                {
                    AllPlayerControl[_event.connectionId].transform.forward = _event.runningForward;
                }
            }
        }

        if (_event.animId == 2)
        {
            if (AllPlayerControl.ContainsKey(_event.connectionId) && AllPlayer.ContainsKey(_event.connectionId))
            {
                AllPlayer[_event.connectionId].Move();

                if (_event.connectionId != MyID)
                {
                    AllPlayerControl[_event.connectionId].NowControlType = PlayerControl.ControlType.Rotate;
                }
            }
        }

        if (_event.animId == 3)
        {
            if (AllPlayerControl.ContainsKey(_event.connectionId) && AllPlayer.ContainsKey(_event.connectionId))
            {
                if (_event.isLucky)
                {
                    AllPlayerControl[_event.connectionId].transform.position = _event.running;
                    AllPlayerControl[_event.connectionId].transform.forward  = _event.runningForward;

                    SpeedUp.Attach(AllPlayerControl[_event.connectionId].gameObject);
                }
                else
                {
                    if (_event.connectionId != MyID)
                    {
                        AllPlayer[_event.connectionId].Attack();

                        AllPlayerControl[_event.connectionId].OBTimer         = _event.fvaule;
                        AllPlayerControl[_event.connectionId].RunFinalPoint   = _event.running;
                        AllPlayerControl[_event.connectionId].NowControlType  = PlayerControl.ControlType.Run;
                        AllPlayerControl[_event.connectionId].TargetMoveIndex = _event.mapping;
                    }
                }
            }
        }

        if (_event.animId == 4)
        {
            //鬼的判定
            if (_event.isLucky == false)
            {
                AllPlayerControl[_event.connectionId].NowControlType = PlayerControl.ControlType.ReadyRun;
            }
            else
            {
                AllPlayerControl[_event.connectionId].NowControlType = PlayerControl.ControlType.Rotate;
            }

            AllPlayerControl[_event.connectionId].GhostRotateVal = _event.fvaule;

            if (_event.running != Vector3.zero)
            {
                AllPlayerControl[_event.connectionId].transform.position = _event.running;
            }

            if (_event.runningForward != Vector3.zero)
            {
                AllPlayerControl[_event.connectionId].transform.forward = _event.runningForward;
            }

            AllPlayerControl[_event.connectionId].RandomF = _event.mapping == 1;

            AllPlayerControl[_event.connectionId].CacheForward = AllPlayerControl[_event.connectionId].transform.forward;
            AllPlayerControl[_event.connectionId].CacheRight   = AllPlayerControl[_event.connectionId].transform.right;
        }
    }