• 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
— Creating a Run and Gun game —
In this part, we're going to focus on fixing some of the issues that we identified recently: bullets passing through map tiles, and enemies being able to see the player anywhere they are.
Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./gunner06 to run the code. You will see a window open like the one above. The main player character starts off in the bottom left-hand corner. The same controls from past tutorials apply. The player can now move around the map, jump on the various map blocks, and battle the enemies. You will notice that you can no longer shoot through the map and that enemies won't fire on you without a clear line of sight. Once you're finished, close the window to exit.
Inspecting the code
This isn't a long part, since we're mainly dealing with a couple of issues. To start with, let's look at fixing the issue with the bullets passing through the game world (the map). We've made some tweaks to checkCollisions in bullets.c:
checkCollisions used to only check to see if our bullet had hit an entity. Now, we're checking first if it has made contact with the world. We've extracted the previous entity check code into a new function called checkEntityCollisions, and introduced another function called checkWorldCollisions. We're first testing if the bullet has hit the world. If it hasn't (its `life` is greater than 0), we're then testing the collisions with entities as before.
checkWorldCollisions is a very simple function:
The idea is that we're going to check to see if the bullet has entered a solid map tile and, if so, kill it. We're first taking the bullet's hitbox `x` value and adding half of its `hitbox` `w` (width), and then assigning it to a variable called `mx`. We're then dividing the result by MAP_TILE_SIZE, to find out the tile index on the horizontal axis. We're doing the same using the `hitbox`'s `y` and `h` values, to find the vertical position, and assigning it to a variable called `my`. With the position within the map found, we're calling isInsideMap to find out if the bullet is inside the map bounds, and also checking if the tile within the map at `mx` and `my` is a non-zero value. If the bullet isn't inside the map or has hit a non-zero tile (non-solid), we're setting the bullet's `life` to 0.
With our bullets no longer able to pass through solid map tiles, we can now look at blocking the enemies' line of sight to the player. We've introduced a new file to handle this, so it can be done centrally. ai.c will contain any functions that relate to common ememy behaviour. There are just two function here - canSeePlayer and traceMap. canSeePlayer is just one line right now:
The function takes two parameters (`x` and `y`) and simply returns the result of traceMap (passing through the `x` and `y` values it received). There is actually a little more to traceMap:
traceMap employs a line of sight (LOS) check, using Bresenham's line algorithm. We won't go into how this algorithm works, and only how we're using it for our LOS checks. First, we're setting up `x1` and `y2` (the line start point) as `x` and `y` divided by MAP_TILE_SIZE. The line end point is the middle of the player's `hitbox`, divided by MAP_TILE_SIZE. This LOS test will simply check the map tiles to see if the view is clear. In other words, it's a very coarse check. At the end of each line step, we're checking if we've left the map or if the map tile at `x1` and `y1` is a non-zero value (via isInsideMap and testing Stage's map data). If we're no longer inside the map or have hit a solid tile, we'll reuturn 0, to indicate that the trace failed. Should we get to the map tile in which the player resides (or, at least, the middle of their hitbox), we'll return 1.
Turning to greenSoldier.c, we've updated the lookForPlayer function to make it a little more sensible:
Now, instead of allowing the enemy to see the player from anywhere (depending on the direction of facing), the enemy can only see the player if they are half a screen away on the horizontal, and their own texture height on the vertical (in other words, a small rectangular region). The test for the direction of facing comes next. If that passes, we then call the canSeePlayer function that we defined in ai.c. The enemy's line of sight starts from their `x` position, plus half their texture width, as well as their `y` position. In other words, from the top of their head. If that check passes, the enemy is allowed to fire.
Those are our fixes done, which means we have now improved our gameplay a lot. What we need now is a larger map to explore. In the next part, we'll increase the size of our map, and even add in a new enemy type.
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: