private void AttackUnits() { Vector3 currentPos = transform.position; GameObject[] targetA = GameObject.FindGameObjectsWithTag("TeamA"); foreach (GameObject o in targetA) { UnitAController unitInfo = o.GetComponent <UnitAController>(); float dist = Vector3.Distance(o.transform.position, currentPos); if (dist <= attackRange) { unitInfo.Health -= attackDamage; if (unitInfo.Health <= 0) { Destroy(o); } } } GameObject[] targetB = GameObject.FindGameObjectsWithTag("TeamB"); foreach (GameObject o in targetB) { UnitAController unitInfo = o.GetComponent <UnitAController>(); float dist = Vector3.Distance(o.transform.position, currentPos); if (dist <= attackRange) { unitInfo.Health -= attackDamage; if (unitInfo.Health <= 0) { Destroy(o); } } } }
public void SpawnUnit(string unitType) { if (unitType == "Melee") { GameObject newUnit = Instantiate(MeleeUnit) as GameObject; newUnit.transform.position = gameObject.transform.position + offset; UnitAController newUnitInfo = newUnit.GetComponent <UnitAController>(); newUnitInfo.UnitType = unitType; } else { GameObject newUnit = Instantiate(RangedUnit) as GameObject; newUnit.transform.position = gameObject.transform.position + offset; UnitAController newUnitInfo = newUnit.GetComponent <UnitAController>(); newUnitInfo.UnitType = unitType; } }
// Update is called once per frame void Update() { GameObject[] checkA = GameObject.FindGameObjectsWithTag("TeamA"); foreach (GameObject o in checkA) { UnitAController newUnitInfo = o.GetComponent <UnitAController>(); if (newUnitInfo.AWin) { isGameOverA = true; newUnitInfo.AWin = false; } if (newUnitInfo.BWin) { isGameOverB = true; newUnitInfo.BWin = false; } } GameObject[] checkB = GameObject.FindGameObjectsWithTag("TeamB"); foreach (GameObject o in checkB) { UnitAController newUnitInfo = o.GetComponent <UnitAController>(); if (newUnitInfo.AWin) { isGameOverA = true; newUnitInfo.AWin = false; } if (newUnitInfo.BWin) { isGameOverB = true; newUnitInfo.BWin = false; } } if (isGameOverA) { gameOverText.text = "Game Over!\nWinner: Red Team"; isGameOverA = false; } if (isGameOverB) { gameOverText.text = "Game Over!\nWinner: Blue Team"; isGameOverB = false; } }
public void Save(string filename, GameObject[] objects) { Debug.Log("Game Saved "); FileStream outFile = new FileStream(filename, FileMode.Create, FileAccess.Write); StreamWriter writer = new StreamWriter(outFile); foreach (GameObject obj in objects) { if (obj.tag == "TeamA" || obj.tag == "TeamB") { UnitAController unitInfo = obj.GetComponent <UnitAController>(); writer.WriteLine(unitInfo.Save()); } else if (obj.tag == "Wizard") { WizardController unitInfo = obj.GetComponent <WizardController>(); writer.WriteLine(unitInfo.Save()); } else if (obj.tag == "rbA" || obj.tag == "rbB") { ResourceManagement unitInfo = obj.GetComponent <ResourceManagement>(); BuildingInfo buildingInfo = obj.GetComponent <BuildingInfo>(); writer.WriteLine(unitInfo.Save() + buildingInfo.Save()); } else if (obj.tag == "FactoryBuildingA" || obj.tag == "FactoryBuildingB") { BuildingInfo buildingInfo = obj.GetComponent <BuildingInfo>(); writer.WriteLine(obj.tag + " " + obj.transform.position.x + " " + obj.transform.position.y + " " + obj.transform.position.z + " " + buildingInfo.Save()); } else if (obj.tag == "Building") { SpawnWizard buildingSpawnInfo = obj.GetComponent <SpawnWizard>(); writer.WriteLine(obj.tag + " " + buildingSpawnInfo.UnitAmount + " " + obj.transform.position.x + " " + obj.transform.position.y + " " + obj.transform.position.z); } } writer.Close(); outFile.Close(); }
// Update is called once per frame void Update() { unitInfo.text = "Red Team Units:\n"; TeamBText.text = "Blue Team Units:\n"; WizardsText.text = "Wizards:\n"; GameObject[] teamAUnits = GameObject.FindGameObjectsWithTag("TeamA"); foreach (GameObject unit in teamAUnits) { UnitAController UnitInfo = unit.GetComponent <UnitAController>(); unitInfo.text += "Team: Red" + " | Unit Type: " + UnitInfo.UnitType + " | Health: " + UnitInfo.Health + "\n"; } GameObject[] teamBUnits = GameObject.FindGameObjectsWithTag("TeamB"); foreach (GameObject unit in teamBUnits) { UnitAController UnitInfo = unit.GetComponent <UnitAController>(); TeamBText.text += "Team: Blue" + " | Unit Type: " + UnitInfo.UnitType + " | Health: " + UnitInfo.Health + "\n"; } GameObject[] Wizards = GameObject.FindGameObjectsWithTag("Wizard"); foreach (GameObject unit in Wizards) { WizardController UnitInfo = unit.GetComponent <WizardController>(); WizardsText.text += "Team: Wizard" + " | Health: " + UnitInfo.Health + "\n"; } }
private void AttackUnit(GameObject closestUnit) { if (closestUnit.tag == "TeamA" || closestUnit.tag == "TeamB") { UnitAController unitInfo = closestUnit.GetComponent <UnitAController>(); Vector3 currentPos = transform.position; float dist = Vector3.Distance(closestUnit.transform.position, currentPos); if (this.unitType == "Ranged") { if (dist <= rangedAttackRange) { unitInfo.Health -= rangedAttackDamage; if (unitInfo.Health <= 0) { Debug.Log("Killed"); Destroy(closestUnit); } } else { Move(); } } else { if (dist <= meleeAttackRange) { unitInfo.Health -= meleeAttackDamage; if (unitInfo.Health <= 0) { Debug.Log("Killed by melee"); Destroy(closestUnit); } } else { Move(); } } } else if (closestUnit.tag == "Wizard") { WizardController unitInfo = closestUnit.GetComponent <WizardController>(); Vector3 currentPos = transform.position; float dist = Vector3.Distance(closestUnit.transform.position, currentPos); if (this.unitType == "Ranged") { if (dist <= rangedAttackRange) { unitInfo.Health -= rangedAttackDamage; if (unitInfo.Health <= 0) { Debug.Log("Killed"); Destroy(closestUnit); } } else { Move(); } } else { if (dist <= meleeAttackRange) { unitInfo.Health -= meleeAttackDamage; if (unitInfo.Health <= 0) { Debug.Log("Killed by melee"); Destroy(closestUnit); } } else { Move(); } } } else { BuildingInfo unitInfo = closestUnit.GetComponent <BuildingInfo>(); Vector3 currentPos = transform.position; float dist = Vector3.Distance(closestUnit.transform.position, currentPos); if (this.unitType == "Ranged") { if (dist <= rangedAttackRange) { unitInfo.Health -= rangedAttackDamage; if (unitInfo.Health <= 0) { Debug.Log("Killed"); Destroy(closestUnit); } } else { Move(); } } else { if (dist <= meleeAttackRange) { unitInfo.Health -= meleeAttackDamage; if (unitInfo.Health <= 0) { Debug.Log("Killed by melee"); Destroy(closestUnit); } } else { Move(); } } } }
public void Load(string filename) { Debug.Log("Loading unit"); FileStream inFile = new FileStream(filename, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(inFile); string recordIn; recordIn = reader.ReadLine(); while (recordIn != null) { string[] info = recordIn.Split(' '); int length = recordIn.IndexOf(" "); string firstField = recordIn.Substring(0, length); switch (firstField) { case "FactoryBuildingA": GameObject newBuildingA = Instantiate(FactoryBuildingA) as GameObject; newBuildingA.transform.position = new Vector3(float.Parse(info[1]), float.Parse(info[2]), float.Parse(info[3])); BuildingInfo buildingInfoA = newBuildingA.GetComponent <BuildingInfo>(); buildingInfoA.Health = int.Parse(info[4]); break; case "FactoryBuildingB": GameObject newBuildingB = Instantiate(FactoryBuildingB) as GameObject; newBuildingB.transform.position = new Vector3(float.Parse(info[1]), float.Parse(info[2]), float.Parse(info[3])); BuildingInfo buildingInfoB = newBuildingB.GetComponent <BuildingInfo>(); buildingInfoB.Health = int.Parse(info[4]); break; case "Building": GameObject newBuildingW = Instantiate(WizardBuilding) as GameObject; newBuildingW.transform.position = new Vector3(float.Parse(info[2]), float.Parse(info[3]), float.Parse(info[4])); SpawnWizard buildingInfoW = newBuildingW.GetComponent <SpawnWizard>(); buildingInfoW.UnitAmount = 10; break; case "rbA": GameObject newBuildingrbA = Instantiate(ResourceBuildingA) as GameObject; ResourceBuildingA.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5])); BuildingInfo buildingInfoRbA = newBuildingrbA.GetComponent <BuildingInfo>(); buildingInfoRbA.Health = int.Parse(info[6]); ResourceManagement resourceInfoA = ResourceBuildingA.GetComponent <ResourceManagement>(); resourceInfoA.ResourceTotal = int.Parse(info[1]); resourceInfoA.ResourcesGenerated = int.Parse(info[2]); break; case "rbB": GameObject newBuildingrbB = Instantiate(ResourceBuildingB) as GameObject; ResourceBuildingB.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5])); BuildingInfo buildingInfoRbB = newBuildingrbB.GetComponent <BuildingInfo>(); buildingInfoRbB.Health = int.Parse(info[6]); ResourceManagement resourceInfoB = ResourceBuildingB.GetComponent <ResourceManagement>(); resourceInfoB.ResourceTotal = int.Parse(info[1]); resourceInfoB.ResourcesGenerated = int.Parse(info[2]); break; case "TeamA": if (info[2] == "400") { GameObject teamAMeleeUnit = Instantiate(TeamAMeleeUnit) as GameObject; teamAMeleeUnit.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5])); UnitAController teamAMeleeUnitInfo = teamAMeleeUnit.GetComponent <UnitAController>(); teamAMeleeUnitInfo.Health = int.Parse(info[1]); teamAMeleeUnitInfo.UnitType = "Melee"; } else { GameObject teamARangedUnit = Instantiate(TeamARangedUnit) as GameObject; teamARangedUnit.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5])); UnitAController teamARangedUnitInfo = teamARangedUnit.GetComponent <UnitAController>(); teamARangedUnitInfo.Health = int.Parse(info[1]); teamARangedUnitInfo.UnitType = "Ranged"; } break; case "TeamB": if (info[2] == "400") { GameObject teamBMeleeUnit = Instantiate(TeamBMeleeUnit) as GameObject; teamBMeleeUnit.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5])); UnitAController teamBMeleeUnitInfo = teamBMeleeUnit.GetComponent <UnitAController>(); teamBMeleeUnitInfo.Health = int.Parse(info[1]); teamBMeleeUnitInfo.UnitType = "Melee"; } else { GameObject teamBRangedUnit = Instantiate(TeamBRangedUnit) as GameObject; teamBRangedUnit.transform.position = new Vector3(float.Parse(info[3]), float.Parse(info[4]), float.Parse(info[5])); UnitAController teamBRangedUnitInfo = teamBRangedUnit.GetComponent <UnitAController>(); teamBRangedUnitInfo.Health = int.Parse(info[1]); teamBRangedUnitInfo.UnitType = "Ranged"; } break; case "Wizard": Debug.Log("Wizard Spawn"); GameObject wizard = Instantiate(WizardUnit) as GameObject; wizard.transform.position = new Vector3(float.Parse(info[2]), float.Parse(info[3]), float.Parse(info[4])); WizardController wizardInfo = wizard.GetComponent <WizardController>(); wizardInfo.Health = int.Parse(info[1]); break; } recordIn = reader.ReadLine(); } reader.Close(); inFile.Close(); }