Galaxy Shooter 2D — Modularity #9

Adding two new powerups: Speed Boost and Shield!

So far, I have only implemented the Triple Shot powerup. It works really well, but it’s not enough to make the game enjoyable. That’s why, today, I will talk about how I have programmed a modular powerup system and the two new powerups for my game:
Speed Boost and Shield!

Time to refactor

One of the ways to modularize the use of powerups has been to enable in the same script to choose what type of powerup it is. That is, the Powerup script will be a container that contains all the references to the powerups and triggers those abilities that are programmed inside the player’s ship script.

With the use of enums and the switch statement, I can trigger the powerup currently selected in the Ship’s script.

This approach is fine if all the powerups behave the same way (movement, interaction… have no difference between powerups). However, if I wanted to truly modularize those powerups, I would’ve made a base class where all the powerups inherit that class and all the Trigger calls would be made through that base. For now, it will stay as it is.

Also, see that SetPowerupType(Type newType) method? It has a line that access and sets an integer in the animator. Because every powerup has it’s own sprites, I have to set the proper animation in the Animator, and I do that like this:

The default animation is an Empty Container that will automatically select the respective animation through that PowerUpType integer.

Whenever my SpawnManager spawns a powerup, it will randomly select a powerup within the Type enum Length. That way, if I ever add new powerups, I don’t have to change any line of code to suit any change.

Implementing the two new Powerups

These two powerups are pretty much self-explanatory, so, I will briefly comment on how I implemented them in the ship’s script.

Speed Boost

With the use of a ternary operator, depending on the state of the Speed Boost powerup, I can dynamically set the speed of the ship.

Shield

Whenever the shield is active, if an enemy or a laser has collided with the player, the shield will take that damage, preventing the ship from being damaged.

What’s next

The next two articles will be about the Unity UI, and how you can create your own HUD and menus with it!

--

--

--

Junior Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Count ways to N’th Stair(Order does not matter)

Overview of Spring framework

Image result for spring framework details and content

Cloudy, with a chance of Meetup: Behind the scenes of moving Meetup to the cloud

Python Beginner — Day 08

Get Started with Stacks and Queues in Python

Community Update

SVG: Scalable Vector Graphics

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pablo Gómez Platón

Pablo Gómez Platón

Junior Unity Developer

More from Medium

Adding laser attacks to our boss

How to upgrade materials to URP in Unity 2021.2 and above

Installing and creating a new Unity project

VR Development — Day#4