Page 1 of 1

Weird Question

Posted: Sun Nov 16, 2014 7:39 am
by lilfinn
I have a bit of code that check if the players and enemies are colliding, if they are it subtracts the player attack from the enemy health and the enemy attack from the player health. It does this ones every 20 ticks. if i make both the player and enemy attack the same and make their health the same, then they should kill each other evenly right? They don't.

if i have this

Code: Select all

for i,v in ipairs(enemies) do
          for ii,vv in ipairs(players) do...
The players always win.

if i have this

Code: Select all

for i,v in ipairs(players) do
          for ii,vv in ipairs(enemies) do...
The enemies always win.

If i spawn less than 5 at a time, they kill each other like they should. Anything over 5 and the above problem happens.

The reason might be obvious but i'm not seeing it.
Thanks in advance

Right click to spawn Enemies
Left click to spawn Players

Re: Weird Question

Posted: Sun Nov 16, 2014 8:56 am
by ivan
Given the example you have provided, I think you could just resolve the battles in 1 step.
As soon as a collision occurs, just subtract the health points of the two units.
If you want to resolve the battles over time you could use floating point numbers to represent the health of the units.
So for example each unit could lose 25.33 health points per second.
But when you draw the unit, just round the number so that it looks like an integer.

Re: Weird Question

Posted: Sun Nov 16, 2014 1:17 pm
by s-ol
The problem is just that the player kills the enemy first, and then the enemy can't hurt the player anymore I think.

Re: Weird Question

Posted: Sun Nov 16, 2014 2:57 pm
by lachlaan
It's as S0|||0s says. I had the same issue when tinkering around with a gravity simulator, where I needed to check each pair's interaction only once, but it was a case of the game not discriminating between "players" and "enemies". So you probably just need to check if both take damage at the same time.