class Gunner {
public $health;
public $attack;
function __construct($health, $attack) {
$this->health = $health;
$this->attack = $attack;
}
}
$player = array();
$player[] = new Gunner(100, 20);
$player[] = new Gunner(100, 20);
$player[] = new Gunner(100, 20);
$enemy = array();
$enemy[] = new Gunner(100, 20);
$enemy[] = new Gunner(100, 20);
I want to have some while loop that run through as long as the both arrays has “entities” / objects. How do I do that?
I want to fight each and every entity, like $player[0] would be fighting (aka do a rand(1,20)) and then remove from the opposites health until its 0. And when its 0 or less I would remove the entity (object) from the array.
I am not sure how the while loop or the delete from array would look like.
while ((count($attacker) > 0) && (count($defender) > 0))
{
$attacker_attack = rand(1, 25);
$defender[0]->health -= $attacker_attack;
if (!$defender[0]->IsAlive()) {
unset($defender[0]);
array_values($defender);
}
$defender_attack = rand(1, 20);
$attacker[0]->health -= $defender_attack;
if (!$attacker[0]->IsAlive()) {
unset($attacker[0]);
array_values($attacker);
}
}
You mean something like this (demo)?
PS: I updated the code to reflect your last comment, it’s kinda unclear how the turns should be played.