• 2D shoot 'em up
SDL2 Santa game tutorial 🎅
SDL2 Shooter 3 tutorial
The Legend of Edgar 1.36
SDL2 map editor tutorial [UPDATED]
TBFTSS: The Pandoran War - Amiga OS4 Port
— A simple turn-based strategy game —
As it stands, we can attack and destroy ghosts by flinging magic at them, but right now we can do so from anywhere on the map, and without the hindrance of walls or other solid entities. In this part, we're going to add in range and line of sight (LOS), so that we need a clear shot at the enemies before we can attack them.
Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./sdl2TBS09 to run the code. You will see a window open like the one above, showing three wizards in a small room, surrounded by walls, as well as a white ghost. Notice how now when clicking on a wizard you can cycle through displaying no range, the move range, and the attack range. The attack range is shown as red squares. In order to be able to attack the ghost, it must be within one of the red squares. Moving your wizards about will update the attack range, like the move range. Play the game as normal, and defeat the ghost. Once you're finished, close the window to exit.
Inspecting the code
Adding in our attack range is fairly straightfoward. As with the accuracy and damage, our weapons will be the things that control the attack range.
We'll start with the update to defs.h:
We've added in SHOW_RANGE_ATTACK to our enums, so that when cycling through our wizard actions this is one of the possibilities.
Next, let's head over to structs.h:
We've updated MapTile, to add in a field called inAttackRange. Much like inMoveRange, this will flag whether the tile can be targetted for an attack.
Weapon has also been updated:
We've added in a field called `range`, that will hold the attack range of the weapon.
Moving over to units.c now, we've made a tweak to updateUnitRanges:
Alongside testMoveRange, we're now calling a new function named testAttackRange:
The funciton itself is quite simple. It takes a single parameter: `range`, the maximum distance our attack can reach. `range` is a radius, and so we're setting up two for-loops, to test the squares in that area. Ultimately, a square that can be targetted will have its MapTile's inAttackRange set. To qualify, a square must be inside the map bounds, must not be a wall, must have a distance from the attack less than or equal to the input range (our range will be circular, not square), and finally the line of sight check (hasLOS) must pass.
Our line of sight check (hasLOS) is much like the one from SDL2 Rogue:
We've talked about this in SDL2 Rogue, so we'll not discuss it here. One thing to note is that it is calling isBlocked, a function we've updated:
We're passing over a new parameter called losTest, to state whether this call is for a line of sight check. This is actually a pre-emptive update for a future part, where some features of the map will block the wizards, but not the ghosts. Otherwise, the function is more or less behaving as before (solid entities and walls will block the movement and line of sight of wizards and ghosts). We're doing this now, as it's part of the LOS feature.
So, we can now flag a load of squares on our map as being in the Unit's attack range, meaning they can be targetted and displayed as such.
Moving next to weapons.c, we've updated initWeapons:
Each of our weapons now has a `range` value set. Blue Magic will have a `range` of 10, Red Magic a `range` of 9, and Purple Magic a `range` of 15! Powerful stuff that Purple Magic, though its accuracy is poor compared to the other two.
Moving across to map.c, we've updated drawMap:
As we're now able to display both our move range and our attack range, we'll need to draw them as appropriate. As before, if Stage's showRange is not SHOW_RANGE_NONE we'll be drawing a range. We're now testing to see if showRange is SHOW_RANGE_MOVE and then testing if the current MapTile's inMoveRange flag is set before drawing moveTile. Similarly, we're checking if showRange is SHOW_RANGE_ATTACK and the MapTile's inAttackRange flag is set before drawing attackTile. Otherwise, we'll be drawing darkTile, so that squares outside of the range will be dark.
loadTiles has been updated, too:
We're now loading attackTile, alongside the others.
Heading next over to player.c, we've updated attackTarget with the final, all important check:
Now, as well as requiring AP to attack, the target entity must reside in a MapTile that has the inAttackRange flag set. Otherwise, nothing will happen.
We're almost done! We need just make one final little tweak, before we wrap things up. Heading over to hud.c, we've made a change to drawTopBar:
Now, when rendering the accuracy of the attack to the target, we're testing if it is possible to hit the target. If not, our accuracy will be displayed as 0. We're defaulting accuracy to 0, and then testing to see if the MapTile the target resides at has its inAttackRange flag set. If so, we'll update accuracy with a call to getAttackAccuracy. The reason for this is so that things don't look odd when we are unable to hit the target, but still see an accuracy reading.
That was easy, wasn't it? Once again, we're seeing how simple it is to slot things into our game thanks to all the work we did in part 1.
I think it's about time that our ghost was able to fight back, don't you? In the next part, we'll look into telling our ghost to attack the wizards whenever it spots them.
The source code for all parts of this tutorial (including assets) is available for purchase:
It is also available as part of the SDL2 tutorial bundle: