This will be hard to explain what I want, so I’ll try my best.
I have a game, and the players in the game have a health counter (hp), and bodyguards as well. Each bodyguard has 100 hp, and once all a players bodyguards are gone, the damage comes off their hp.
So, these are the values I have:
$dmg // the damage
$bgs // the amount of bodyguards the player has
$bg_hp // the bodyguards health. Starts at 100, drains with $dmg fired at
$hp // the players health
When they’re shot, it needs to check if they have $bgs. This is a basic example.
if ($bgs > 0) {
$bgs_hp = $bgs_hp - $dmg;
if ($bg_hp <= 0) { $bg = $bg - 1; $bg_hp = 100; }
} else {
$hp = $hp - $dmg;
}
Anyway, the part I need help with is this. I want the damage to overlap.
So say the $dmg is 200, that would kill 2 bodyguards (they have 100 hp each). Or someone could shoot all their bodyguards and a remainder of the $dmg comes off the $hp too.
The reason I need help is because im awful at maths, and theres problem some MOD function or something that I need to use.
Examples;
1) Player has 3 bodyguards. Someone shoots at him for 150 dmg. It would kill 1 bodyguard, and cause 50 damage to the next.
2) Player has 1 bodyguard that is half hp (50). Someone shoots him for 160, it’d kill the bodyguard (50hp), and the rest of that damage (110) would kill the player too.
I’d simply retain an array with the hps of each bodyguard, like this:
or
If you want to know how many bodyguards there are, a simple
count($bgs)tells you.This will make it much easier for you to expand your game in the future, and it also makes it much easier to resolve these types of calculations. You can now write:
See it in action.
Update: Here’s code that works with your current scheme. It’s not really simpler, and it is more restrictive on what you can do: