public function HandleUpgrade($Game, $Upgrades) { $HpUpgrade = false; foreach ($Upgrades as $UpgradeId) { $Upgrade = $this->GetTechTree()->GetUpgrade($UpgradeId); if ($Upgrade->GetCostForNextLevel() > $this->GetGold() || Upgrade::IsLevelOneUpgrade($UpgradeId) && $Upgrade->GetLevel() >= 1 || Upgrade::HasRequiredUpgrade($UpgradeId) && $this->GetTechTree()->GetUpgrade(Upgrade::GetRequiredUpgrade($UpgradeId))->GetLevel() < Upgrade::GetRequiredLevel($UpgradeId)) { continue; } $this->DecreaseGold($Upgrade->GetCostForNextLevel()); $Upgrade->IncreaseLevel(); if (Upgrade::IsElementalUpgrade($UpgradeId)) { $ElementalUpgrades = $this->GetTechTree()->GetElementalUpgrades(); $TotalLevel = 0; foreach ($ElementalUpgrades as $ElementalUpgrade) { $TotalLevel += $ElementalUpgrade->GetLevel(); } // Loop again to set the next level cost for each elemental foreach ($ElementalUpgrades as $ElementalUpgrade) { $ElementalUpgrade->SetPredictedCostForNextLevel($TotalLevel); } } else { if ($UpgradeId === Enums\EUpgrade::DPS_AutoFireCanon) { $this->getTechTree()->BaseDps = Upgrade::GetInitialValue($UpgradeId); $this->getTechTree()->Dps = $this->getTechTree()->BaseDps; } else { if (Upgrade::GetType($UpgradeId) === Enums\EUpgradeType::HitPoints) { $HpUpgrade = true; } else { if (Upgrade::GetType($UpgradeId) === Enums\EUpgradeType::PurchaseAbility) { $this->GetTechTree()->AddAbilityItem(Upgrade::GetAbility($UpgradeId)); } } } } } $this->GetTechTree()->RecalulateUpgrades(); if ($HpUpgrade && !$this->IsDead()) { $this->Hp = $this->getTechTree()->GetMaxHp(); } }