This will be a pretty strange question, but bear with me.
I'm coding a browser based game, and each player has an amount of guards, each with 100 health. Every time they are shot, the guards lose health. If all the guards are dead, the player takes the health instead.
The shot damage always overlaps as well, so if player has 3 guards, and the top guard has 60 health, a shot of 100 will kill guard 3 and leave guard 2 with 60 health.
i use a php array to sort this, and it works, except when it comes down to the players health. It doesn't calculate correctly, eg all a players guards are dead and the player has 60 health left. He is shot for 100, but instead of dying the health loops round so he has some other number health instead of -40.
$p_bg = array(); // players guard count
$p_bg[0] = $rs[bgs_hp2]; // only the top guard hp is saved (bgs_hp2)
$p_hp = $rs[hp2]; // players health
$dmg = 80 // shot damage
$x = 0;
while($x < $rs[BGs2]) { $p_bg[$x] = 100; $x++; }
// As long as there's still damage to take and bgs to take it:
while($dmg && !empty($p_bg)) {
$soak = min($p_bg[0], $dmg); // not more than the first bg can take
$p_bg[0] -= $soak; // remove hps from the first bg
$dmg -= $soak; // deduct from the amount of damage to tage
if ($p_bg[0] == 0) {
// bodyguard dead, remove him from the array
array_shift($p_bg);
}
}
// If there's any damage left over, it goes to hp
$p_hp = $p_hp - $dmg;