Saturday, August 20, 2016

Attack Waves

In defender all of the baddies don’t all arrive at once there are a number of waves per level. So to do that when instatiating the baddies I'll have a delay between them.

In a previous blog on the development of Rhino roll I used Editor scripting to help create the levels. So I’ll do the same here again. 

Here is my code to create an array of objects in the editor script.

    static void ObjectsToArray(string objType)
    {
        var Code = "private Vector2[] " + objType + " = new Vector2[] {";
        var objs = Selection.gameObjects.OrderBy(go => go.transform.position.x);
        bool IsFirst = true;

        foreach (var item in objs)
        {
            if (!IsFirst)
                Code += ", ";
            else
                IsFirst = false;

            Code += string.Format("new Vector2({0:0.000f}, {1:0.000f})", item.transform.position.x, item.transform.position.y);
        }
        Code += "};";

        print(Code);
    }

Here is the code to add the Menu Items to the IDE of Unity and call the function 

    [MenuItem("Adz/Util/SheepToArray")]
    static void SheepToArray()
    {
        ObjectsToArray("Sheep");
    }

    [MenuItem("Adz/Util/LandersToArray")]
    static void LandersToArray()
    {
        ObjectsToArray("Landers");
    }


The idea is to move the landers aboout, duplicate new ones et.c. then select them all and call the LandersToArray function, like below.

Here is the code automatically generated by the function call. Saves so much time when designing levels, in my opinion.

    void Level1Wave1()
    {
        Vector2[] Landers = new Vector2[] { new Vector2(-48.800f, -0.140f), new Vector2(-42.200f, 1.560f), new Vector2(-11.880f, -1.120f), new Vector2(-5.680f, -0.440f), new Vector2(-0.080f, 2.320f), new Vector2(0.020f, -1.850f), new Vector2(3.500f, 1.560f), new Vector2(24.200f, 0.960f) };
        Vector2[] LanderDirections = new Vector2[] { new Vector2(-0.240f, -0.074f), new Vector2(-0.933f, -0.997f), new Vector2(0.489f, -0.842f), new Vector2(0.702f, -0.403f), new Vector2(0.700f, 0.146f), new Vector2(-0.497f, -0.331f), new Vector2(-0.186f, 0.392f), new Vector2(-0.868f, 0.386f) };

        DoLanders(Landers, LanderDirections, .02f);
    }


Then in the fixed update add the code to invoke these functions after a given time.

        if (CurrentWave < Waves.Length)
        {
            Waves[CurrentWave]--;
            if (Waves[CurrentWave] == 0)
            {
                CurrentWave++;
                var wave = string.Format("Level{0}Wave{1}", CurrentLevel, CurrentWave);
                Invoke(wave, 0f);
            }
        }

As before here is a video to demonstrate. It has 2 attack waves from the aliens. I haven't done any fancy animations for the materialisation yet.


No comments:

Post a Comment