GameDev : Speed Powerup [and how start to think in modular way in OOP (Part I)]

Hello guys!!
We made many progress from the beginning, we understand how work Classes, variables, Methods or Function, how set up the Animator and the Animation. Everything realy great and beautifu!
But now, we start to enter in the real magic world of programming!

Objective : create a modular Powerup logic

As before, we start from scratch and try to understand the logic, ok?

In this game, we need more than one Powerup.
So, we start to set up Speed Powerup.

We have to think this : what behaviour has this Powerup?
A part from the logic of Powerup, the behaviour in the Game is EXACTLY THE SAME of any other Powerup.
After game start, the Powerup spawn every tot seconds.
Same behaviour.

Make a try to understand better :
I have this Speed Powerup in my project

Drag it in the scene and reset the scale and add CircleCollider2D and Rigidbody2D.
Same things of Triple Shot Powerup.

Remenber to check the Trigger, set the Gravity to 0 and set Layer to Foreground.
Rename it to Speed Powerup and make it a prefab.

Now, is time for us to think about this :
has sense create another script that is EXACTLY THE SAME of other Powerup?
And the answer in NO, because we have already this behaviour, why we have to create another equal?

Obviously, we need to modify something, but if we drag and drop the Powerup script in the Speed Powerup, this is what happen

This is my Speed Powerup, with every component and the Powerup script

Drag and drop the Speed Power in the scene and press Play

As we can see, it move down, and when we collide with us, we collect it and it Destroy itself.

The problem is that it has same logic for Triple Shot mechanics, but this is the ONLY PART WE NEED TO CHANGE.

We open Powerup script, and look at this part

Inside here there is the logic about the mechanics : if we collide with Player, run player.TripleShot IsActive().
So to create a system of different Powerup, a good way is to check WHAT Powerup we collected, and inside here CALL THE RIGHT METHOD.

For example, we can add to our Powerup script another variable to check the ID.
And if the ID is 1, we run TripleShot, if ID is 2, we run Speed Power and so on.

For now we simply create a powerupID variable, and Serialize it, so we can manage in the inspector.

Now if we look at the inspector for Powerup

We can assign different ID for every Powerup, and inside the Powerup script we can CHECK WHAT POWERUP WE COLLECTED.

This is very powerful, because if we add more Powerup we don’t have to change ABSOLUTELY NOTHING, the structure is functional and we simple add another Powerup with its logic and ID.
That’s why object oriented programming is so powerful!

We can use an if statement to do this

and it is perfectly fine.
Or, in my case, I prefer to use a SWITCH STATEMENT.

Because add many powerup and have more than 4 or 5 if / else is not the best thing to do.
With Switch, we have the structure, and we can add as many we want and a structure is a little bit clean and readable.

In next article we set up the logic for Speed Powerup!

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!