From dc19a985607d9d6b3f6926e6c78fa4e06d457383 Mon Sep 17 00:00:00 2001 From: Samuel Date: Thu, 13 Aug 2015 13:02:34 -0400 Subject: [PATCH] - Upgraded Squad, ZombieSquad, VampireSquad - Refactored the class ZombieUnit, VampireUnit now are renamed to ZombieSquad and VampireSquad --- Assets/Scripts/Squad.cs | 67 +++++++++---------- .../{VampireUnit.cs => VampireSquad.cs} | 39 ++++++----- ...mpireUnit.cs.meta => VampireSquad.cs.meta} | 0 .../Scripts/{ZombieUnit.cs => ZombieSquad.cs} | 39 ++++++----- ...ZombieUnit.cs.meta => ZombieSquad.cs.meta} | 0 5 files changed, 78 insertions(+), 67 deletions(-) rename Assets/Scripts/{VampireUnit.cs => VampireSquad.cs} (71%) rename Assets/Scripts/{VampireUnit.cs.meta => VampireSquad.cs.meta} (100%) rename Assets/Scripts/{ZombieUnit.cs => ZombieSquad.cs} (70%) rename Assets/Scripts/{ZombieUnit.cs.meta => ZombieSquad.cs.meta} (100%) diff --git a/Assets/Scripts/Squad.cs b/Assets/Scripts/Squad.cs index f98fb63..8c29d28 100644 --- a/Assets/Scripts/Squad.cs +++ b/Assets/Scripts/Squad.cs @@ -16,7 +16,7 @@ public class Squad : Unit /// /// List of attackable units. /// - private List soldiers; + private List soldiers; /// /// List of abandonned units. (this list is accessible for both teams) @@ -31,16 +31,15 @@ public class Squad : Unit /// /// The tag to assign for each soldiers in the squad /// - public String squadTag; + protected String squadTag; #endregion // Use this for initialization - void Start () + void Awake () { - InitializeSquadTag(); humans = new List(); - soldiers = new List(); + soldiers = new List(); } // Update is called once per frame @@ -49,24 +48,22 @@ public class Squad : Unit // TODO execute movement command // TODO Check if all the units are dead // if yes destroy - if (soldiers.Count == 0) - { - // first release all humans ... - AbandonUnits(humans.Count); - // then we destroy the squad - DestroyUnit(); - } + if (soldiers.Count != 0) return; + // first release all humans ... + AbandonUnits(humans.Count); + // then we destroy the squad + DestroyUnit(); } #region squad related functions /// /// Assign the corresponding squad tag to each soldiers. /// - private void InitializeSquadTag() + protected void InitializeSquad() { - foreach (var soldier in soldiers) + if (soldiers.Count == 0) { - soldier.Tag = squadTag; + this.soldiers.Add(this.gameObject.GetComponent()); } } @@ -83,7 +80,7 @@ public class Squad : Unit /// Add the human unit in the abandonned unit list. /// /// the soldier unit to add in the soldier unit list - void AddSoldier(Unit soldierUnit) + void AddSoldier(Squad soldierUnit) { soldiers.Add(soldierUnit); } @@ -153,7 +150,7 @@ public class Squad : Unit /// Remove the selected soldier from the unit list. /// /// the corresponding soldier that we want to remove - void RemoveSoldier(Unit soldierUnit) + void RemoveSoldier(Squad soldierUnit) { soldiers.Remove(soldierUnit); } @@ -182,17 +179,20 @@ public class Squad : Unit // remove the human from the human list RemoveHuman(humanUnit); - - // AddSoldier((VampireUnit) humanUnit) ) (VampireUnit or ZombieUnit) + + // set the human tag to the same as the squad + humanUnit.Tag = Tag; + + // AddSoldier((VampireSquad) humanUnit) ) (VampireSquad or ZombieSquad) if (squadTag.Equals(TagManager.VampirePlayer)) { // add the vampire to the soldier list - AddSoldier((VampireUnit) humanUnit); + AddSoldier((VampireSquad) humanUnit); } else { // add the zombie to the soldier list - AddSoldier((ZombieUnit)humanUnit); + AddSoldier((ZombieSquad)humanUnit); } } } @@ -212,6 +212,8 @@ public class Squad : Unit // and then dead soldier is removed in the soldier list var topSoldier = soldiers.ElementAt(0); topSoldier.Hp -= amountOfDamage; + + Debug.Log(string.Format("{0} received {1} damage!",topSoldier.name, amountOfDamage)); } void CaptureHuman(Unit unit) @@ -220,28 +222,15 @@ public class Squad : Unit Debug.Log("Entered in collision with: " + unit.Tag); } - void AttackEnemy(Unit targetedUnit) + protected void AttackEnemySquad(Squad targettedEnemySquad) { //TODO improve this method add the total of squad damage or to compute the reduce of hp, etc... // compute the amount of hp reduced to this unit //unit.Hp -= Attack; // we remove some hp of the unit that was var amountOfDamageToApply = ComputeAttackDamage(); + targettedEnemySquad.ReceiveDamage(amountOfDamageToApply); - var squad = targetedUnit as Squad; - - if (squad != null) - { - // cast the unit to a squad - var squadTargetedUnit = squad; - // apply the damage to the squad - squadTargetedUnit.ReceiveDamage(amountOfDamageToApply); - } - else - { - // TODO update the attack ennemy in Vampire Unit and Zombie Unit class - } - - Debug.Log("Attacked the ennemy : " + targetedUnit.Tag); + Debug.Log("Attacked the ennemy : " + targettedEnemySquad.Tag); } int ComputeAttackDamage() @@ -264,5 +253,9 @@ public class Squad : Unit return abandonnedUnits; } + public int NumberOfSoldiers() + { + return soldiers.Count(); + } #endregion } diff --git a/Assets/Scripts/VampireUnit.cs b/Assets/Scripts/VampireSquad.cs similarity index 71% rename from Assets/Scripts/VampireUnit.cs rename to Assets/Scripts/VampireSquad.cs index c63b9a9..e63efad 100644 --- a/Assets/Scripts/VampireUnit.cs +++ b/Assets/Scripts/VampireSquad.cs @@ -4,12 +4,13 @@ using System.Collections; /// /// This class contains the information of the Vampire Units. /// -public class VampireUnit : Squad +public class VampireSquad : Squad { // Use this for initialization void Start () { + InitializeSquad(); InitializeDefaultTag(); // initialize default hp //Hp = defaultHp; @@ -51,18 +52,18 @@ public class VampireUnit : Squad void CaptureHuman(Unit unit) { // TODO either add the human as a squad member or change it's tag to vampireHuman - // when the player is transformed we just make VampireUnit vampireUnit2 = (VampireUnit) unit; + // when the player is transformed we just make VampireSquad vampireUnit2 = (VampireSquad) unit; Debug.Log("Entered in collision with: " + unit.Tag ); } - void AttackEnemy(Unit unit) + /* void AttackEnemySquad(Unit unit) { //TODO improve this method to compute the reduce of hp, etc... // compute the amount of hp reduced to this unit unit.Hp -= Attack; // we remove some hp of the unit that was Debug.Log("Attacked the ennemy : " + unit.Tag); - } + }*/ void OnTriggerEnter(Collider collider) { @@ -71,20 +72,28 @@ public class VampireUnit : Squad //switch(objectTag.GetType()) var unitComponent = collider.GetComponent(); - if (unitComponent != null) + if (unitComponent == null) + return; + // check if the unit is not a friendly one + if (this.Tag == unitComponent.Tag) + return; + + if (unitComponent.Tag.Equals(TagManager.Human)) { - // check if the unit is not a friendly one - if (this.Tag != unitComponent.Tag) + CaptureHuman(unitComponent); + } + else // we know that it's an ennemy + { + try { - if (unitComponent.Tag.Equals(TagManager.Human)) - { - CaptureHuman(unitComponent); - } - else // we know that it's an ennemy - { - AttackEnemy(unitComponent); - } + AttackEnemySquad(unitComponent as Squad); } + catch (InvalidCastException exception) + { + Debug.LogError(exception.ToString()); + //throw; + } + } } } diff --git a/Assets/Scripts/VampireUnit.cs.meta b/Assets/Scripts/VampireSquad.cs.meta similarity index 100% rename from Assets/Scripts/VampireUnit.cs.meta rename to Assets/Scripts/VampireSquad.cs.meta diff --git a/Assets/Scripts/ZombieUnit.cs b/Assets/Scripts/ZombieSquad.cs similarity index 70% rename from Assets/Scripts/ZombieUnit.cs rename to Assets/Scripts/ZombieSquad.cs index c54ea88..78febb1 100644 --- a/Assets/Scripts/ZombieUnit.cs +++ b/Assets/Scripts/ZombieSquad.cs @@ -4,12 +4,13 @@ using System.Collections; /// /// This class contains the information of the Vampire Units. /// -public class ZombieUnit : Squad +public class ZombieSquad : Squad { // Use this for initialization void Start() { + InitializeSquad(); InitializeDefaultTag(); // initialize default hp //Hp = defaultHp; @@ -51,17 +52,17 @@ public class ZombieUnit : Squad void CaptureHuman(Unit unit) { // TODO either add the human as a squad member or change it's tag to vampireHuman - // when the player is transformed we just make VampireUnit vampireUnit2 = (VampireUnit) unit; + // when the player is transformed we just make VampireSquad vampireUnit2 = (VampireSquad) unit; Debug.Log("Entered in collision with: " + unit.Tag); } - void AttackEnemy(Unit unit) + /*void AttackEnemySquad(Unit unit) { // compute the amount of hp reduced to this unit unit.Hp -= Attack; // we remove some hp of the unit that was Debug.Log("Attacked the ennemy : " + unit.Tag); - } + }*/ void OnTriggerEnter(Collider collider) { @@ -70,20 +71,28 @@ public class ZombieUnit : Squad //switch(objectTag.GetType()) var unitComponent = collider.GetComponent(); - if (unitComponent != null) + if (unitComponent == null) + return; + // check if the unit is not a friendly one + if (this.Tag == unitComponent.Tag) + return; + + if (unitComponent.Tag.Equals(TagManager.Human)) { - // check if the unit is not a friendly one - if (this.Tag != unitComponent.Tag) + CaptureHuman(unitComponent); + } + else // we know that it's an ennemy + { + try { - if (unitComponent.Tag.Equals(TagManager.Human)) - { - CaptureHuman(unitComponent); - } - else // we know that it's an ennemy - { - AttackEnemy(unitComponent); - } + AttackEnemySquad(unitComponent as Squad); } + catch (InvalidCastException exception) + { + Debug.LogError(exception.ToString()); + //throw; + } + } } } diff --git a/Assets/Scripts/ZombieUnit.cs.meta b/Assets/Scripts/ZombieSquad.cs.meta similarity index 100% rename from Assets/Scripts/ZombieUnit.cs.meta rename to Assets/Scripts/ZombieSquad.cs.meta