What happens if d is say -7?
Yes, you'll want the absolute value of d.
class Post
def initialize
a = $game_map.events[14].x #coordinate of landmine
for i in 1..20
next if !$ABS.enemies[i]
b = $ABS.enemies[i].event.x #coordinate of enemy mob
d = a - b
if $ABS.enemies[i].hp > 20
return $ABS.enemies[i].hp -= d.abs < 2 ? 20 : 0
end
end
end
end
You are still only looking at the x coordinate which means the mine will explode in a 5 tile wide vertical line. (Or at least do damage there.)
If you want a diamond shaped area you can do something like this:
class Post
def initialize
landmine = $game_map.events[14] #coordinate of landmine
for i in 1..20
next if !$ABS.enemies[i]
enemy = $ABS.enemies[i].event #coordinate of enemy mob
d = (landmine.x - enemy.x).abs + (landmine.y - enemy.y).abs
if $ABS.enemies[i].hp > 20
return $ABS.enemies[i].hp -= d < 2 ? 20 : 0
end
end
end
end
Assuming $ABS.enemies is an array I would use the array's .each method and clean up the code like this:
class Post
def initialize
landmine = $game_map.events[14] #coordinate of landmine
for enemy in $ABS.enemies
next unless enemy
# Calculate manhatten distance
d = (landmine.x - enemy.event.x).abs + (landmine.y - enemy.event.y).abs
if enemy.hp > 20
return enemy.hp -= d < 2 ? 20 : 0
end
end
end
end
I haven't tested any of the code snippets, but I do hope it'll help you along
*hugs*