Road to Game Dev: Phoning Home (Part 2)

Rowan Mcmanus
3 min readSep 1, 2021

It’s time to make our first truly complex weapon for the player: A spray of homing missiles.

What Features Do We Want?

There’s a lot of moving parts in this, so let me break down everything involved. First, we want to fire 4 missiles every time the missile button is pressed. We want each missile to target a different enemy, and we want our targets to be the closest enemies on the screen. We also want our missiles to only explode on their intended target, and not if they encounter another enemy on the way. And finally, we want to have an ammo system for our missiles.

The ammo system is easy, as is the button press; both of these are things we’ve already done several times now, so we can pretty safely move ahead. Firing 4 missiles each time seems simple, but there’s a caveat: We want to fire 4 missiles, even if there are fewer than 4 targets. However, before anything else, we need to decide what those targets even are. For this, we have to do a lot of array manipulation and nested loops.

Identifying Targets

When we push the missile button, a lot of things are going to happen all at once, so let’s run through it step by step. Before anything else, we need to know how many enemies exist, and how many of them are alive, therefore being valid targets. For this, we need to create some arrays; one array tracking every enemy we found, and one array tracking their status. We also want an int to tell us how many total targets there are so we don’t have to check the entire array each time we want to know.

Next step is to actually sort out our valid targets. We could sort the existing array of enemies, but we don’t actually need to store all of them, so we could instead create an array that only tracks our living enemies, and use that from now on. We want to check through the entire array again, this time checking two factors: If that enemy is alive, and how far away they are, sorting accordingly. The final array should have the enemies sorted from nearest to furthest.

Firing Missiles

Now, we’ve successfully found all of our targets and sorted them. What’s next is to actually fire our missiles! The tricky part here is twofold: Sometimes firing more missiles than we have targets (and therefore overlapping a bit), and making sure our missiles fire at different angles so we can actually see all of them firing and the player doesn’t get confused. This part is a bit simpler; it’s just a bit of math and some angle manipulation. Here it is:

--

--