/// <summary> /// Method deals damage to unit given as parameter. /// </summary> public virtual void DealDamage(Unit other, bool fromNetwork = false) { if (isMoving) { return; } if (ActionPoints == 0) { return; } if (!IsUnitAttackable(other, Cell) && !fromNetwork) { return; } MarkAsAttacking(other); ActionPoints--; GetComponent <Renderer>().material.color = LeadingColor / 6.0f; string a = this.GetType().ToString(); string b = other.GetType().ToString(); //AttackFactor = DamageChart.damageValues[this.GetType().ToString()][other.GetType().ToString()] ; AttackFactor = (int)(DamageChart.damageValues[this.GetDamageType().ToString().ToUpper()][other.GetArmorType().ToString().ToUpper()] * damageMultiplier * other.armorMultiplier); AttackFactor *= (typeOfTargets == Target.FRIENDLY ? -1 : 1); other.Defend(this, AttackFactor); UpdateHpBar(); if (ActionPoints == 0) { SetState(new UnitStateMarkedAsFinished(this)); MovementPoints = 0; } hasAttacked = true; isAbleToCapture = false; if (HitPoints <= 0) { CellGrid grid = GameObject.Find("CellGrid").GetComponent <CellGrid>(); Player myPlayer = grid.Players.Find(p => p.PlayerNumber.Equals(other.PlayerNumber)); if (grid.Units.FindAll(u => u.PlayerNumber.Equals(PlayerNumber) && !(u is Building)).Count <= 1) { myPlayer.EliminatePlayer(false, this); grid.CheckForEndGame(); } DestroyUnit(other, 100); return; } }
private void EliminateByRout(Unit defeater) { CellGrid grid = GameObject.Find("CellGrid").GetComponent <CellGrid>(); foreach (Unit u in grid.Units) { if (u is Building && u.PlayerNumber == this.PlayerNumber) { u.PlayerNumber = defeater.PlayerNumber; (u as Building).UpdateColor(); u.GetComponent <Renderer>().material.color = (u as Unit).LeadingColor; } else if (u.PlayerNumber == this.PlayerNumber) { u.DestroyUnit(defeater, 100); } } grid.CheckForEndGame(); }
/// <summary> /// Attacking unit calls Defend method on defending unit. /// </summary> protected virtual void Defend(Unit other, int damage) { MarkAsDefending(other); HitPoints -= DamageCalculator(other, damage, 0, Cell.defenseValue); GameObject.Find("DamageSound").GetComponent <AudioSource>().Play(); StartCoroutine("DamageFlash"); //(55 * ((100 / (100 + 0 * 10))) / (10 - Math.Ceiling(10))) //HitPoints -= Mathf.Clamp(damage - DefenceFactor, 1, damage); //Damage is calculated by subtracting attack factor of attacker and defence factor of defender. If result is below 1, it is set to 1. //This behaviour can be overridden in derived classes. if (UnitAttacked != null) { UnitAttacked.Invoke(this, new AttackEventArgs(other, this, damage)); } if (HitPoints <= 0) { CellGrid grid = GameObject.Find("CellGrid").GetComponent <CellGrid>(); Player myPlayer = grid.Players.Find(p => p.PlayerNumber.Equals(this.PlayerNumber)); if (grid.Units.FindAll(u => u.PlayerNumber.Equals(this.PlayerNumber) && !(u is Building)).Count <= 1) { myPlayer.EliminatePlayer(false, other); grid.CheckForEndGame(); } DestroyUnit(other, damage); return; } if (CanCounterAttack(other)) { AttackFactor = (int)(DamageChart.damageValues[this.GetDamageType().ToString().ToUpper()][other.GetArmorType().ToString().ToUpper()] * damageMultiplier * other.armorMultiplier); //AttackFactor = DamageChart.damageValues[this.GetType().ToString()][other.GetType().ToString()]; other.HitPoints -= other.DamageCalculator(this, AttackFactor, 0, 0); other.StartCoroutine("DamageFlash"); } UpdateHpBar(); }