Prototype: Shield Powerup

Introduction

In the last post we added a new boost speed powerup. I’m thinking our player may also benefit from a shield powerup. Let’s add that today.

The Challenge

The challenge is to create a new shield powerup prefab, based on the work we’ve already done for the boost powerup.

Let’s get started.

The Prefab

We’ll need a way to represent this new type, so I’ll add it to the CollectableType enumeration.

public enum CollectableType
{
    Boost,
    Shield,
}

I’ll duplicate the powerup prefab and update the type to Shield. For now, I’ll leave the effect duration as is.

Absorbing Damage

We now need to decide how the shield will work for now. I want a way for designers (well, just myself for this project) to be able to quickly and easily update the shield strength.

In the player script, I’ll add a new Serialize Field called _shieldsRemaining, which will be initialized to 0 by default.

public class Player : MonoBehaviour
{
    ...

    private int _lives = 3;
    private int _shieldsRemaining = 0;

    ...
}

Because the shield powerup is tagged as Collectable, we don’t need to do anything inside of the OnTriggerEnter method. We can make our changes inside of ActivateCollectable instead. Here we will add a switch section to account for the shield, where we will assign a constant shield strength value to our _shieldsRemaining field.

private void ActivateCollectable(Collectable collectable)
{
    Destroy(collectable.gameObject);

    switch (collectable.Type)
    {
        case CollectableType.Boost:
            _speed *= Constants.Collectable.BoostMultiplier;
            StartCoroutine(CollectablePowerdownRoutine(collectable.Duration, UndoBoost));
            break;
        case CollectableType.Shield:
            _shieldsRemaining = Constants.Collectable.ShieldStrength;
            break;
        default:
            break;
    }
}

I have that constant currently set to 3.

public static class Collectable
{
    public static int ShieldStrength = 3;
    public static float BoostMultiplier = 2;
}

Here’s the result. After picking up the shield powerup, it now takes 6 hits before our player is destroyed. shield demonstration

Summary

I believe that’s all the powerups we need for now. If we have another idea for one, it will be quick and easy to add it with this small system in place. If our needs require a bit more complexity, we can expand what we have to meet that need.

Keep your systems as simple as possible until you absolutely need to add more abstraction and complexity.

Stay tuned for the next post! Take care.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s