/// <summary> /// Updates the component while it's actively engaging in an attack /// </summary> private void OnActiveAttackUpdate() { if (!IsAttacking || //not really actively attacking? targetFaction == null || //no target faction assigned? gameMgr.InPeaceTime()) //or still in peace time? { return; } //attack order timer: if (attackOrderTimer > 0) { attackOrderTimer -= Time.deltaTime; } else { //reload attack order timer: attackOrderTimer = attackOrderReloadRange.getRandomValue(); if (currentAttackUnits.Count == 0) //if there are no more units in the attacking squad? stop attacking { CancelAttack(); } //did the current attack power hit the surrender resource type amount? foreach (ResourceInputRange resource in cancelAttackResources) { if (gameMgr.ResourceMgr.GetResourceAmount(factionMgr.FactionID, resource.Name) < resource.Amount.getRandomValue()) { CancelAttack(); //Cancel the attack and do not proceed. return; } } //if it doesn't have a current target yet, start by search for a building to attack, if none is found then look for a target unit (if defeat condition is set to eliminate all) if (currentTarget == null && SetTargetEntity(targetFaction.GetBuildings().Cast <FactionEntity>(), false) == false && gameMgr.GetDefeatCondition() == DefeatConditions.eliminateAll) { SetTargetEntity(targetFaction.GetUnits().Cast <FactionEntity>(), false); } EngageCurrentTarget(); } }
//when the NPC faction is attacking: void AttackProgress() { //if we're attacking and there's a valid target faction: if (isAttacking == true && targetFaction != null) { //attack order timer: if (attackOrderTimer > 0) { attackOrderTimer -= Time.deltaTime; } else { //reload attack order timer: attackOrderTimer = attackOrderReloadRange.getRandomValue(); //did the current attack power hit the surrender attack power? if (factionMgr.GetCurrentAttackPower() <= surrenderAttackPowerRange.getRandomValue()) { CancelAttack(); return; //do not proceed. } //does the faction has a target. if (currentTarget != null) { //attack it: AttackTarget(); } //if it doesn't have one yet, start by search for a building to attack, if none is found then look for a target unit (if defeat condition is set to eliminate all) else if (SetTargetEntity(targetFaction.GetBuildings().Cast <FactionEntity>()) == false && gameMgr.GetDefeatCondition() == DefeatConditions.eliminateAll) { SetTargetEntity(targetFaction.GetUnits().Cast <FactionEntity>()); } } } }