GameMaker Tutorials: Splash Damage

 

In this tutorial we are going to take a look at creating an explosion that deals a sliding scale of damage based on proximity, better known as Splash Damage. This is a fairly simple process, though many people struggle with implementing it. The basic concept most people start with for the implementation is to find a point and then check in a radius around it if anything damageable is within range. Then based on the distance apply a different amount of damage. While we could use a radius and do a sweep of the area, there is an easier way. What we really need to do is check all the objects and seeing how far away they are from the center point and applying damage accordingly. No radius or fancy maths needed, just a well built for loop.

Download the Work File (.gmz)

Building The Targets

We’ll start by creating the targets. While these could be any size, for the purposes of this tutorial we will use a 32 x 32 pixel sprite. To indicate various levels of damage, the supplied sprite uses 5 frames of animation to represent the states of Pristine, Slight, Moderate, and Heavy damage, and finally Dead. The Origin is centered, which is important to note, as we will use that point to determine how far away the instance is away from the epicenter of the explosion.

We need to create an object called obj_Target and the only properties we are going to give the target is a basic health system. Each target change what image is displayed according the health level it currently is at. Let’s initialize some variables in the target’s create event. We don’t want the targets to be animated, so we set the image_speed to zero. Since we want each individual target to its own health, we need to create a variable for it, in this case myHealth.

Next we will check the health level every frame. If the target has more than 90 percent health we will consider it Pristine. As we drop down in health we switch to Light, Medium and Heavy damage. The value range can be whatever you want it to be, but in this example we will just drop it by 30% each level. The important part is that  we are changing the index of the image according to what range of health the individual target is currently in.

The target is now complete and we need to fill a room completely with instances of the target. This will give us a sandbox to test the splash damage.

Creating the Splash Damage

For the splash damage explosion we will use a Global Left Mouse Released event on an Overlord object to represent the point of detonation. We want to make this functionality reusable, but for expediency, we are only going to pass the epicenter of the explosion as arguments. We will start by capturing the location of the epicenter of the explosion. We then set up three zones that we will use to determine the falloff distances. Finally, we find out how many targets exist in the room. We need to subtract this number by one as we will run a loop through all the instances, starting at zero.

Next we get into the nitty gritty of what we are attempting to do. We run through every instance of the target in the room to see how far away it is from the epicenter of the explosion. If it is within any of the zones, we reduce the health of that specific instance accordingly. Any target outside of this range will not be affected by the explosion.

That’s it! If you run the game you should be able to click anywhere in the room and cause an explosion with yellow at the center, then some orange, red and finally the grey Pristine targets. Notice that it will not necessarily be a perfect circle of colors. The falloff occurs from the epicenter of where the mouse was clicked and we are comparing it to the center of each target sprite. If you click several times you should also notice that the damage can be applied more than once. That tells us everything is working exactly as intended!

Now that you know how to create splash damage, why not try adding it to a projectile or other object. Instead of targets, try it with moving enemies.  If you want to make this script even more reusable, you could easily add arguments for the various zone distances and the object to search for. I look forward to seeing it in your games!

Download the Completed File (.gmz)

If you liked this tutorial and are thinking “I need to get me some more of this learnin’ up in here” Then here is how you can get it! 
Check out my book here! If you want to see what you will learn, I have playable versions of the projects here!
HTML5 Game Development with GameMaker

4 thoughts on “GameMaker Tutorials: Splash Damage

  1. Your for loop could be simpler still.

    Obviously, if the distance puts target within zone_01, all three of these conditionals will return true, and each will apply 25 points of damage, for a total of 75. So you don’t have to do as many checks on your ifs, and it should perform a little faster as a result.

  2. I like this. I usually play things safe, but this would make things even simpler. Thanks!

  3. In the above comment, I made a typo, in the line

    distance = point_distance(epicenter_x, epicenter_Y, target.x, target.y) the ‘x’ in epicenter_x should have been capitalized. So, the for loop should have been:

  4. Hey thanks for the tutorial. I’m brand-new to GML (first week or so) and this taught me some useful things. I couldn’t successfully implement your “for” statement (it gave me an assignment operator error, possibly due to syntax changes in the past 2 years?) but at least now I know what a “for” statement is and how it can be applied.

Leave a Reply