public override void Combat(int type) // the method to get the units to fight their enemies and shows them who to fight { if (type == 0) { if (ClosestUnit is MelleUnit) { MelleUnit M = (MelleUnit)ClosestUnit; M.Health -= Attack; } else if (ClosestUnit is RangedUnit) { RangedUnit R = (RangedUnit)ClosestUnit; R.Health -= Attack; } else if (ClosestUnit is WizardUnit) { WizardUnit Wu = (WizardUnit)ClosestUnit; Wu.Health -= Attack; } } else if (type == 1) { if (ClosestBuilding is FactoryBuilding) { FactoryBuilding Fb = (FactoryBuilding)ClosestBuilding; Fb.Health -= Attack; } else if (ClosestBuilding is ResourceBuilding) { ResourceBuilding RB = (ResourceBuilding)ClosestBuilding; RB.Health -= Attack; } } }
public override Units Position() //checks the Position of the Buildings and which positions to spawn units { int Xdis = 0, Ydis = 0; double Distance = 1000; double temp = 1000; Units Target = null; foreach (Units b in units) { if (b is RangedUnit) { RangedUnit Fb = (RangedUnit)b; if (FactionType != b.factionType) { Xdis = Math.Abs(PosX - Fb.PosX) * (PosX - Fb.PosX); Ydis = Math.Abs(PosY - Fb.PosY) * (PosY - Fb.PosY); Distance = Math.Round(Math.Sqrt(Xdis + Ydis), 0); } } else if (b is MelleUnit) { MelleUnit Rb = (MelleUnit)b; if (FactionType != b.factionType) { Xdis = Math.Abs(PosX - Rb.PosX) * (PosX - Rb.PosX); Ydis = Math.Abs(PosY - Rb.PosY) * (PosY - Rb.PosY); Distance = Math.Round(Math.Sqrt(Xdis + Ydis), 0); } } else if (b is WizardUnit) { WizardUnit Wu = (WizardUnit)b; if (FactionType != b.factionType) { Xdis = Math.Abs(PosX - Wu.PosX) * (PosX - Wu.PosX); Ydis = Math.Abs(PosY - Wu.PosY) * (PosY - Wu.PosY); Distance = Math.Round(Math.Sqrt(Xdis + Ydis), 0); } } if (Distance < temp) { temp = Distance; Target = b; } } return(Target); }
public void Populate() // method used to populate the map full of units { for (int i = 0; i < mapWidth; i++) { for (int j = 0; j < mapHeight; j++) { map[i, j] = " "; } } foreach (Units u in units) //// { if (u is RangedUnit) { RangedUnit r = (RangedUnit)u; uniMap[r.PosY, r.PosX] = u; } else if (u is MelleUnit) { MelleUnit m = (MelleUnit)u; uniMap[m.PosY, m.PosX] = u; } else if (u is WizardUnit) { WizardUnit w = (WizardUnit)u; uniMap[w.PosY, w.PosX] = u; } } foreach (Units u in units) { uniMap[u.posY, u.posX] = u; ////// } foreach (Units u in rangedUnit) { map[u.posY, u.posX] = "R"; } foreach (Units u in melleUnit) { map[u.posY, u.posX] = "M"; } foreach (WizardUnit u in wizardUnits) { map[u.posY, u.posX] = "W"; } }
public override void Move(int type) // all the move functions for the units once they have spawned and the the Buildings themselves { if (Health > MaxHealth * 0.25) { if (type == 0) { if (ClosestUnit is MelleUnit) { MelleUnit closestUnitM = (MelleUnit)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 RangedUnit) { RangedUnit closestUnitR = (RangedUnit)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 if (ClosestUnit is WizardUnit) // showing the new wizard who he can attack and when he can attack them { WizardUnit closestUnitW = (WizardUnit)ClosestUnit; if (closestUnitW.PosX > posX && PosX < 20) { posX++; } else if (closestUnitW.PosX < posX && posX > 0) { posX--; } if (closestUnitW.PosY > posY && PosY < 20) { posY++; } else if (closestUnitW.PosY < posY && posY > 0) { posY--; } } } else { if (ClosestBuilding is FactoryBuilding) { FactoryBuilding closestBuildingFB = (FactoryBuilding)ClosestBuilding; if (closestBuildingFB.PosX > posX && PosX < 20) { posX++; } else if (closestBuildingFB.PosX < posX && posX > 0) { posX--; } if (closestBuildingFB.PosY > posY && PosY < 20) { posY++; } else if (closestBuildingFB.PosY < posY && posY > 0) { posY--; } } else if (ClosestBuilding is ResourceBuilding) { ResourceBuilding closestBuildingRB = (ResourceBuilding)ClosestBuilding; if (closestBuildingRB.PosX > posX && PosX < 19) { posX++; } else if (closestBuildingRB.PosX < posX && posX > 0) { posX--; } if (closestBuildingRB.PosY > posY && PosY < 19) { posY++; } else if (closestBuildingRB.PosY < posY && posY > 0) { posY--; } } } } else { int direction = r.Next(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 GenerateBattleField() // method to allow the random number of units, including the ranged and the melee units { for (int i = 0; i < BuildingNum; i++) { int UnitNum = Rd.Next(0, 2); string UnitName; if (UnitNum == 0) { UnitName = "Melee"; } else { UnitName = "Ranged"; } ResourceBuilding DiamondMine = new ResourceBuilding(0, 0, 100, Faction.Hero, "◘", 10); // setting the values and symbols for the diamond mines diamondMines.Add(DiamondMine); FactoryBuilding barrack = new FactoryBuilding(0, 0, 100, Faction.Hero, "┬", Rd.Next(3, 10), UnitName); // setting the values and symbols for the barracks barracks.Add(barrack); } for (int i = 0; i < BuildingNum; i++) { int UnitNum = Rd.Next(0, 2); string UnitName; if (UnitNum == 0) { UnitName = "Melee"; } else { UnitName = "Ranged"; } ResourceBuilding DiamondMine = new ResourceBuilding(0, 0, 100, Faction.Villain, "◘", 10); diamondMines.Add(DiamondMine); FactoryBuilding barrack = new FactoryBuilding(0, 0, 100, Faction.Villain, "┬", Rd.Next(3, 10), UnitName); barracks.Add(barrack); } for (int i = 0; i < BuildingNum; i++) { WizardUnit wizard = new WizardUnit("Wizard", 0, 0, 15, 1, 3, 1, Faction.Neutral, "≈", false); //setting values for the new wizard unit wizardUnits.Add(wizard); } foreach (ResourceBuilding u in diamondMines) { for (int i = 0; i < diamondMines.Count; i++) { int xPos = Rd.Next(0, mapHeight); int yPos = Rd.Next(0, mapWidth); while (xPos == diamondMines[i].PosX && yPos == diamondMines[i].PosY && xPos == barracks[i].PosX && yPos == barracks[i].PosY) { xPos = Rd.Next(0, mapHeight); yPos = Rd.Next(0, mapWidth); } u.PosX = xPos; u.PosY = yPos; buildingMap[u.PosX, u.PosX] = (Building)u; } buildings.Add(u); } foreach (FactoryBuilding u in barracks) { for (int i = 0; i < barracks.Count; i++) { int xPos = Rd.Next(0, mapHeight); int yPos = Rd.Next(0, mapWidth); while (xPos == barracks[i].PosX && yPos == barracks[i].PosY && xPos == diamondMines[i].PosX && yPos == diamondMines[i].PosY) { xPos = Rd.Next(0, mapHeight); yPos = Rd.Next(0, mapWidth); } u.PosX = xPos; u.PosY = yPos; buildingMap[u.PosY, u.PosX] = (Building)u; } buildings.Add(u); u.SpawnPointY = u.PosY; if (u.PosX < 19) { u.SpawnPointX = u.PosX + 1; } else { u.SpawnPointX = u.PosX - 1; } } foreach (WizardUnit u in wizardUnits) // placing the wizard units randomly throughout the map { for (int i = 0; i < wizardUnits.Count; i++) { int xPos = Rd.Next(0, mapHeight); int yPos = Rd.Next(0, mapWidth); while (xPos == diamondMines[i].PosX && yPos == diamondMines[i].PosY && xPos == barracks[i].PosX && yPos == barracks[i].PosY && xPos == wizardUnits[i].PosX && yPos == wizardUnits[i].PosY) { xPos = Rd.Next(0, mapHeight); yPos = Rd.Next(0, mapWidth); } u.PosX = xPos; u.PosY = yPos; uniMap[u.PosX, u.PosX] = (Units)u; } units.Add(u); } Populate(); PlaceBuildings(); }