public string FindUnit(int x, int y) { string info = "Item not found"; foreach (MeleeUnit u in GameMap.MeleeList) { if (u != null) { if (u.X == x && u.Y == y) { info = u.ToString(); } } } foreach (RangedUnit u in GameMap.RangedList) { if (u != null) { if (u.X == x && u.Y == y) { info = u.ToString(); } } } foreach (Wizard u in GameMap.WizardList) { if (u != null) { if (u.X == x && u.Y == y) { info = u.ToString(); } } } for (int k = 0; k < 4; k++) { if (k == 0 || k == 1) { RecourceBuilding u = (RecourceBuilding)GameMap.buildingList[k]; if (u != null) { if (u.X == x && u.Y == y) { info = u.ToString(); } } } else { Factory u = (Factory)GameMap.buildingList[k]; if (u != null) { if (u.X == x && u.Y == y) { info = u.ToString(); } } } } return(info); }
} // the working of the game private void Update() { if (timeCount != 60) { timeCount++; } else { if (pause) { int count = 0; for (int i = 0; i < 2; i++) { if (GameMap.WizardList[i] != null) { Wizard w = GameMap.WizardList[i]; CombatEngine(w); } } foreach (MeleeUnit u in GameMap.MeleeList) { if (u != null) { if (u.Alive) { count++; } } } if (count == 0) { meleeAlive = false; } if (meleeAlive) { foreach (MeleeUnit u in GameMap.MeleeList) { if (u != null) { if (u.Alive) { if (count > 0) { CombatEngine(u); } } } } } count = 0; foreach (RangedUnit u in GameMap.RangedList) { if (u != null) { if (u.Alive) { count++; } } } if (count == 0) { rangedAlive = false; } if (rangedAlive) { foreach (RangedUnit u in GameMap.RangedList) { if (u != null) { if (u.Alive) { if (count > 0) { CombatEngine(u); } } } } } else { // MessageBox.Show("Ranged units dead"); } int unitcount = 0; for (int i = 0; i < 15; i++) { if (GameMap.MeleeList[i] != null) { unitcount++; } } RecourceBuilding b1 = (RecourceBuilding)GameMap.buildingList[0]; if (b1.Remaining > 0) { b1.generate(b1.AmountTick); GameMap.availableBlue = GameMap.availableBlue + b1.AmountTick; } b1 = (RecourceBuilding)GameMap.buildingList[1]; if (b1.Remaining > 0) { b1.generate(b1.AmountTick); GameMap.availableYellow = GameMap.availableYellow + b1.AmountTick; } if (unitcount < 14) { RecourceBuilding b = (RecourceBuilding)GameMap.buildingList[0]; if (b.Remaining > 0) { GameMap.availableBlue = GameMap.availableBlue - 5; Factory factory = (Factory)GameMap.buildingList[2]; GameMap.MeleeList[unitcount] = (MeleeUnit)factory.SpawnUnit(); } b = (RecourceBuilding)GameMap.buildingList[1]; if (b.Remaining > 0) { GameMap.availableYellow = GameMap.availableYellow - 5; Factory factory = (Factory)GameMap.buildingList[3]; GameMap.MeleeList[unitcount + 1] = (MeleeUnit)factory.SpawnUnit(); } } timeCount = 0; GameCount++; updatebuttons(); } } }
private void updatebuttons() { foreach (GameObject G in GameObject.FindGameObjectsWithTag("Unit")) { Destroy(G); } Vector3 posP = new Vector3(9, -2, 0); Instantiate(parent, posP, Quaternion.identity); for (int x = 0; x < 20; x++) { for (int y = 0; y < 20; y++) { Map_01 UnitPos = new Map_01(); foreach (MeleeUnit u in GameMap.MeleeList) { if (u != null) { if (x == u.X && y == u.Y) { Vector3 pos; switch (u.team) { case "Blue": pos = new Vector3(x, y, -2); Instantiate(BlueS, pos, Quaternion.Euler(90, 180, 0)); break; case "Yellow": pos = new Vector3(x, y, -2); Instantiate(YellowS, pos, Quaternion.Euler(90, 180, 0)); break; } } } } foreach (RangedUnit u in GameMap.RangedList) { if (u != null) { if (x == u.X && y == u.Y) { Vector3 pos; switch (u.team) { case "Blue": pos = new Vector3(x, y, -2); Instantiate(BlueA, pos, Quaternion.Euler(90, 180, 0)); break; case "Yellow": pos = new Vector3(x, y, -2); Instantiate(YellowA, pos, Quaternion.Euler(90, 180, 0)); break; } } } } for (int i = 0; i < 4; i++) { if (i == 0 || i == 1) { RecourceBuilding u = (RecourceBuilding)GameMap.buildingList[i]; if (x == u.X && y == u.Y) { Vector3 pos; switch (u.Team) { case "Blue": pos = new Vector3(x, y, -2); Instantiate(BlueRec, pos, Quaternion.Euler(90, 180, 0)); break; case "Yellow": pos = new Vector3(x, y, -2); Instantiate(YellowRec, pos, Quaternion.Euler(90, 180, 0)); break; } } } else { Factory u = (Factory)GameMap.buildingList[i]; if (x == u.X && y == u.Y) { Vector3 pos; switch (u.Team) { case "Blue": pos = new Vector3(x, y, -2); Instantiate(BlueFac, pos, Quaternion.Euler(90, 180, 0)); break; case "Yellow": pos = new Vector3(x, y, -2); Instantiate(YellowFac, pos, Quaternion.Euler(90, 180, 0)); break; } } } } foreach (Wizard u in GameMap.WizardList) { if (u != null) { if (x == u.X && y == u.Y) { Vector3 pos; switch (u.team) { case "Blue": pos = new Vector3(x, y, -2); Instantiate(BlueW, pos, Quaternion.Euler(90, 180, 0)); break; case "Yellow": pos = new Vector3(x, y, -2); Instantiate(YellowW, pos, Quaternion.Euler(90, 180, 0)); break; } } } } } } //Update } // displays positions of units and buildings
public void Begin() { foreach (GameObject G in GameObject.FindGameObjectsWithTag("Unit")) { Destroy(G); } foreach (GameObject G in GameObject.FindGameObjectsWithTag("Building")) { Destroy(G); } GameMap = new Map_01(); GameMap.NewBattlefield(); TheGame = new GameEngine(); for (int x = 0; x < 20; x++) { for (int y = 0; y < 20; y++) { Map_01 UnitPos = new Map_01(); foreach (MeleeUnit u in GameMap.MeleeList) { if (u != null) { if (x == u.X && y == u.Y) { Vector3 pos; switch (u.team) { case "Blue": pos = new Vector3(x, y, -2); Instantiate(BlueS, pos, Quaternion.Euler(90, 180, 0)); break; case "Yellow": pos = new Vector3(x, y, -2); Instantiate(YellowS, pos, Quaternion.Euler(90, 180, 0)); break; } } } } foreach (RangedUnit u in GameMap.RangedList) { if (u != null) { if (x == u.X && y == u.Y) { Vector3 pos; switch (u.team) { case "Blue": pos = new Vector3(x, y, -2); Instantiate(BlueA, pos, Quaternion.Euler(90, 180, 0)); break; case "Yellow": pos = new Vector3(x, y, -2); Instantiate(YellowA, pos, Quaternion.Euler(90, 180, 0)); break; } } } } for (int i = 0; i < 4; i++) { if (i == 0 || i == 1) { RecourceBuilding u = (RecourceBuilding)GameMap.buildingList[i]; if (x == u.X && y == u.Y) { Vector3 pos; switch (u.Team) { case "Blue": pos = new Vector3(x, y, -2); Instantiate(BlueRec, pos, Quaternion.Euler(90, 180, 0)); break; case "Yellow": pos = new Vector3(x, y, -2); Instantiate(YellowRec, pos, Quaternion.Euler(90, 180, 0)); break; } } } else { Factory u = (Factory)GameMap.buildingList[i]; if (x == u.X && y == u.Y) { Vector3 pos; switch (u.Team) { case "Blue": pos = new Vector3(x, y, -2); Instantiate(BlueFac, pos, Quaternion.Euler(90, 180, 0)); Debug.Log("factory"); break; case "Yellow": pos = new Vector3(x, y, -2); Instantiate(YellowFac, pos, Quaternion.Euler(90, 180, 0)); break; } } } } foreach (Wizard u in GameMap.WizardList) { if (u != null) { if (x == u.X && y == u.Y) { Vector3 pos; switch (u.team) { case "Blue": pos = new Vector3(x, y, -2); Instantiate(BlueW, pos, Quaternion.Euler(90, 180, 0)); break; case "Yellow": pos = new Vector3(x, y, -2); Instantiate(YellowW, pos, Quaternion.Euler(90, 180, 0)); break; } } } } } } //Update }
public void NewBattlefield() { int melee = 5; int ranged = 5; int countM = 0; int count = 0; for (int i = 0; i < 2; i++) { Wizard wizard = new Wizard(); int rX = UnityEngine.Random.Range(1, 20); int rY = UnityEngine.Random.Range(1, 20); WizardList[i] = (Wizard)wizard.constuctor(rX, rY, i + 1); } for (int i = 0; i < 2; i++) { if (i == 0) { RecourceBuilding GoldMine = new RecourceBuilding(); Factory factory = new Factory(); int rX = UnityEngine.Random.Range(1, 20); int rY = UnityEngine.Random.Range(1, 20); GoldMine.Constructor(rX, rY, "Blue"); rX = UnityEngine.Random.Range(1, 20); rY = UnityEngine.Random.Range(1, 20); factory.Constructor(rX, rY, "Blue"); buildingList[0] = GoldMine; buildingList[2] = factory; } else { RecourceBuilding GoldMine = new RecourceBuilding(); Factory factory = new Factory(); int rX = UnityEngine.Random.Range(1, 20); int rY = UnityEngine.Random.Range(1, 20); GoldMine.Constructor(rX, rY, "Yellow"); rX = UnityEngine.Random.Range(1, 20); rY = UnityEngine.Random.Range(1, 20); factory.Constructor(rX, rY, "Yellow"); buildingList[1] = GoldMine; buildingList[3] = factory; } } for (int i = 0; i < melee; i++) // melee units { int rX = UnityEngine.Random.Range(1, 20); int rY = UnityEngine.Random.Range(1, 20); bool placed = false; while (!placed && countM < 5) { if (unitMap[rX, rY] == null) { int team = UnityEngine.Random.Range(1, 3); MeleeUnit mU = new MeleeUnit(); mU = (MeleeUnit)mU.constuctor(rX, rY, team); placed = true; unitMap[rX, rY] = "S"; MeleeList[countM] = mU; countM++; } else { rX = UnityEngine.Random.Range(1, 20); rY = UnityEngine.Random.Range(1, 20); } } } // melee generating for (int i = 0; i < ranged; i++) // Ranged units { int rX = UnityEngine.Random.Range(1, 20); int rY = UnityEngine.Random.Range(1, 20); bool placed = false; while (!placed && count < 5) { if (unitMap[rX, rY] == null && unitMap[rX, rY] != "S") { int team = UnityEngine.Random.Range(1, 3); RangedUnit mU = new RangedUnit(); mU = (RangedUnit)mU.constuctor(rX, rY, team); placed = true; unitMap[rX, rY] = "S"; RangedList[count] = mU; count++; } else { rX = UnityEngine.Random.Range(1, 20); rY = UnityEngine.Random.Range(1, 20); } } //ranged generation } // populate the map }