Boss Battle: Let’s code each section!
As promised, let’s go through the section code, again here it is in full, and we’ll start breaking it down!:
The first few lines are events for the main boss to delete a section. We covered this in the cycling through different sections article but in a nutshell, we’ll need these to announce that a section is deleted, and the boss will pick up on it and do the deed.
We’ll need to manually tell Unity each sections ID to tell the boss script what to delete when the time comes.
_childrenCount as stated before is the amount of children to count down to before deleting the section. Sometimes, there will be a a boss-ship element along with the layers we need to ignore so we set the childrenCount to 1 instead of 0.
_Layers are the various layers we’ll be cycling through when our players ship destroys turrets and boss ship parts!
_currentLayer will be for keeping track what layer each section is on.
_currentCount is also a variable we use for a specific function that will count all the layers inside of the _Layers array.
sentMessage is to make it so an event is only announced once.
Our current layer is the amount of layers we have in our array. Simple enough!
So in our Update, instead of long code chunks, we have descriptive functions. Check to see if all children are destroyed and then check to see if a layer’s been destroyed. On to the actual code!
We’ve been over this code before but with one slight alteration. Instead of checking for 0 children, we now check for _childrenCount because we migrated our boss ship pieces from its own empty into the sections. Of course it goes without saying that the children in this case are the layers. If its true that all the layers are destroyed, then we check to see if we’ve sent the message already, if we haven’t, then we announce to the boss we need to be deleted from the paths.
The first part is cycling through the layers array to shorten the currentCount if we find any null entries when the layers destroy themselves when they have 0 children.
if the current count is smaller than the _currentLayer, then we update the _currentLayer to the next smaller number, and on that layer it turns on all the colliders it finds so they can become destructable! This way, you can have all the layers graphics on screen, but they won’t be affected until we need them to be!
Let’s see it in action!
So cool! We’re almost finished. There’s still a few bits we need to do like create live shooting turrets and possibly more weapons, set off fires and explosions, and most importantly assign points and an actual ‘death’ when the boss is completely destroyed, which I’ll write about tomorrow. But for now, we now have a working multi-tiered destructable boss battle!