예제 #1
0
    public Platform(float x, float y, Type type, ItemThatSitsOnPlatform attachedItem, Transform roomTransform)
    {
        X            = x;
        Y            = y;
        MyType       = type;
        AttachedItem = attachedItem;

        switch (type)
        {
        case Type.Gap:
            //don't instantiate anything
            break;

        case Type.Ordinary:
            Object.Instantiate(RoomGenerator.StaticPlatformPrefab, new Vector3(x, y), Quaternion.identity, roomTransform);
            break;

        case Type.Small:
            Object.Instantiate(RoomGenerator.StaticSmallPlatformPrefab, new Vector3(x, y), Quaternion.identity, roomTransform);
            break;
        }
    }
예제 #2
0
    public ProceduralRoom(ProceduralRoom previousRoom) : base(previousRoom)
    {
        _unityObject = new GameObject("ProceduralRoom" + _index);

        GameObject bottomSpriteObject = new GameObject("bottom sprite");

        bottomSpriteObject.transform.parent   = _unityObject.transform;
        bottomSpriteObject.transform.position = new Vector3(_centerX, -4.76f);
        bottomSpriteObject.AddComponent <SpriteRenderer>().sprite = RoomGenerator.StaticBottomOfRoomSprite;


        float       halfRoomWidth     = 50.28f / 2; //because we are using a background with 5028 pixels width
        const float platformWidth     = 6.285f;     //(notice platformWidth * 8 = roomWidth)  , also 1356 * scale= 628.5, scale= 0.4635
        float       halfPlatformWidth = platformWidth / 2;
        const float platformHeight    = 0.616455f;

        float roomX = _centerX;

        Platform previousPlatform = previousRoom == null ? null : previousRoom.LastPlatform;  //the previous platform is the last platform of the previous room


        Transform roomTransform = _unityObject.transform;

        for (int i = 0; i < 8; i++)
        {
            float                  platformX    = roomX - halfRoomWidth + halfPlatformWidth + platformWidth * i; //x is easy as we know the position of the room and the relative position of the platform inside the room
            float                  platformY    = -3.8f;                                                         //it is important that we initialize this to -3.8f. This is the desired y value for the very first platform of our game (for this platform previousplatform=null)
            Platform.Type          type         = Platform.Type.Ordinary;
            ItemThatSitsOnPlatform attachedItem = null;


            if (previousPlatform != null)
            {
                platformY = previousPlatform.Y;


                switch (previousPlatform.MyType)
                {
                case Platform.Type.Gap:
                    type = Platform.Type.Small;
                    //override platformx calculation
                    platformX = roomX - halfRoomWidth + platformWidth * i;
                    break;

                case Platform.Type.Small:
                    type = Platform.Type.Ordinary;
                    break;

                case Platform.Type.Ordinary:

                    //check for gap eligibility
                    if (
                        _index > 0 &&  //not first room, we don't want any gaps and moving platforms in the very first toom
                        i >= 1 && i <= 5 &&     //platforms 0, 6 and 7 cannot be gaps
                        previousPlatform.AttachedItem == null &&
                        Random.Range(0, 4) == 0 //let's say 25% probability of gap
                        )
                    {                           //eligible
                        type = Platform.Type.Gap;
                    }
                    else
                    {    //no gap
                        type = Platform.Type.Ordinary;

                        //override platformy calculation
                        //we want a y that is the y of the previous platform plus/minus a small random value
                        //the only exception is when the previous platform had a ponger on it. We want the new platform to be placed much higher than usual
                        if (previousPlatform.AttachedItem != null && previousPlatform.AttachedItem.RequiresSufficientSpaceAbove)
                        {
                            platformY = previousPlatform.Y + 4 * platformHeight;
                        }
                        else
                        {
                            int upSameOrDown = Random.Range(0, 3);

                            switch (upSameOrDown)
                            {
                            case 0:         //up
                                platformY = previousPlatform.Y + platformHeight;

                                if (platformY > 2f)        //out of bounds, go down instead
                                {
                                    platformY = previousPlatform.Y - platformHeight;
                                }
                                break;

                            case 1:         //same level
                                platformY = previousPlatform.Y;
                                break;

                            case 2:         //down
                                platformY = previousPlatform.Y - platformHeight;

                                if (platformY < -3.8f)        //out of bounds, go up instead
                                {
                                    platformY = previousPlatform.Y + platformHeight;
                                }
                                break;
                            }

                            //check for attached item eligibility
                            //check if we should attach an object to the top of the platform
                            if (
                                _index > 0                            //don't add any objects to the first room so the player adjusts to the gameplay mechanics
                                &&
                                previousPlatform.AttachedItem == null //don't add objects to two platforms in a row
                                &&
                                Random.Range(0, 4) == 0               //25% probability of object
                                )
                            {                                         //eligible
                                int typeIndex = Random.Range(0, RoomGenerator.StaticItemsThatSitOnPlatforms.Length);
                                ItemThatSitsOnPlatform item = RoomGenerator.StaticItemsThatSitOnPlatforms[typeIndex];

                                if (item.RequiresSufficientSpaceAbove)
                                {    //we want to add a ponger. But we should be careful: when we add a ponger the next platform is placed higher than usual.
                                     //this means that in order to add a ponger we should first make sure that there is indeed sufficient space above

                                    if (platformY > 1)
                                    {    //nope, we are way too high for a ponger, instantiate another type
                                        int anotherTypeIndex;
                                        do
                                        {
                                            anotherTypeIndex = Random.Range(0, RoomGenerator.StaticItemsThatSitOnPlatforms.Length);
                                        } while (anotherTypeIndex == typeIndex);

                                        typeIndex = anotherTypeIndex;
                                        item      = RoomGenerator.StaticItemsThatSitOnPlatforms[typeIndex];
                                    }
                                }

                                //now we know what item to add
                                attachedItem = item;
                                //instantiate it
                                GameObject go = Object.Instantiate(item.Prefab, roomTransform);

                                //let's determine its position
                                //to have the object perfectly sit on top of the platform we should move it up a bit.
                                //By how much? ...   By half its height + half the platform's height (constant 0.308227f)
                                //(Oh and it is important that we use the instantiated object for that, not the prefab. The prefab has no bounds)
                                //so:
                                float verticalOffset = go.GetComponent <Collider2D>().bounds.extents.y + 0.308227f;

                                float horizontalOffset = 0.0f;
                                if (item.AutoDetermineHorizontalOffset)
                                {    //e.g. spike
                                     //when we go to a spike platform that is higher than the previous one it is difficult to avoid the spike, so we move the spike to the right
                                     //when we go to a spike platform that is lower than the previous one it is difficult to avoid the spike, so we move the spike to the left
                                    if (platformY > previousPlatform.Y)
                                    {    //new platform is higher, so move the spike right
                                        horizontalOffset = 2.5f;
                                    }
                                    else if (platformY < previousPlatform.Y)
                                    {    //move spike left
                                        horizontalOffset = -2f;
                                    }
                                }

                                //starting with its platform's position
                                go.transform.position = new Vector3(platformX + horizontalOffset, platformY + verticalOffset);
                            }
                        }
                    }
                    break;
                }
            }



            //we instantiate the platform and also make it previousplatform for the next room
            previousPlatform = new Platform(platformX, platformY, type, attachedItem, roomTransform);
        }//for platform


        LastPlatform = previousPlatform;
    }