Welcome back! Time to continue our journey with our hypercasual game.
What we were missing in the last episode was enemies and we will get into it now. An enemy works similarly to the person that we have in the crowd, at least visually. So we will use the same character.
First off, we want to create an IDamageable interface that will define the objects that our Shootable can hurt. The IDamageable will have a function using which our Shootable will apply the damage
Now, we can create the Enemy class which will inherit IDamageable. Let’s make sure the Enemy has a hitPoints variable that defines its health. Using the inherited Damage function we will apply damage to our Enemy. We will add visual feedback such as animation and renderer color change when the hitpoints expire.
Now, the reason we created the IDamageable class is that we want our shootable to apply damage to not only enemies but also any other objects that we may come up with in the future. For eg: a barrel.
We want to create a new Barrel class for that and apply what will happen as it gets damaged. But we don’t want to touch the Shootable class. The shootable class does not need to know what it is damaging or how it is doing so. This way, we won’t need to do any changes to our already committed classes but only the classes that are new. This ensures good programming practice and fewer bugs. It is the Open Closed principle
Entities should be open for extension but closed for modification
The IDamageable class is open for extension, which it is. But we are not modifying it, nor we are modifying any scripts that depend on it i.e. the Shootable class
At this point, we want to give a lifetime to our Shootables so that it does not go too far and hit enemies that are too far out of the screen.
Pretty standard stuff!
Now we can simply scale our enemies and provide different hitpoints to get variations. A simple playthrough looks like this
We have simple gameplay. We can polish it to our needs to make it look great. We can also add different gameplay elements on top of the code groundwork that we laid out. But this wraps up our rather short episode 3. I delivered what I promised, the core gameplay. If you have any questions, please leave comments or reach out to me.
Thanks and see you again on a new one!