PC Games
• Orb Tutorials
• 2D shoot 'em up Latest Updates
SDL2 Versus game tutorial
Download keys for SDL2 tutorials on itch.io
The Legend of Edgar 1.37
SDL2 Santa game tutorial 🎅
SDL2 Shooter 3 tutorial
Tags • android (3) • battle-for-the-solar-system (10) • blob-wars (10) • brexit (1) • code (6) • edgar (9) • games (43) • lasagne-monsters (1) • making-of (5) • match3 (1) • numberblocksonline (1) • orb (2) • site (1) • tanx (4) • three-guys (3) • three-guys-apocalypse (3) • tutorials (17) • water-closet (4) Books The Third Side (Battle for the Solar System, #2) The White Knights have had their wings clipped. Shot down and stranded on a planet in independent space, the five pilots find themselves sitting directly in the path of the Pandoran war machine as it prepares to advance The Mission. But if they can somehow survive and find a way home, they might just discover something far more worrisome than that which destroyed an empire. |
— A simple turn-based strategy game — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction It's time to introduce an enemy with some different capabilities. Our Red Ghost will be able to create slime pools, by targetting the map itself with its weapon. These pools will be just like the ones that are placed when we create the map, although we won't be growing them any larger than the single square they occupy. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./sdl2TBS19 to run the code. You will see a window open like the one above, showing three wizards in a maze-like map, as well as a number of ghosts. Play the game as normal. The Red Ghosts will attack and retreat from the player, much like the Blue Ghosts, but will also target the map around the mages, to create slime pools. Once you're finished, close the window to exit. Inspecting the code Once again, adding in our Red Ghosts won't be too difficult, as we have a lot of things already in place to support them. Their behaviour will also be quite a lot like the Blue Ghosts, except they will be a little more aggressive. Starting with defs.h:
We've added a new AI enum: AI_SLIMER will be used to tell our ai system that this ghost is a slimer type. We've also added in a new weapon:
WT_SLIME_POOL is the weapon that the Red Ghosts will use to create slime pools. Just like regular weapons, it can also hurt the mages. Let's define this weapon now. We just need to head over to weapons.c and update initWeapons:
Like everything else, we're just defining the weapon's stats. It uses the same texture image as the slime ball, but has a much larger range and can cause a little more damage. It's less accurate, though, as is the case for most powerful weapons in games! Moving over to ai.c now, we'll add in our new AI profile. Starting with doAI:
We've added AI_SLIMER to our switch statement, where we'll call a new function named doSlimer, passing over our Unit data for the current ghost. The doSlimer function looks quite a bit like the doNormal function we updated for the Blue Ghost:
In much the same way as doNormal, we're making a call to lookForEnemies, and deciding how to respond, based on whether the doFallback and doAttack flags are set. However, when it comes to attacking, there is now a 1 in 3 chance that instead of calling fireBullet, we'll call throwSlimePool. Overall, there is also a much higher chance that the Slimer (Red Ghost) will attack a target, rather than falling back, compared to doNormal. We'll move onto the throwSlimePool function now:
This function is responsible for creating a slime pool. It will randomly select a location on the map near to its selected target, and attempt to fire at it. We start by setting a variable called `attempts` to 12. Again, this is a control variable, to ensure our function gives up trying to target a square within a certain time and not get stuck in an endless loop. Next, we'll enter a while-loop, and take the `x` and `y` coordinates of our currently targeted entity and adjust them by between -3 and 3. These `x` and `y` values will be where we'll attempt to create our slime pool. We test that this new location (x / y) is a ground tile, is within the ghost's attack range, and that there is nothing already occupying the tile, with a call to getEntityAt. We'll assign the result to a variable called `ok`. If `ok` is 1, we'll prepare to attack the tile. We'll be making use of Stage's worldTarget entity, just as we did with the mages. We set worldTarget's `x` and `y` to the `x` and `y` values we picked, and also assign worldTarget's takeDamage function pointer to a function called worldTargetTakeDamage (we'll see this in a moment). Next, we'll update Stage's targetEntity to be the worldTarget. Ultimately, this will mean that we will be attacking the map, at the specified x and y coordinates. If we exhaust all our `attempts` (or `ok` is set), we'll exit the loop. Regardless of whether we were able to target the world, we'll call fireBullet, meaning we'll either attack the ghost's original target or throw a slime pool. That's all we need to do for our slimer logic. We now just need to make a few adjustments elsewhere, to support the action further. First, let's finish up ai.c, by tweaking addAIUnits:
We'll be creating Red Ghosts for this part, so we want to call initEntity and pass over "Red Ghost" (once again, with the appropriate additionals to entityFactory.c). The last function we want to investigate is worldTargetTakeDamage:
A very simple function - it simply converts a ground tile into a slime tile. We're calling isGround here, just to sanity check Stage's worldTarget's `x` and `y` values, in case we attempt to set memory outside of the bounds of our map array. Now, let's move over to bullets.c, where we've made one small change to applyDamage:
WT_SLIME_POOL will cause the same hit effect as WT_SLIME_BALL, so we simply add that case to our bullet type switch statement. Almost done! Just one very important thing left to do, and that's to define the ghost itself. Moving over to ghosts.c, we've added a new function named initRedGhost:
We're defining our Red Ghost, giving it its `name`, `texture`, and all other attributes. For the `weapon`, we're calling getWeapon with WT_SLIME_POOL, and for its AI type we're setting AI_SLIMER. Finished! We now have a ghost that attacks with a little more aggression and can create slime pools, to hinder our mages. 4 ghosts down, 1 more to go. Our final ghost will be a very special type and will have some gameplay implications - the Green Ghost will explode when it is killed, splattering the surrounding area with slime pools, and also injurying anything standing nearby..! We'll see how we create this guy next. Purchase The source code for all parts of this tutorial (including assets) is available for purchase: From itch.io It is also available as part of the SDL2 tutorial bundle: |