Enemies not piling up

Started by wizard_wizzle (aka ZeldaHistorian), May 26, 2017, 02:36:48 AM

Previous topic - Next topic
In my game my enemies have a bad habit of "piling up" on each other, which makes it much easier to kill several of them with one hit. I was trying to avoid this with the code below, but I've now traced the code to some random crashes I've been having, so I need to take it out. I'd still like to find a way to not have the enemies pile up, and my guess is this was a stupid way in the first place and there's some obvious answer I've missed. So, my hope is the community can save me from my own stupidity again :)

Code ("lua") Select
function enemy:on_collision_enemy(other_enemy, other_sprite, my_sprite)
  if enemy:is_traversable() then
    enemy:set_traversable(false)
    sol.timer.start(200, function() enemy:set_traversable(true) end)
  end
end

Quote from: wrightmat on May 26, 2017, 02:36:48 AM
In my game my enemies have a bad habit of "piling up" on each other, which makes it much easier to kill several of them with one hit. I was trying to avoid this with the code below, but I've now traced the code to some random crashes I've been having, so I need to take it out. I'd still like to find a way to not have the enemies pile up, and my guess is this was a stupid way in the first place and there's some obvious answer I've missed. So, my hope is the community can save me from my own stupidity again :)

Code ("lua") Select
function enemy:on_collision_enemy(other_enemy, other_sprite, my_sprite)
  if enemy:is_traversable() then
    enemy:set_traversable(false)
    sol.timer.start(200, function() enemy:set_traversable(true) end)
  end
end


This is not a stupid question, but a very interesting and important one!  :)

First of all, your code is not a good idea, since two enemies could overlap and block themselves at the same time, a catastrophe! :o

I tried to do something similar one year ago, and had problems with my "custom pushing" between slime enemies. The game became too slow and unplayable (I don't remember if it crashed or not) because there were too many collision tests (there were tons of slimes overlapping themselves and not too much space!). I think that coding a "custom pushing" is not so trivial as it may seem, or maybe my code was terribly bad (which is very possible), nevermind. It can be done in Lua for sure, but I preferred to do something different.

In the end, I found a very different and easy solution to avoid enemies walking over themselves like if they were only one, and it's the one I plan to use.
These are the ingredients:

1) First of all, add some randomness for the behavior of each enemy breed: their sequence of actions could be random, at least partially. The randomness can be used to slightly modify the walking speed and target point (where the enemy goes), height and angle for jumps, and time duration if the enemy is waiting or changing state, etc. It also allows to choose the next action, randomly. For my slime enemies of the same breed this works great, and if they pile up, that is just temporary. You should try to avoid using only target movements; combine them with other types of behavior, attacks and movements.

2) Also, try not to repeat too many instances of the same enemy breed on the same map. Try to combine different enemies instead. And each enemy breed could have different behavior in the code. Walking speeds could be different, which would allow to avoid different enemies of the same breed overlapping themselves; and the same can be done with other moving parameters and behaviors.

My final recipe: (yummy!)
In summary, code different behaviors for different enemy breeds, add a bit of randomness to separate enemies of the same breed, and mix different enemy breeds on each map whenever it is possible (this also makes the game funnier).

If you don't like this recipe, maybe others can share their code of a "custom push" between enemies, or another yummy recipe!. ;D
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Hmm... i like the randomness idea, thanks! I've already tried to diversify my enemies, but will definitely continue that.