예제 #1
0
    int count        = 0;          // 记录已经玩过的关卡数

    // 控制墙创建、移动和销毁,计算得分
    void ComingWall()
    {
        // 创建第一个墙
        // 此时游戏关卡计数为0,且场景中没有墙
        if (count == 0 && !hasWall)
        {
            wall    = CreateWall();
            hasWall = true;
            count++;
            // Debug.Log("first wall"+count);
        }
        // wall换成下一面墙
        // 此时关卡计数不是0,没有正在移动的墙,有墙等待
        else if (count < numOfWall && !hasWall && waiting)
        {
            // 把等待的墙赋值给正在移动的墙
            wall    = nextWall;
            waiting = false;
            hasWall = true;
            count++;
            // Debug.Log("other wall"+count);
        }
        // 一轮游戏结束
        // 此时关卡计数达到目标数,并且场景中没有正在移动的墙了
        else if (count == numOfWall && !hasWall)
        {
            count = 0;
            // 调用游戏结束的处理函数
            GameOver();
            return;
        }

        // 每次调用都更新当前关卡墙的位置
        wallController = wall.GetComponent <WallController>();
        float posZ = wallController.move();

        // 比较墙是否到要进行计算的位置,这样在墙离玩家比较远时不用做碰撞检测
        if (posZ <= stopZ)
        {
            // 保留最大值
            if (type == GameType.Single)
            {
                // 调用poseManager的方法计算得分,保留最高得分
                score = Mathf.Max(poseManagerSingle.Play(wallController.colliders), score);
            }
            else
            {
                // A B玩家分别计算分数
                scoreA = Mathf.Max(poseManagerA.Play(wallController.colliders), scoreA);
                scoreB = Mathf.Max(poseManagerB.Play(wallController.colliders), scoreB);
            }

            // 应该有下一面墙但是还没有时,创建一个新的对象
            if (count < numOfWall && !waiting)
            {
                nextWall = CreateWall();
                waiting  = true;
            }
        }

        // 墙到了终点,应该给玩家反馈
        // 反馈只需要一次,之后response为true,不会再给反馈
        if (posZ <= responseZ && !response)
        {
            // 根据分数做出反应
            // 这里还应该添加对应的声效和画面提示
            if (type == GameType.Single)
            {
                Debug.Log("score" + score);
            }
            else
            {
                Debug.Log("scoreA" + scoreA);
                Debug.Log("scoreB" + scoreB);
            }

            response = true;
        }

        // 墙到了要销毁的位置
        if (posZ < destroyZ)
        {
            Debug.Log("destroy" + count);
            // 销毁对象
            Destroy(wall);
            hasWall  = false;
            response = false;

            // 计算总分,清空单次分数
            // 某一模式下另一模式的分数为0,所以加上也不影响正确性
            totalScore += (score + scoreA + scoreB);
            score       = 0;
            scoreA      = 0;
            scoreB      = 0;
        }
    }