Exemplo n.º 1
0
    void Start()
    {
        Debug.Assert(platformPrefab != null);
        Debug.Assert(spawnCount > 0);
        ninja = FindObjectOfType <NinjaController>();

        BoxCollider2D platformCollider = platformPrefab.GetComponent <BoxCollider2D>();

        halfPlatformHeight = platformCollider.size.y / 2;
        xPadding           = (xSpread + 1) * platformCollider.size.x;

        float platformHalfWidth = platformCollider.size.x / 2;

        //just set this game object's position = the main camera's position to avoid local positioning confusion
        transform.position = new Vector2(mainCam.transform.position.x, mainCam.transform.position.y);

        //this is in world coordinates
        float topCamEdge = (mainCam.transform.position.y - mainCam.orthographicSize) + (mainCam.orthographicSize * 2 * ySpread);
        float botCamEdge = (mainCam.transform.position.y - mainCam.orthographicSize) + halfPlatformHeight;

        //the top edge of the camera's y location is relative to this game object's y position
        localTopCamEdge = topCamEdge - transform.position.y;
        localBotCamEdge = botCamEdge - transform.position.y;


        //always assign the first platform to be right below the player at all times

        float randomHeight = Random.Range(localBotCamEdge, localTopCamEdge);

        GameObject platform = Instantiate <GameObject>(platformPrefab, transform, false);

        platform.transform.localPosition = new Vector3(platformHalfWidth, ninja.transform.position.x - this.transform.position.x - platform.GetComponent <BoxCollider2D>().size.x / 2, ninja.transform.position.y - this.transform.position.y - ninja.GetComponent <Collider2D>().bounds.size.y / 2);
        platforms.Add(platform);

        //generate all other platform heights based on the previous platform's height and the player's jump height
        for (int i = 1; i < spawnCount; ++i)
        {
            GameObject prevPlatform = platforms[i - 1];
            float      minHeightMod = prevPlatform.transform.localPosition.y - ninja.jumpHeight;
            float      maxHeightMod = prevPlatform.transform.localPosition.y + ninja.jumpHeight;
            if (minHeightMod < localBotCamEdge)
            {
                minHeightMod = localBotCamEdge;
            }
            if (maxHeightMod > localTopCamEdge)
            {
                maxHeightMod = localTopCamEdge;
            }

            randomHeight = Random.Range(minHeightMod, maxHeightMod);

            GameObject platformClone = Instantiate <GameObject>(platformPrefab, transform, false);
            platformClone.transform.localPosition = new Vector3(i * xPadding + platformHalfWidth, randomHeight, 0.0f);
            platforms.Add(platformClone);
        }

        Debug.Log(mainCam.pixelRect.ToString());
    }
Exemplo n.º 2
0
    private void Update()
    {
        var inputDevice = InputManager.ActiveDevice;

        ninja.GetComponent <NinjaInputManager>().AssignInput(inputDevice as XInputDevice);
    }