Lets Build Community

Lets Build is a community of 163 amazing members

We're a place where indie game developers to connect with one another to share ideas, ask for help and teach each other.

Create new account Log in
loading...

I added Levels to My Game

gamedev4k profile image Game Dev 4K ・1 min read

Today, I added levels to my game. I did that by moving all the configurations into a Class & tweaking all these settings based on the level.

So, here the LevelManager.cs file:

using UnityEngine;

public class LevelManager : MonoBehaviour
{
    public int CurrentLevel { get; private set; }
    public int GameTime { get; private set; }
    public float DevSpeed { get; private set; }
    public float WalkSpeed { get; private set; }
    public int NoOfDropsInScreenIncrease { get; private set; }
    public float DropSpeedIncrease { get; private set; }
    public int MinPoints { get; private set; }
    public bool ShowTwitter { get; private set; }

    public LevelManager()
    {
        Reset();
    }

    public void Reset()
    {
        CurrentLevel = 1;
        GameTime = 30;
        MinPoints = 30;
        ShowTwitter = false;

        DevSpeed = 10;
        WalkSpeed = 1;
        NoOfDropsInScreenIncrease = 1;
        DropSpeedIncrease = 0.5f;
    }

    public void IncreaseLevel()
    {
        switch(CurrentLevel)
        {
            case 1:
                CurrentLevel += 1;
                GameTime = 30;
                MinPoints = 20;
                ShowTwitter = true;
                break;

             case 2:
                CurrentLevel += 1;
                GameTime = 60;
                MinPoints = 50;
                ShowTwitter = true;
                break;

            case 3:
                CurrentLevel += 1;
                GameTime = 60;
                MinPoints = 75;
                ShowTwitter = true;
                break;

            case 4:
                CurrentLevel += 1;
                GameTime = 60;
                MinPoints = 100;
                ShowTwitter = true;
                break;

            default:
                throw new System.Exception("It's impossible to reach this level");
        }
    }
}

Check the complete source code for the game.

C# getters without getters

This is the highlight of the above code:

public int CurrentLevel { get; private set; }

Now my CurrentLevel variable is not writable outside of the class. But inside the class, I can edit it.

With this, I can get rid of a bunch of stupid getters :).

Discussion

pic
Editor guide
Collapse
oivoodoo profile image
Alexandr Korsak

Probably you could remove your constructor because of using MonoBehaviour it should use Awake, Start, OnEnable, OnDisable, Destroy methods. it's the lifecycle of unity component. Probably once you boot your manager component, you can reset your variables and then boot your game.

Collapse
gamedev4k profile image
Game Dev 4K Author

Good idea. Thanks. I'll try that.

Collapse
cpluta profile image
Chris Pluta

One thing that helps wrap my head around scaling things like level definition is putting by using a Dictionary where the int is the level id, and the LevelInfo is an object holding your properties such as GameTime, MinPoints, and ShowTwitter in there.

Then you can just access your level dictionary and use that reference! But that's just how my brain works.

Collapse
gamedev4k profile image
Game Dev 4K Author

I thought that too. Then I have to introduce some strings. I thought it's better those as types.