• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Damage all enemies in a specific radius

A

atxgamedesigner

Guest
Hello,

I'm trying to to create a grenade-like object.
A bomb that will damage all enemies within a specific radius of it.

I thought this would be as simple as having a large collision mask for the object, however its only destroying the first object it collides with at time of detonation - not all objects within the collision mask.

Some pseudo code would be helpful.
 

obscene

Member
Here's my damage_aoe script. It'll give the idea. This also scales the damage based on distance.

Code:
/// scr_damage_aoe(x,y,range,damage,type)

var xx=argument0;
var yy=argument1;
var range=argument2;
var damage=argument3;
var type=argument4;

with par_character
    {
    var distance=point_distance(x,y,xx,yy);
    if distance<=range
        {
        // Factor
        var f=(range-distance)/range;
     
        // Damage
        var d=damage*f;
        scr_damage(id,d,type);    
        }
    }
 
A

atxgamedesigner

Guest
Thanks for the reply, I'll check that out.

PS - Orphan looks incredible. Congrats!
 
From what you described, it sounds like your are destroying your object after it has dealt damage to an enemy?
If this is true, then it would only damage one enemy before destroying itself.
You can try having the object only destroy itself after a small delay using an alarm, that might fix the issue.

Or, if you don't have too many enemies on the map at once, you can always do this:
Code:
///scr_aoe_damage(x,y,radius,damage, obj_target)
var _x = argument0;
var _y = argument1;
var _damage = argument2;
var _targetType = argument3;

for(var i = 0; i < instance_number(_targetType); i++){
  var _target = instance_find(_targetType, i);
  if(point_distance(_x,_y,_target.x,_target.y) < radius){
    scr_damage(_target, _damage); //assuming you have some sort of damage script written
  }
}
I would call this script when the object causing the aoe damage arrives at its location.
 

Sabnock

Member
make a blank sprite & obj with a circular mask the radius you want which is created when the grenade explodes and then set up collisions to deal the damage you want?

you could modulate the damage done by measuring the distance for from the grenade to the enemy
 
Last edited:
A

atxgamedesigner

Guest
I appreciate all of your replies.
I'm having a really hard time getting this to work.

I only have one enemy object in the room, however there are many instances of it - like 30+.
My player has a gun, which I'd like to use the grenade as an "attachment" - like a grenade launcher.

I'm able to shoot the grenade towards enemies, however it doesn't stop.
Basically I've set the projectile to move towards the mouse position on creation.
I save the mouse position into variables as well, and tell the grenade to stop once it reaches that position in a step event.
Again, it doesn't stop - so I'm not sure if its skipping over those coordinates in the step even, or what..

The grenade has a very large circular mask, so it should easily encompass the area of effect I'm looking for.

So my issue -
A) I need to figure out how to get my grenade to go to and stop at a specific location.
B) Once at that location, kill anything that is within its collision mask.
(And, I'd like the grenade to not damage anything it encounters on its way to the landing point - so it only kills enemies when its stopped)

Thanks for any tips!
 

Sabnock

Member
if you are using if grenade x&y = mousex&y then that condition is unlikely to ever be true unless your grenade moves one pixel at a time. try if distance_to_point(mouse_x, mouse_y) < 2 or what ever works based on the speed the grenade moves.

https://docs.yoyogames.com/source/dadiospice/002_reference/movement and collisions/movement/distance_to_point.html

can you post the code you are using?

i wouldn't do any collisions with the grenade itself but instead create and obj like i mentioned above when the grenade is at or near mousex&y
 
Last edited:
A

atxgamedesigner

Guest
I tried the distance_to_point function, which certainly got me a bit closer to what I want.
It will travel almost to the point, and stop.

One issue is that no matter what distance I use, it appears to be roughly 25ish pixels away from where it should be.
So is it based on the edge of my collision mask, of the center point, or what specifically?
I've tried a ton of different values, and cant get the object to stop close enough to the mouse point I saved.

I may try playing around with your other suggestion.
Create a blank object at the mouse position > have grenade travel to it > destroy the grenade > blank object damages area around it...?
 
A

anomalous

Guest
Moving to a point is tripping you up? Take the time to figure out the basics here IMO.

Movement is delta x/y per step. That's it.

delta x/y can be expressed like this:

dx = lengthdir_x(dist,dir);
----------------------
dist = distance the grenade moves per step (the speed)
dir = the direction you threw it

To get the x position each step (to have it move) you just add dx to its current position
-----------------
x = x+dx;

Do that for for Y as well, I would, to get a feel for doing this yourself.

To precisely hit your target you simply check each step if the distance from the bullet to the target, is <= bullet speed
and if so, that step you move the bullet to the target position (and draw it there, so it hits!), and the next step, calculate the AOE damage and destroy the nade/spawn explosion.

If this tires you, you may be able to use the built in function for this, I have never used these I always do it myself because I'm lame.
if mp_linear_step(mouse_x, mouse_y, 5, 0)
{
instance_create(x,y,obj_Explosion);
instance_destroy();
}
From the manual, give it a try.
 
M

Me Myself and I

Guest
Put your grenade on a timer. Instead of blowing up at a certain distance, just have it blow up after XX steps. Adjust your move speed or alarm duration to get the right distance.

I also have a "lethal = 0" or "lethal = 1" setting so that my explosion only damages enemies during the "fireball" animation frames and not during the "smoke clearing" animation frames.
 
Last edited by a moderator:
D

Diveyoc

Guest
You could probably just add friction to the grenade code. Also, I'd say make the friction a random range so that the grenade doesn't always stop at the same distance, rather than having 100% accuracy.
Then - if speed = 0 {instance_destroy(); } and of course detonate.
So then you would throw it in the point direction of the mouse, and it would stop within a random range based on the friction.
 
Top