public void AttackOverkillDeath() { MeleeUnit testAttacker = new MeleeUnit(100, 101, 10, float.MaxValue); MeleeUnit testDefender = new MeleeUnit(100, 10, 10, float.MaxValue); testAttacker.AttackCost = 1; testAttacker.IsNotTesting = false; testAttacker.Attack(testDefender); int expected = 0; int actual = testDefender.Health; Assert.AreEqual(expected, actual); }
public void AgainstMeleeUnitAttackTest() { VehicleUnit testAttacker = new VehicleUnit(100, 10, 10, float.MaxValue); MeleeUnit testDefender = new MeleeUnit(100, 10, 10, float.MaxValue); testAttacker.AttackCost = 5; testAttacker.IsNotTesting = false; testAttacker.Attack(testDefender); int expected = 90; int actual = testDefender.Health; Assert.AreEqual(expected, actual); }
public void AttackIfAPIsGreaterThanAC() { MeleeUnit testAttacker = new MeleeUnit(100, 10, 10, float.MaxValue); MeleeUnit testDefender = new MeleeUnit(100, 10, 10, float.MaxValue); testAttacker.AttackCost = 3; testAttacker.IsNotTesting = false; testAttacker.Attack(testDefender); int expectedAC = 7; int actualAC = testAttacker.ActionPoints; int expctedHP = 90; int actualHP = testDefender.Health; Assert.AreEqual(expectedAC, actualAC); Assert.AreEqual(expctedHP, actualHP); }
public override void Combat(int type) //combat method for the wizard to attack the { foreach (Unit u in units) { if (u is MeleeUnit) { MeleeUnit M = (MeleeUnit)u; if (M.PosX == PosX - 1 && M.PosY == PosY - 1) { M.Health -= 1; } else if (M.PosX == PosX && M.PosY == PosY - 1) { M.Health -= 1; } else if (M.PosX == PosX + 1 && M.PosY == PosY - 1) { M.Health -= 1; } else if (M.PosX == PosX - 1 && M.PosY == PosY) { M.Health -= 1; } else if (M.PosX == PosX + 1 && M.PosY == PosY) { M.Health -= 1; } else if (M.PosX == PosX - 1 && M.PosY == PosY + 1) { M.Health -= 1; } else if (M.PosX == PosX && M.PosY == PosY + 1) { M.Health -= 1; } else if (M.PosX == PosX + 1 && M.PosY == PosY + 1) { M.Health -= 1; } } else if (u is RangedUnits) { RangedUnits R = (RangedUnits)u; if (R.PosX == PosX - 1 && R.PosY == PosY - 1) { R.Health -= 1; } else if (R.PosX == PosX && R.PosY == PosY - 1) { R.Health -= 1; } else if (R.PosX == PosX + 1 && R.PosY == PosY - 1) { R.Health -= 1; } else if (R.PosX == PosX - 1 && R.PosY == PosY) { R.Health -= 1; } else if (R.PosX == PosX + 1 && R.PosY == PosY) { R.Health -= 1; } else if (R.PosX == PosX - 1 && R.PosY == PosY + 1) { R.Health -= 1; } else if (R.PosX == PosX && R.PosY == PosY + 1) { R.Health -= 1; } else if (R.PosX == PosX + 1 && R.PosY == PosY + 1) { R.Health -= 1; } } } }
public override void CheckAttackRange(List <Unit> uni, List <buildings> build) { units = uni; building = build; closestUnit = ClosestEnemy(); int enemyType; int xDis = 0, yDis = 0; int uDistance = 10000, bDistance = 10000; int distance; if (closestUnit is MeleeUnit) { MeleeUnit M = (MeleeUnit)closestUnit; xDis = Mathf.Abs((PosX - M.PosX) * (PosX - M.PosX)); yDis = Mathf.Abs((PosY - M.PosY) * (PosY - M.PosY)); uDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } else if (closestUnit is RangedUnits) { RangedUnits R = (RangedUnits)closestUnit; xDis = Mathf.Abs((PosX - R.PosX) * (PosX - R.PosX)); yDis = Mathf.Abs((PosY - R.PosY) * (PosY - R.PosY)); uDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } if (units[0] != null) { if (uDistance < bDistance) { distance = uDistance; enemyType = 0; } else { distance = bDistance; enemyType = 1; } } else { distance = bDistance; enemyType = 1; } if (Health > MaxHealth * 0.5) { if (distance <= AttackRange) { IsAttacking = true; Combat(enemyType); } else { IsAttacking = false; Move(enemyType); } } else { Move(enemyType); } }
//this method places the units within the map public void UnitPlacing() { for (int k = 0; k < marrUnits.Length; k++) { int unitSelection; int Team; int name = r.Next(0, Names.Length); string placeTeam = ""; string placeSymbol = ""; int x = r.Next(0, 20); int y = r.Next(0, 20); unitSelection = r.Next(1, 3); switch (unitSelection) { case 1: //meleee unit of the rogues who are the villains Team = r.Next(1, 3); switch (Team) { case 1: placeTeam = "rogue"; placeSymbol = "M"; break; case 2: placeTeam = "rogue"; placeSymbol = "m"; break; } marrUnits[k] = new MeleeUnit(x, y, placeTeam, placeSymbol, Names[name]); break; case 2: //meleee unit of the rogues who are the villains Team = r.Next(1, 3); switch (Team) { case 1: placeTeam = "viking"; placeSymbol = "R"; break; case 2: placeTeam = "viking"; placeSymbol = "r"; break; } rarrUnits[k] = new RangedUnit(x, y, placeTeam, placeSymbol, Names[name]); break; } mapArray[marrUnits[k].YPosition, marrUnits[k].XPosition] = marrUnits[k].Symbol; mapArray[rarrUnits[k].YPosition, rarrUnits[k].XPosition] = rarrUnits[k].Symbol; } //for(int j=0;j<rarrUnits.Length;j++) //{ // int unitSelection; // int Team; // int name = r.Next(0, Names.Length); // string placeTeam = ""; // string placeSymbol = ""; // int x = r.Next(0, 20); // int y = r.Next(0, 20); // unitSelection = r.Next(1, 3); // switch (unitSelection) // { // case 1://meleee unit of the rogues who are the villains // Team = r.Next(1, 3); // switch (Team) // { // case 1: // placeTeam = "viking"; // placeSymbol = "R"; // break; // case 2: // placeTeam = "viking"; // placeSymbol = "r"; // break; // } // rarrUnits[j] = new RangedUnit(x, y, placeTeam, placeSymbol, Names[name]); // break; // } // mapArray[rarrUnits[j].YPosition, rarrUnits[j].XPosition] = rarrUnits[j].Symbol; //} }
public override Unit ReturnPosition(MeleeUnit[] enemyM, RangedUnit[] enemyR) { MeleeUnit MEnemy = null; RangedUnit rEnemy = null; int count = 0; int distance = 0; int distancer = 0; foreach (MeleeUnit u in enemyM) { if (u != null) { if (u.Alive && u.team != team) { if (u.X != x && u.Y != y) { if (count == 0) { count++; distance = ((Math.Abs(x) - Math.Abs(u.X)) + (Math.Abs(y) - Math.Abs(u.Y))); } else { if (distance > ((Math.Abs(x) - Math.Abs(u.X)) + (Math.Abs(y) - Math.Abs(u.Y)))) { distance = ((Math.Abs(x) - Math.Abs(u.X)) + (Math.Abs(y) - Math.Abs(u.Y))); MEnemy = u; } } } } } } count = 0; foreach (RangedUnit u in enemyR) { if (u != null) { if (u.alive && u.team != team) { if (u.X != x && u.Y != y) { if (count == 0) { count++; distancer = ((Math.Abs(x) - Math.Abs(u.x)) + (Math.Abs(y) - Math.Abs(u.y))); rEnemy = u; } if (distancer > ((Math.Abs(x) - Math.Abs(u.x)) + (Math.Abs(y) - Math.Abs(u.y)))) { distancer = ((Math.Abs(x) - Math.Abs(u.x)) + (Math.Abs(y) - Math.Abs(u.y))); rEnemy = u; } } } } } if (distance > distancer) { return(rEnemy); } else { return(MEnemy); } }
public void NoAttackIfACIsZero() { MeleeUnit testAttacker = new MeleeUnit(100, -100, 10, float.MaxValue); MeleeUnit testDefender = new MeleeUnit(100, 10, 10, float.MaxValue); testAttacker.AttackCost = 0; testAttacker.IsNotTesting = false; testAttacker.Attack(testDefender); int expectedAP = 10; int actualAP = testAttacker.ActionPoints; int expected = 100; int actual = testDefender.Health; Assert.AreEqual(expectedAP, actualAP); Assert.AreEqual(expected, actual); }
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 }
public void action() { foreach (Building b in map.Buildings) { if (b is FactoryBuilding) { FactoryBuilding f = (FactoryBuilding)b; if (f.Production_Speed % round == 0 && resources >= UnitCost) { map.Units.Add(f.SpawnUnit()); } } if (b is ResourceBuilding) { ResourceBuilding rs = (ResourceBuilding)b; resources = rs.GenerateResources(); } } for (int i = 0; i < map.Units.Count; i++) { if (map.Units[i] is MeleeUnit) { MeleeUnit mu = (MeleeUnit)map.Units[i]; if (mu.health <= mu.maxHealth * 0.25) // Running Away { mu.Move(r.Next(0, 4)); } else { (Unit closest, int distanceTo) = mu.EnemyDistance(map.Units); (Building closestbuilding, int distance) = mu.ClosestBuilding(map.Buildings); if (distanceTo < distance) //Checks if Unit is closer than building { //Check In Range if (distanceTo <= mu.attackRange) { mu.isAttack = true; mu.Combat(closest); if (mu.IsDead == true) //Returns resources if unit is dead { foreach (Building bg in map.Buildings) { if (bg is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bg; if (mu.team == rb.team) { rb.ResourcesGenerated++; } } } } } else //Move Towards { if (closest is MeleeUnit) { MeleeUnit closestMu = (MeleeUnit)closest; if (mu.xPos > closestMu.xPos) //North { mu.Move(0); } else if (mu.xPos < closestMu.xPos) //South { mu.Move(2); } else if (mu.yPos > closestMu.yPos) //West { mu.Move(3); } else if (mu.yPos < closestMu.yPos) //East { mu.Move(1); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (mu.xPos > closestRu.xPos) //North { mu.Move(0); } else if (mu.xPos < closestRu.xPos) //South { mu.Move(2); } else if (mu.yPos > closestRu.yPos) //West { mu.Move(3); } else if (mu.yPos < closestRu.yPos) //East { mu.Move(1); } } if (closest is WizzardUnit) { WizzardUnit closestwu = (WizzardUnit)closest; if (mu.xPos > closestwu.xPos) //North { mu.Move(0); } else if (mu.xPos < closestwu.xPos) //South { mu.Move(2); } else if (mu.yPos > closestwu.yPos) //West { mu.Move(3); } else if (mu.yPos < closestwu.yPos) //East { mu.Move(1); } } } } else { //Check In Range if (distanceTo <= mu.attackRange) { mu.isAttack = true; mu.Combat(closest); } else //Move Towards { if (closestbuilding is FactoryBuilding) { FactoryBuilding closestfb = (FactoryBuilding)closestbuilding; if (mu.xPos > closestfb.xPos) //North { mu.Move(1); } else if (mu.xPos < closestfb.xPos) //South { mu.Move(2); } else if (mu.yPos > closestfb.yPos) //West { mu.Move(3); } else if (mu.yPos < closestfb.yPos) //East { mu.Move(4); } } else if (closestbuilding is ResourceBuilding) { ResourceBuilding closestrb = (ResourceBuilding)closestbuilding; if (mu.xPos > closestrb.xPos) //North { mu.Move(1); } else if (mu.xPos < closestrb.xPos) //South { mu.Move(2); } else if (mu.yPos > closestrb.yPos) //West { mu.Move(3); } else if (mu.yPos < closestrb.yPos) //East { mu.Move(4); } } } } } } else if (map.Units[i] is RangedUnit) { RangedUnit ru = (RangedUnit)map.Units[i]; if (ru.health <= ru.maxHealth * 0.25) // Running Away is commented out to make for a more interesting battle - and some insta-deaths { ru.Move(r.Next(0, 4)); } else { (Unit closest, int distanceTo) = ru.EnemyDistance(map.Units); (Building closestbuilding, int distance) = ru.ClosestBuilding(map.Buildings); if (distanceTo < distance) //Checks if Unit is closer than building { //Check In Range if (distanceTo <= ru.attackRange) { ru.isAttack = true; ru.Combat(closest); if (ru.IsDead == true) //Returns resources if unit is dead { foreach (Building bg in map.Buildings) { if (bg is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bg; if (ru.team == rb.team) { rb.ResourcesGenerated++; } } } } } else //Move Towards { if (closest is MeleeUnit) { MeleeUnit closestMu = (MeleeUnit)closest; if (ru.xPos > closestMu.xPos) //North { ru.Move(0); } else if (ru.xPos < closestMu.xPos) //South { ru.Move(2); } else if (ru.yPos > closestMu.yPos) //West { ru.Move(3); } else if (ru.yPos < closestMu.yPos) //East { ru.Move(1); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (ru.xPos > closestRu.xPos) //North { ru.Move(0); } else if (ru.xPos < closestRu.xPos) //South { ru.Move(2); } else if (ru.yPos > closestRu.yPos) //West { ru.Move(3); } else if (ru.yPos < closestRu.yPos) //East { ru.Move(1); } } if (closest is WizzardUnit) { WizzardUnit closestMu = (WizzardUnit)closest; if (ru.xPos > closestMu.xPos) //North { ru.Move(0); } else if (ru.xPos < closestMu.xPos) //South { ru.Move(2); } else if (ru.yPos > closestMu.yPos) //West { ru.Move(3); } else if (ru.yPos < closestMu.yPos) //East { ru.Move(1); } } } } else { //Check In Range if (distanceTo <= ru.attackRange) { ru.isAttack = true; ru.Combat(closest); } else //Move Towards { if (closestbuilding is FactoryBuilding) { FactoryBuilding closestfb = (FactoryBuilding)closestbuilding; if (ru.xPos > closestfb.xPos) //North { ru.Move(1); } else if (ru.xPos < closestfb.xPos) //South { ru.Move(2); } else if (ru.yPos > closestfb.yPos) //West { ru.Move(3); } else if (ru.yPos < closestfb.yPos) //East { ru.Move(4); } } else if (closestbuilding is ResourceBuilding) { ResourceBuilding closestrb = (ResourceBuilding)closestbuilding; if (ru.xPos > closestrb.xPos) //North { ru.Move(1); } else if (ru.xPos < closestrb.xPos) //South { ru.Move(2); } else if (ru.yPos > closestrb.yPos) //West { ru.Move(3); } else if (ru.yPos < closestrb.yPos) //East { ru.Move(4); } } } } } } else if (map.Units[i] is WizzardUnit) { WizzardUnit wu = (WizzardUnit)map.Units[i]; if (wu.health <= wu.maxHealth * 0.50) // Wizzards will run away when on half health or less { wu.Move(r.Next(0, 4)); } else { (Unit closest, int distanceTo) = wu.EnemyDistance(map.Units); //Check In Range if (distanceTo <= wu.attackRange) { wu.isAttack = true; wu.Combat(closest); } else //Move Towards { if (closest is MeleeUnit) { MeleeUnit closestMu = (MeleeUnit)closest; if (wu.xPos > closestMu.xPos) //North { wu.Move(0); } else if (wu.xPos < closestMu.xPos) //South { wu.Move(2); } else if (wu.yPos > closestMu.yPos) //West { wu.Move(3); } else if (wu.yPos < closestMu.yPos) //East { wu.Move(1); } } else if (closest is RangedUnit) { RangedUnit closestRu = (RangedUnit)closest; if (wu.xPos > closestRu.xPos) //North { wu.Move(0); } else if (wu.xPos < closestRu.xPos) //South { wu.Move(2); } else if (wu.yPos > closestRu.yPos) //West { wu.Move(3); } else if (wu.yPos < closestRu.yPos) //East { wu.Move(1); } } } } } } }
//Displays the units onto the form public void Display() { for (int k = 0; k < 20; k++) { for (int l = 0; l < 20; l++) { Instantiate(floor, new Vector3(k, 0, l), Quaternion.identity); } } //Clears map GameObject[] tiles = GameObject.FindGameObjectsWithTag("floor"); foreach (GameObject u in tiles) { GameObject.Destroy(u); } //Adding Units foreach (Unit u in map.units) { GameObject gb = new GameObject(); if (u is MeleeUnit) { MeleeUnit mu = (MeleeUnit)u; if (mu.FactionType == 0) { GameObject GO = Instantiate(redMelee, new Vector3(mu.PosX, 0, mu.PosY), Quaternion.identity); unitCo = GO.GetComponent <UnitController>(); unitCo.DisplayHealth(mu.Health); } else { GameObject GO = Instantiate(blueMelee, new Vector3(mu.PosX, 0, mu.PosY), Quaternion.identity); unitCo = GO.GetComponent <UnitController>(); unitCo.DisplayHealth(mu.Health); } } else if (u is RangedUnit) { RangedUnit ru = (RangedUnit)u; if (ru.FactionType == 0) { GameObject GO = Instantiate(redRanged, new Vector3(ru.PosX, 0, ru.PosY), Quaternion.identity); unitCo = GO.GetComponent <UnitController>(); unitCo.DisplayHealth(ru.Health); } else { GameObject GO = Instantiate(blueRanged, new Vector3(ru.PosX, 0, ru.PosY), Quaternion.identity); unitCo = GO.GetComponent <UnitController>(); unitCo.DisplayHealth(ru.Health); } } else { WizardUnit wu = (WizardUnit)u; GameObject GO = Instantiate(wizard, new Vector3(wu.PosX, 0, wu.PosY), Quaternion.identity); unitCo = GO.GetComponent <UnitController>(); unitCo.DisplayHealth(wu.Health); } } //Adding Buildings foreach (Building bud in map.buildings) { GameObject gb = new GameObject(); if (bud is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bud; if (rb.FactionType == 0) { GameObject GO = Instantiate(redResourceFactory, new Vector3(rb.PosX, 0, rb.PosY), Quaternion.identity); unitCo = GO.GetComponent <UnitController>(); unitCo.DisplayHealth(rb.Health); } else { GameObject GO = Instantiate(blueResourceFactory, new Vector3(rb.PosX, 0, rb.PosY), Quaternion.identity); unitCo = GO.GetComponent <UnitController>(); unitCo.DisplayHealth(rb.Health); } } else { FactoryBuilding fb = (FactoryBuilding)bud; if (fb.FactionType == 0) { GameObject GO = Instantiate(redUnitFactory, new Vector3(fb.PosX, 0, fb.PosY), Quaternion.identity); unitCo = GO.GetComponent <UnitController>(); unitCo.DisplayHealth(fb.Health); } else { GameObject GO = Instantiate(blueUnitFactory, new Vector3(fb.PosX, 0, fb.PosY), Quaternion.identity); unitCo = GO.GetComponent <UnitController>(); unitCo.DisplayHealth(fb.Health); } } } }
public void NoAttackIfAPIsLessThanAC() { MeleeUnit testAttacker = new MeleeUnit(100, 100, 10, float.MaxValue); MeleeUnit testDefender = new MeleeUnit(100, 10, 10, float.MaxValue); testAttacker.AttackCost = 11; testAttacker.IsNotTesting = false; testAttacker.Attack(testDefender); int expectedAP = 10; int actualAP = testAttacker.ActionPoints; int expectedHealth = 100; int actualHealth = testDefender.Health; Assert.AreEqual(expectedAP, actualAP); Assert.AreEqual(expectedHealth, actualHealth); }
public void NoDamageWhenLessThanZero() { MeleeUnit testAttacker = new MeleeUnit(100, -100, 10, float.MaxValue); MeleeUnit testDefender = new MeleeUnit(100, 10, 10, float.MaxValue); testAttacker.AttackCost = 1; testAttacker.IsNotTesting = false; testAttacker.Attack(testDefender); int expected = 100; int actual = testDefender.Health; Assert.AreEqual(expected, actual); }
public override void CheckAttackRange(List <Unit> uni, List <buildings> build) { units = uni; building = build; closestUnit = ClosestEnemy(); closestBuilding = ClosestBuilding(); int enemyType; int xDis = 0, yDis = 0; int uDistance = 10000, bDistance = 10000; int distance; if (closestUnit is MeleeUnit) { MeleeUnit M = (MeleeUnit)closestUnit; xDis = Mathf.Abs((PosX - M.PosX) * (PosX - M.PosX)); yDis = Mathf.Abs((PosY - M.PosY) * (PosY - M.PosY)); uDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } else if (closestUnit is RangedUnits) { RangedUnits R = (RangedUnits)closestUnit; xDis = Mathf.Abs((PosX - R.PosX) * (PosX - R.PosX)); yDis = Mathf.Abs((PosY - R.PosY) * (PosY - R.PosY)); uDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } else if (closestUnit is WizzardUnits) { WizzardUnits R = (WizzardUnits)closestUnit; xDis = Mathf.Abs((PosX - R.PosX) * (PosX - R.PosX)); yDis = Mathf.Abs((PosY - R.PosY) * (PosY - R.PosY)); uDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } if (closestBuilding is FactoryBuildings) { FactoryBuildings FB = (FactoryBuildings)closestBuilding; xDis = Mathf.Abs((PosX - FB.PosX) * (PosX - FB.PosX)); yDis = Mathf.Abs((PosY - FB.PosY) * (PosY - FB.PosY)); bDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } else if (closestBuilding is ResourceBuildings) { ResourceBuildings RB = (ResourceBuildings)closestBuilding; xDis = Mathf.Abs((PosX - RB.PosX) * (PosX - RB.PosX)); yDis = Mathf.Abs((PosY - RB.PosY) * (PosY - RB.PosY)); bDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } if (units[0] != null) { if (uDistance < bDistance) { distance = uDistance; enemyType = 0; } else { distance = bDistance; enemyType = 1; } } else { distance = bDistance; enemyType = 1; } //Checks to see if they are below 25% health so they move rather than attacking if (Health > MaxHealth * 0.25) { if (distance <= AttackRange) { IsAttacking = true; Combat(enemyType); } else { IsAttacking = false; Move(enemyType); } } else { Move(enemyType); } }
public void Generate() { for (int x = 0; x < sizeX; x++) { for (int y = 0; y < sizeY; y++) { Object.Instantiate(Sprites[0], new Vector3(x, y, 5), Quaternion.identity); } } for (int i = 0; i < numUnits; i++) { int r = Random.Range(0, 2); if (r == 0) { MeleeUnit m = new MeleeUnit(Random.Range(0, sizeX), Random.Range(0, sizeY), "Soldier", 40, 3, 3, i % 2 == 0 ? 1 : 0, "M"); units.Add(m); numM++; } else { RangedUnit ru = new RangedUnit(Random.Range(0, sizeX), Random.Range(0, sizeY), "Archer", 40, 2, 5, 5, i % 2 == 0 ? 1 : 0, "R"); units.Add(ru); numR++; } } for (int j = 0; j < numBuilds; j++) { int r = Random.Range(0, 2); if (r == 0) { int f = Random.Range(0, 2); if (f == 0) { FactoryBuilding fb = new FactoryBuilding(Random.Range(0, sizeX), Random.Range(0, sizeY), 0, 80, 5, j % 2 == 0 ? 1 : 0, "FB"); builds.Add(fb); } else { FactoryBuilding fb = new FactoryBuilding(Random.Range(0, sizeX), Random.Range(0, sizeY), 1, 80, 5, j % 2 == 0 ? 1 : 0, "FB"); builds.Add(fb); } } else { ResourceBuilding rb = new ResourceBuilding(Random.Range(0, sizeX), Random.Range(0, sizeY), "Swords", 80, 2, 60, j % 2 == 0 ? 1 : 0); builds.Add(rb); } } if (numR < numM) { for (int k = 0; k < Random.Range(numR, numM); k++) { WizardUnit wu = new WizardUnit(Random.Range(0, sizeX), Random.Range(0, sizeY), "Wizro", 20, 1, 10, 3, "W"); units.Add(wu); } } else { for (int k = 0; k < Random.Range(numM, numR); k++) { WizardUnit wu = new WizardUnit(Random.Range(0, sizeX), Random.Range(0, sizeY), "Wizro", 20, 1, 10, 3, "W"); units.Add(wu); } } }
//Changes the x and y position towards the closest enemy or to run away public override void Move(int type) { //Moves towards closest enemey if (Health > MaxHealth * 0.25) { if (type == 0) { if (closestUnit is MeleeUnit) { MeleeUnit closestUnitM = (MeleeUnit)closestUnit; if (closestUnitM.PosX > posX && PosX < MapWidth - 1) { posX++; } else if (closestUnitM.PosX < posX && posX > 0) { posX--; } if (closestUnitM.PosY > posY && PosY < MapHeight - 1) { posY++; } else if (closestUnitM.PosY < posY && posY > 0) { posY--; } } else if (closestUnit is RangedUnit) { RangedUnit closestUnitR = (RangedUnit)closestUnit; if (closestUnitR.PosX > posX && PosX < MapWidth - 1) { posX++; } else if (closestUnitR.PosX < posX && posX > 0) { posX--; } if (closestUnitR.PosY > posY && PosY < MapHeight - 1) { posY++; } else if (closestUnitR.PosY < posY && posY > 0) { posY--; } } else if (closestUnit is WizardUnit) { WizardUnit closestUnitW = (WizardUnit)closestUnit; if (closestUnitW.PosX > posX && PosX < MapWidth - 1) { posX++; } else if (closestUnitW.PosX < posX && posX > 0) { posX--; } if (closestUnitW.PosY > posY && PosY < MapHeight - 1) { posY++; } else if (closestUnitW.PosY < posY && posY > 0) { posY--; } } } else { if (closestBuilding is FactoryBuilding) { FactoryBuilding closestBuildingFB = (FactoryBuilding)closestBuilding; if (closestBuildingFB.PosX > posX && PosX < MapHeight - 1) { posX++; } else if (closestBuildingFB.PosX < posX && posX > 0) { posX--; } if (closestBuildingFB.PosY > posY && PosY < MapWidth - 1) { posY++; } else if (closestBuildingFB.PosY < posY && posY > 0) { posY--; } } else if (closestBuilding is ResourceBuilding) { ResourceBuilding closestBuildingRB = (ResourceBuilding)closestBuilding; if (closestBuildingRB.PosX > posX && PosX < MapHeight - 1) { posX++; } else if (closestBuildingRB.PosX < posX && posX > 0) { posX--; } if (closestBuildingRB.PosY > posY && PosY < MapWidth - 1) { posY++; } else if (closestBuildingRB.PosY < posY && posY > 0) { posY--; } } } } else //Moves in random direction to run away { int direction = Random.Range(0, 4); if (direction == 0 && PosX < MapHeight - 1) { posX++; } else if (direction == 1 && posX > 0) { posX--; } else if (direction == 2 && posY < MapWidth - 1) { posY++; } else if (direction == 3 && posY > 0) { posY--; } } }
public override void Move(int type) { //Moves towards closest enemey if (Health > MaxHealth * 0.5) { if (type == 0) { if (closestUnit is MeleeUnit) { MeleeUnit closestUnitM = (MeleeUnit)closestUnit; if (closestUnitM.PosX > posX && PosX < 20) { posX++; } else if (closestUnitM.PosX < posX && posX > 0) { posX--; } if (closestUnitM.PosY > posY && PosY < 20) { posY++; } else if (closestUnitM.PosY < posY && posY > 0) { posY--; } } else if (closestUnit is RangedUnits) { RangedUnits closestUnitR = (RangedUnits)closestUnit; if (closestUnitR.PosX > posX && PosX < 20) { posX++; } else if (closestUnitR.PosX < posX && posX > 0) { posX--; } if (closestUnitR.PosY > posY && PosY < 20) { posY++; } else if (closestUnitR.PosY < posY && posY > 0) { posY--; } } } } else //Moves the unit in a direction that is determined by random { int direction = Random.Range(0, 4); if (direction == 0 && PosX < 19) { posX++; } else if (direction == 1 && posX > 0) { posX--; } else if (direction == 2 && posY < 19) { posY++; } else if (direction == 3 && posY > 0) { posY--; } } }
public void display() { foreach (Building bg in map.Buildings) { if (bg is FactoryBuilding) { FactoryBuilding fb = (FactoryBuilding)bg; if (fb.team == 0) { if (fb.IsDead == false) { GameObject obj = Instantiate(FactoryT1, new Vector2(fb.xPos, fb.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT1, new Vector2(fb.xPos, fb.yPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (fb.IsDead == false) { GameObject obj = Instantiate(FactoryT2, new Vector2(fb.xPos, fb.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT2, new Vector2(fb.xPos, fb.yPos), Quaternion.identity); obj.transform.parent = transform; } } } else if (bg is ResourceBuilding) { ResourceBuilding rb = (ResourceBuilding)bg; if (rb.team == 0) { if (rb.IsDead == false) { GameObject obj = Instantiate(ResourceT1, new Vector2(rb.xPos, rb.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT1, new Vector2(rb.xPos, rb.yPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (rb.IsDead == false) { GameObject obj = Instantiate(ResourceT2, new Vector2(rb.xPos, rb.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT2, new Vector2(rb.xPos, rb.yPos), Quaternion.identity); obj.transform.parent = transform; } } } } foreach (Unit u in map.Units) { if (u is MeleeUnit) { MeleeUnit mu = (MeleeUnit)u; if (mu.team == 0) { if (mu.IsDead == false) { GameObject obj = Instantiate(MeleeT1, new Vector2(mu.xPos, mu.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT1, new Vector2(mu.xPos, mu.yPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (mu.IsDead == false) { GameObject obj = Instantiate(MeleeT2, new Vector2(mu.xPos, mu.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT2, new Vector2(mu.xPos, mu.yPos), Quaternion.identity); obj.transform.parent = transform; } } } else if (u is RangedUnit) { RangedUnit ru = (RangedUnit)u; if (ru.team == 0) { if (ru.IsDead == false) { GameObject obj = Instantiate(RangedT1, new Vector2(ru.xPos, ru.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT1, new Vector2(ru.xPos, ru.yPos), Quaternion.identity); obj.transform.parent = transform; } } else { if (ru.IsDead == false) { GameObject obj = Instantiate(RangedT2, new Vector2(ru.xPos, ru.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(DeathT2, new Vector2(ru.xPos, ru.yPos), Quaternion.identity); obj.transform.parent = transform; } } } else if (u is WizzardUnit) { WizzardUnit wu = (WizzardUnit)u; if (wu.IsDead == false) { GameObject obj = Instantiate(WizzardTeam, new Vector2(wu.xPos, wu.yPos), Quaternion.identity); obj.transform.parent = transform; } else { GameObject obj = Instantiate(WizzardDeath, new Vector2(wu.xPos, wu.yPos), Quaternion.identity); obj.transform.parent = transform; } } } }
// Use this for initialization void Start() { int x = 0; int y = 0; time = 0; System.Random r = new System.Random(); for (int i = 0; i < 12; i++) { for (int k = 0; k < i; k++) { x = r.Next(0, 20); y = r.Next(0, 20); if (unit[k].Xpos == x && unit[k].Ypos == y) { k = 0; } } if (r.Next(1, 3) == 1) { if (i < 6) { unit[i] = new RangedUnit(x, y, 1); } else { unit[i] = new RangedUnit(x, y, 2); } } else { if (i < 6) { unit[i] = new MeleeUnit(x, y, 1); } else { unit[i] = new MeleeUnit(x, y, 2); } } } build[0] = new FactoryBuilding(r.Next(0, 20), r.Next(0, 20), 1); build[1] = new FactoryBuilding(r.Next(0, 20), r.Next(0, 20), 2); build[2] = new ResourceBuilding(r.Next(0, 20), r.Next(0, 20), 1); build[3] = new ResourceBuilding(r.Next(0, 20), r.Next(0, 20), 2); map.NewMap(unit); for (int i = 0; i < 20; i++) { for (int k = 0; k < 20; k++) { Instantiate(Resources.Load("Grass"), new Vector3(i, k, 1), Quaternion.identity); } } for (int i = 0; i < 12; i++) { if (unit[i].Atkrange > 1) { Instantiate(Resources.Load("Archer" + unit[i].Fact), new Vector3(unit[i].Xpos, unit[i].Ypos, -2), Quaternion.identity); } else { Instantiate(Resources.Load("Knight" + unit[i].Fact), new Vector3(unit[i].Xpos, unit[i].Ypos, -2), Quaternion.identity); } } Instantiate(Resources.Load("Resource" + build[3].Fact), new Vector3(build[3].Xpos, build[3].Ypos, -1), Quaternion.identity); Instantiate(Resources.Load("Resource" + build[2].Fact), new Vector3(build[2].Xpos, build[2].Ypos, -1), Quaternion.identity); Instantiate(Resources.Load("Factory" + build[1].Fact), new Vector3(build[1].Xpos, build[1].Ypos, -1), Quaternion.identity); Instantiate(Resources.Load("Factory" + build[0].Fact), new Vector3(build[0].Xpos, build[0].Ypos, -1), Quaternion.identity); //lblGameArea.Text = map.getMap() }
private void GameLogic() { ResourceDisplay(); //Working out if both teams are alive red = 0; blue = 0; foreach (Building B in map.buildings) { if (B is ResourceBuilding) { ResourceBuilding RB = (ResourceBuilding)B; if (RB.FactionType == 0) { red++; } else { blue++; } } else { FactoryBuilding FB = (FactoryBuilding)B; if (FB.FactionType == 0) { red++; } else { blue++; } } } foreach (Unit u in map.Units) { if (u is MeleeUnit) { MeleeUnit mu = (MeleeUnit)u; if (mu.FactionType == 0) { red++; } else { blue++; } } else if (u is RangedUnit) { RangedUnit ru = (RangedUnit)u; if (ru.FactionType == 0) { red++; } else { blue++; } } else { } } if (red > 0 && blue > 0)//Checks to see if both teams are still alive { //Reset resource values redResourcesLeft = 0; blueResourcesLeft = 0; foreach (Building b in map.buildings) { if (b is ResourceBuilding) { ResourceBuilding RB = (ResourceBuilding)b; if (RB.FactionType == 0) { redResources += RB.GenerateResource(); redResourcesLeft += RB.ResresourcesLeft; } else if (RB.FactionType == 1) { blueResources += RB.GenerateResource(); blueResourcesLeft += RB.ResresourcesLeft; } } else { FactoryBuilding FB = (FactoryBuilding)b; Unit u = FB.SpawnUnit(); if (FB.FactionType == 0 && redResources > FB.SpawnCost) { if (round % FB.SpawnSpeed == 0) { map.units.Add(u); if (u is MeleeUnit) { MeleeUnit M = (MeleeUnit)u; M.MapHeight = mapHeight; M.MapWidth = mapWidth; map.Units.Add(M); } else if (u is RangedUnit) { RangedUnit R = (RangedUnit)u; R.MapHeight = mapHeight; R.MapWidth = mapWidth; map.Units.Add(R); } redResources -= FB.SpawnCost; } } else if (FB.FactionType == 1 && blueResources > FB.SpawnCost) { if (round % FB.SpawnSpeed == 0) { if (u is MeleeUnit) { MeleeUnit M = (MeleeUnit)u; map.Units.Add(M); } else if (u is RangedUnit) { RangedUnit R = (RangedUnit)u; map.Units.Add(R); } blueResources -= FB.SpawnCost; } } } } foreach (Unit u in map.units) { u.CheckAttackRange(map.units, map.buildings); } round++; CheckDeath(); Display(); } else { Display(); runGame = false; if (red > blue) { txtWinText.text = "Red Wins!"; } else { txtWinText.text = "Blue Wins!"; } } }