예제 #1
0
    void updateFunc()
    {
        updateLoginTime += Time.deltaTime;
        updateScoreTime += Time.deltaTime;
        gameTime        += Time.deltaTime;

        if (updateLoginTime > 1)
        {
            updateLoginTime = 0;
            // 匹配房间
            if (gameStatus != GameStatus.GAME_OVER && gameStatus != GameStatus.PLAY_AGAIN &&
                gameStatus != GameStatus.GAME_WIN)
            {
                System.Action <string> callback = unfriendlyCSharpFunction;
                StartCoroutine(HttpHelper.HttpHelper.canStart(yourName.roomId, yourName.playerId, callback));
            }
        }

        if (updateScoreTime > 0.5f)
        {
            updateScoreTime = 0;
            if (gameStatus != GameStatus.GAME_OVER && gameStatus != GameStatus.PLAY_AGAIN &&
                gameStatus != GameStatus.GAME_WIN)
            {
                StartCoroutine(HttpHelper.HttpHelper.syncPlayerStatus(
                                   PlayerJson.JsonHelper.animJson("update_score", playerList, yourName.playerId),
                                   yourName.roomId, yourName.playerId));
            }
        }

        if (MasterSceneManager.Instance.mainPause)
        {
            return;
        }

        syncAnimation();

        //print("current status: " + gameStatus);
        switch (gameStatus)
        {
        case GameStatus.INIT:
            initGame();
            cameraCtrl.CameraInit();
            break;

        case GameStatus.CREATE_PLATFORM:
            //createPlatform();
            //gameStatus = GameStatus.TAPING;
            if ((playerList[yourName.playerId].currentPlatformIndex + 1) <= (platformQueue.Count - 1))
            {
                // render color
                if (extraBonus > 0)
                {
                    Material materialColored = new Material(Shader.Find("Diffuse"));
                    materialColored.color = extraBonus == 1 ? Color.green : Color.yellow;
                    platforms[playerList[yourName.playerId].currentPlatformIndex + 1].GetComponent <Renderer>().material = materialColored;
                }
                PlayerJson.JsonHelper.Player p = playerList[yourName.playerId];
                LitJson.JsonData             x = LitJson.JsonMapper.ToObject(platformQueue[p.currentPlatformIndex + 1]);
                p.direction = x["direction"].ToString() == "True" ? true : false;
                playerList[yourName.playerId] = p;
                gameStatus = GameStatus.TAPING;
                Timer      = 0;
            }
            break;

        case GameStatus.TAPING:
            if (isPressing(true))
            {
                playMusic(pressingAudio);
                IsPressing = true;
            }
            if (IsPressing)
            {
                PlayerJson.JsonHelper.Player p = playerList[yourName.playerId];
                if (isPressing(false))
                {
                    playMusic(pressingAudio);
                    Timer += Time.deltaTime;
                    if (Timer < 4)
                    {
                        p.power = Timer * 3;
                        // currPlatform 可能返回空,因为当前木块可能为空
                        if (currPlatform(yourName.playerId) != null)
                        {
                            currPlatform(yourName.playerId).transform.localScale = new Vector3(1, 1 - 0.2f * Timer, 1);
                            currPlatform(yourName.playerId).transform.Translate(0, -0.1f * Time.fixedDeltaTime, 0);
                        }
                        p.player.transform.Translate(0, -0.2f * Time.deltaTime, 0);
                    }
                }
                else
                {
                    stopMusic(pressingAudio);
                    IsPressing = false;
                    gameStatus = GameStatus.REBOUND;
                    if (currPlatform(yourName.playerId) != null)
                    {
                        Scale = currPlatform(yourName.playerId).transform.localScale.y;
                    }
                }
                playerList[yourName.playerId] = p;
            }
            break;

        case GameStatus.REBOUND:
            PlayerJson.JsonHelper.Player f = playerList[yourName.playerId];
            Timer += Time.deltaTime;
            if (currPlatform(yourName.playerId) != null)
            {
                currPlatform(yourName.playerId).transform.localScale = Vector3.Lerp(new Vector3(1, Scale, 1), new Vector3(1, 1, 1), Timer);
                currPlatform(yourName.playerId).transform.Translate(0, 0.5f * Time.deltaTime, 0);
            }
            f.player.transform.Translate(0, 1.0f * Time.deltaTime, 0);

            if (currPlatform(yourName.playerId) == null || currPlatform(yourName.playerId).transform.position.y >= 0.5)
            {
                gameStatus                    = GameStatus.PLAYER_JUMPING;
                f.VSpeed                      = 0.3f;
                f.prePlayerPosition           = f.player.transform.position;
                playerList[yourName.playerId] = f;
                // 记录动画
                StartCoroutine(HttpHelper.HttpHelper.syncPlayerStatus(
                                   PlayerJson.JsonHelper.animJson("jump", playerList, yourName.playerId),
                                   yourName.roomId, f.playerId));
            }
            playerList[yourName.playerId] = f;
            break;

        case GameStatus.PLAYER_JUMPING:
            playerJump(yourName.playerId);
            break;

        case GameStatus.GAME_OVER:
            stopMusic(pressingAudio);
            print("game_over");
            Timer += Time.deltaTime;
            if (Timer >= 1)
            {
                Timer      = 0;
                gameStatus = GameStatus.PLAY_AGAIN;
            }
            timeText.text = "再接再厉";
            break;

        case GameStatus.PLAY_AGAIN:
            stopMusic(pressingAudio);
            playAgainFunc();
            break;

        case GameStatus.GAME_WIN:
            stopMusic(pressingAudio);
            print("chijichijichijichijichiji");
            timeText.text = "吃鸡";
            if (!syncGameWinFlag)
            {
                syncGameWinFlag = true;
                StartCoroutine(HttpHelper.HttpHelper.syncPlayerStatus(
                                   PlayerJson.JsonHelper.animJson("game_win", playerList, yourName.playerId), yourName.roomId, playerList[yourName.playerId].playerId,
                                   delegate(string s) { playAgainFunc(); }
                                   ));
            }
            break;

        default:
            break;
        }
    }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        print(status);

        switch (status)
        {
        case InGameStatus.init:         //如果游戏处于初始状态
            Score = 0;
            UpdateScore();

            foreach (GameObject current in Plats)                //销毁所以的木桩
            {
                Destroy(current);
            }
            Destroy(Chess);               //销毁弹跳物体(象棋子)

            //跳台的位置
            Vector3 position = new Vector3(0, 0.5f, 0);
            NextPlatsform = Instantiate(Platsform, position, Quaternion.Euler(Vector3.zero));
            Plats.Add(NextPlatsform);

            Chess      = Instantiate(ChessAsset, new Vector3(0, 1.25f, 0), Quaternion.Euler(Vector3.zero));
            ChessChild = Chess.transform.Find("default").gameObject;

            cameraCtrl.CameraInit();

            status = InGameStatus.CreatePlat;
            break;

        case InGameStatus.CreatePlat:          //如果游戏进入创建 plat 阶段

            PrePlatsform = NextPlatsform;      //需要显示的跳台

            IsToRight     = (Random.Range(0, 10) > 5);
            NextPlatsform = Instantiate(Platsform, PrePlatsform.transform.position + GetStartPoint(), Quaternion.Euler(Vector3.zero));

            Plats.Insert(Plats.Count, NextPlatsform);

            Point1   = NextPlatsform.transform.position;
            Point2   = Point1;
            Point2.y = 0.5f;

            //重新设置相机的位置
            cameraCtrl.SetPosition((PrePlatsform.transform.position + Point2) / 2);

            Timer  = 0;
            status = InGameStatus.ShowPlat;
            Power  = 0;
            break;

        case InGameStatus.ShowPlat:

            Timer += Time.deltaTime;
            NextPlatsform.transform.position = Vector3.Lerp(Point1, Point2, Timer * Speed);

            if (Timer * Speed > 1)
            {
                status = InGameStatus.TapInput;
                Timer  = 0;
            }
            break;

        case InGameStatus.TapInput:          //按压时
            if (Input.anyKey)
            {
                IsPressing = true;
            }
            if (IsPressing)
            {
                if (Input.anyKey)
                {
                    Timer += Time.deltaTime;
                    if (Timer < 4)
                    {
                        Power = Timer * 3;
                        PrePlatsform.transform.localScale = new Vector3(1, 1 - 0.2f * Timer, 1);
                        PrePlatsform.transform.Translate(0, -0.1f * Time.deltaTime, 0);
                        Chess.transform.Translate(0, -0.2f * Time.deltaTime, 0);
                    }
                }
                else
                {
                    IsPressing = false;
                    status     = InGameStatus.BounceBack;
                    Scale      = PrePlatsform.transform.localScale.y;
                }
            }
            break;

        case InGameStatus.BounceBack:          //弹起

            Timer += Time.deltaTime;
            PrePlatsform.transform.localScale = Vector3.Lerp(new Vector3(1, Scale, 1), new Vector3(1, 1, 1), Timer);
            PrePlatsform.transform.Translate(0, 0.5f * Time.deltaTime, 0);
            Chess.transform.Translate(0, Time.deltaTime, 0);

            if (PrePlatsform.transform.position.y >= 0.5)
            {
                status           = InGameStatus.ChessAnim;
                VSpeed           = 0.3f;
                PreChessPosition = Chess.transform.position;
            }

            //跳完之后, 确保最多只有5个物体, 将之前多余的删除掉
            if (Plats.Count > 5)
            {
                //销毁不在视野范围内的物体
                Plats.RemoveAt(0);
                Destroy((GameObject)Plats [0]);
            }
            break;

        case InGameStatus.ChessAnim:

            VSpeed -= Time.deltaTime;
            if (IsToRight)
            {
                Chess.transform.Translate(new Vector3((NextPlatsform.transform.position.x - PreChessPosition.x) / 0.6f * Time.deltaTime, VSpeed / 2, Power / 0.6f * Time.deltaTime));
                ChessChild.transform.Rotate(new Vector3(600 * Time.deltaTime, 0));
            }
            else
            {
                Chess.transform.Translate(new Vector3(-Power / 0.6f * Time.deltaTime, VSpeed / 2, (NextPlatsform.transform.position.z - PreChessPosition.z) / 0.6f * Time.deltaTime));
                ChessChild.transform.Rotate(new Vector3(0, 0, 600 * Time.deltaTime));
            }
            if (Chess.transform.position.y <= 1)
            {
                Chess.transform.position      = new Vector3(Chess.transform.position.x, 1.25f, Chess.transform.position.z);
                ChessChild.transform.rotation = Quaternion.Euler(0, 0, 0);
                if (Mathf.Abs(Chess.transform.position.x - PrePlatsform.transform.position.x) < 0.5 && Mathf.Abs(Chess.transform.position.z - PrePlatsform.transform.position.z) < 0.5)
                {
                    status = InGameStatus.TapInput;
                }
                else
                {
                    if (Mathf.Abs(Chess.transform.position.x - NextPlatsform.transform.position.x) > 0.5 || Mathf.Abs(Chess.transform.position.z - NextPlatsform.transform.position.z) > 0.5)
                    {
                        status = InGameStatus.GameOver;
                        Timer  = 0;
                    }
                    else
                    {
                        if (Mathf.Abs(Chess.transform.position.x - NextPlatsform.transform.position.x) < 0.1 && Mathf.Abs(Chess.transform.position.z - NextPlatsform.transform.position.z) < 0.1)
                        {
                            Bonus++;
                            Score += Bonus * 2;
                            UpdateScore();
                        }
                        else
                        {
                            Bonus = 0;
                            Score++;
                            UpdateScore();
                        }
                        status = InGameStatus.CreatePlat;
                    }
                }
            }
            break;

        case InGameStatus.GameOver:
            if (Timer == 0)
            {
                Rig = ChessChild.AddComponent <Rigidbody> ();
            }
            Timer += Time.deltaTime;
            if (Timer >= 1)
            {
                Timer  = 0;
                status = InGameStatus.ShowMenu;
            }
            break;

        case InGameStatus.ShowMenu:
            button.SetActive(true);
            break;

        default:
            Debug.LogError("这是什么状况~!");
            break;
        }
    }