public override void Combat(int type) { if (type == 0) { if (closestUnit is MeleeUnit) { MeleeUnit M = (MeleeUnit)closestUnit; M.Health -= Attack; } else if (closestUnit is RangedUnit) { RangedUnit R = (RangedUnit)closestUnit; R.Health -= Attack; } else if (closestUnit is WizzardUnit) { WizzardUnit R = (WizzardUnit)closestUnit; R.Health -= Attack; } } else if (type == 1) { if (closestBuilding is FactoryBuildings) { FactoryBuildings FB = (FactoryBuildings)closestBuilding; FB.Health -= Attack; } else if (closestBuilding is ResourceBuilding) { ResourceBuilding RB = (ResourceBuilding)closestBuilding; RB.Health -= Attack; } } }
public void PlaceBuildings() //method that places the buidlings on the map { for (int i = 0; i < mapWidth; i++) { for (int j = 0; j < mapHeight; j++) { buildingMap[i, j] = null; } } foreach (buildings u in buildings) { if (u is FactoryBuildings) { FactoryBuildings Factory = (FactoryBuildings)u; buildingMap[Factory.PosY, Factory.PosX] = u; } else if (u is ResourceBuilding) { ResourceBuilding factory = (ResourceBuilding)u; buildingMap[factory.PosY, factory.PosX] = u; } } foreach (ResourceBuilding u in BitCoinMine) { map[u.PosY, u.PosX] = "RB"; } foreach (FactoryBuildings u in Barracks) { map[u.PosY, u.PosX] = "FB"; } }
public buildings ClosestBuilding() { int xDis = 0, yDis = 0; double distance = 1000; double temp = 1000; buildings target = null; foreach (buildings u in building) { if (u is FactoryBuildings) { FactoryBuildings b = (FactoryBuildings)u; if (factionType != b.Faction) { xDis = Math.Abs((PosX - b.PosX) * (PosX - b.PosX)); yDis = Math.Abs((PosY - b.PosY) * (PosY - b.PosY)); distance = Math.Round(Math.Sqrt(xDis + yDis), 0); } } else if (u is ResourceBuilding) { ResourceBuilding b = (ResourceBuilding)u; if (factionType != b.Faction) { xDis = Math.Abs((PosX - b.PosX) * (PosX - b.PosX)); yDis = Math.Abs((PosY - b.PosY) * (PosY - b.PosY)); distance = Math.Round(Math.Sqrt(xDis + yDis), 0); } } if (distance < temp) { temp = distance; target = u; } } return(target); }
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 < 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 WizzardUnit) { WizzardUnit closestUnitR = (WizzardUnit)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 (closestBuilding is FactoryBuildings) { FactoryBuildings closestBuildingFB = (FactoryBuildings)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 < 20) { posX++; } else if (closestBuildingRB.PosX < posX && posX > 0) { posX--; } if (closestBuildingRB.PosY > posY && PosY < 20) { posY++; } else if (closestBuildingRB.PosY < posY && posY > 0) { posY--; } } } } else //Moves the unit in a direction that is determined by random { 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 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 = Math.Abs((PosX - M.PosX) * (PosX - M.PosX)); yDis = Math.Abs((PosY - M.PosY) * (PosY - M.PosY)); uDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } else if (closestUnit is RangedUnit) { RangedUnit R = (RangedUnit)closestUnit; xDis = Math.Abs((PosX - R.PosX) * (PosX - R.PosX)); yDis = Math.Abs((PosY - R.PosY) * (PosY - R.PosY)); uDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } else if (closestUnit is WizzardUnit) { WizzardUnit R = (WizzardUnit)closestUnit; xDis = Math.Abs((PosX - R.PosX) * (PosX - R.PosX)); yDis = Math.Abs((PosY - R.PosY) * (PosY - R.PosY)); uDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } if (closestBuilding is FactoryBuildings) { FactoryBuildings FB = (FactoryBuildings)closestBuilding; xDis = Math.Abs((PosX - FB.PosX) * (PosX - FB.PosX)); yDis = Math.Abs((PosY - FB.PosY) * (PosY - FB.PosY)); bDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } else if (closestBuilding is ResourceBuilding) { ResourceBuilding RB = (ResourceBuilding)closestBuilding; xDis = Math.Abs((PosX - RB.PosX) * (PosX - RB.PosX)); yDis = Math.Abs((PosY - RB.PosY) * (PosY - RB.PosY)); bDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } 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 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.Overwatch, "**"); BitCoinMine.Add(DiamondMine); FactoryBuildings Barrack = new FactoryBuildings(0, 0, 100, Faction.Overwatch, "$$", Rd.Next(3, 10), UnitName); 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.Talon, "**"); BitCoinMine.Add(DiamondMine); FactoryBuildings barrack = new FactoryBuildings(0, 0, 100, Faction.Talon, "$$", Rd.Next(3, 10), UnitName); Barracks.Add(barrack); } for (int i = 0; i < BuildingNum; i++) { WizzardUnit wizzard = new WizzardUnit("Wizard", 0, 0, Faction.Neutral, 20, 2, 3, 1, "^", false); wizzardUnits.Add(wizzard); } foreach (ResourceBuilding u in BitCoinMine) { for (int i = 0; i < BitCoinMine.Count; i++) { int xPos = Rd.Next(0, mapHeight); int yPos = Rd.Next(0, mapWidth); while (xPos == BitCoinMine[i].PosX && yPos == BitCoinMine[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.PosY, u.PosX] = (buildings)u; buildings.Add(u); } foreach (FactoryBuildings 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 == BitCoinMine[i].PosX && yPos == BitCoinMine[i].PosY) { xPos = Rd.Next(0, mapHeight); yPos = Rd.Next(0, mapWidth); } u.PosX = xPos; u.PosY = yPos; } buildingMap[u.PosY, u.PosX] = (buildings)u; buildings.Add(u); u.SpawnPointY = u.PosY; if (u.PosX < 19) { u.SpawnPointX = u.PosX + 1; } else { u.SpawnPointX = u.PosX - 1; } } foreach (WizzardUnit u in wizzardUnits) { for (int i = 0; i < wizzardUnits.Count; i++) { int xPos = Rd.Next(0, mapHeight); int yPos = Rd.Next(0, mapWidth); while (xPos == BitCoinMine[i].PosX && yPos == BitCoinMine[i].PosY && xPos == Barracks[i].PosX && yPos == Barracks[i].PosY && xPos == wizzardUnits[i].PosX && yPos == wizzardUnits[i].PosY) { xPos = Rd.Next(0, mapHeight); yPos = Rd.Next(0, mapWidth); } u.PosX = xPos; u.PosY = yPos; } uniMap[u.PosY, u.PosX] = (Unit)u; units.Add(u); } Populate(); PlaceBuildings(); }
public void GameEng() { int hero = 0; int villian = 0; foreach (ResourceBuilding u in m.BitCoinMine) { if (u.Faction == Faction.Overwatch) { hero++; } else { villian++; } } foreach (FactoryBuildings u in m.Barracks) { if (u.Faction == Faction.Overwatch) { hero++; } else { villian++; } } foreach (Unit u in m.units) { if (u.factionType == Faction.Overwatch) { villian++; } else { villian++; } } if (hero > 0 && villian > 0) { foreach (ResourceBuilding Rb in m.BitCoinMine) { Rb.GenerateResources(); } foreach (FactoryBuildings Fb in m.Barracks) { if (Round % Fb.ProductionSpeed == 0) { m.SpawnUnits(Fb.SpawnPointX, Fb.SpawnPointY, Fb.Faction, Fb.SpawnUnits()); } } foreach (Unit u in m.units) { u.CheckAttackRange(m.units, m.buildings); } m.Populate(); m.PlaceBuildings(); Round++; Placebuttons(); } else { m.Populate(); m.PlaceBuildings(); Placebuttons(); GameTick.Enabled = false; if (hero > villian) { MessageBox.Show("Hero Wins on Round: " + Round); } else { MessageBox.Show("Villian Wins on Round: " + Round); } } for (int i = 0; i < m.rangedUnits.Count; i++) { if (m.rangedUnits[i].Death()) { m.map[m.rangedUnits[i].posX, m.rangedUnits[i].posX] = ""; m.rangedUnits.RemoveAt(i); } } for (int i = 0; i < m.meleeUnits.Count; i++) { if (m.meleeUnits[i].Death()) { m.map[m.meleeUnits[i].posX, m.meleeUnits[i].posX] = ""; m.meleeUnits.RemoveAt(i); } } for (int i = 0; i < m.units.Count; i++) { if (m.units[i].Death()) { m.map[m.units[i].posX, m.units[i].posX] = ""; m.units.RemoveAt(i); } } for (int i = 0; i < m.Barracks.Count; ++i) { if (m.Barracks[i].Destruction()) { m.map[m.Barracks[i].PosX, m.Barracks[i].PosY] = ""; m.Barracks.RemoveAt(i); } } for (int i = 0; i < m.BitCoinMine.Count; ++i) { if (m.BitCoinMine[i].Destruction()) { m.map[m.BitCoinMine[i].PosX, m.BitCoinMine[i].PosY] = ""; m.BitCoinMine.RemoveAt(i); } } for (int i = 0; i < m.buildings.Count; ++i) { if (m.buildings[i].Destruction()) { if (m.buildings[i] is FactoryBuildings) { FactoryBuildings FB = (FactoryBuildings)m.buildings[i]; m.map[FB.PosX, FB.PosY] = ""; } else if (m.buildings[i] is ResourceBuilding) { ResourceBuilding RB = (ResourceBuilding)m.buildings[i]; m.map[RB.PosX, RB.PosY] = ""; } m.buildings.RemoveAt(i); } } }
public void Placebuttons() //places the buttons in the text box to create the map { gb1.Controls.Clear(); Size btnSize = new Size(30, 30); for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { Button btn = new Button(); btn.Size = btnSize; btn.Location = new Point(x * 30, y * 30); if (m.map[x, y] == "R") { btn.Text = "︻デ═一"; btn.Name = m.uniMap[x, y].ToString(); btn.Click += MyButtonClick; if (m.uniMap[x, y].factionType == Faction.Overwatch) { btn.BackColor = Color.Red; } else { btn.BackColor = Color.Green; } } else if (m.map[x, y] == "M") { btn.Text = "▬===>"; btn.Name = m.uniMap[x, y].ToString(); btn.Click += MyButtonClick; if (m.uniMap[x, y].factionType == Faction.Overwatch) { btn.BackColor = Color.Red; } else { btn.BackColor = Color.Green; } } else if (m.map[x, y] == "W") { btn.Text = "^"; btn.Name = m.uniMap[x, y].ToString(); btn.Click += MyButtonClick; btn.BackColor = Color.Orange; } else if (m.map[x, y] == "FB") { FactoryBuildings FB = (FactoryBuildings)m.buildingMap[x, y]; btn.Text = FB.Symbol; if (FB.Faction == Faction.Overwatch) { btn.BackColor = Color.Red; } else { btn.BackColor = Color.Green; } btn.Name = m.buildingMap[x, y].ToString(); btn.Click += MyButtonClick; } else if (m.map[x, y] == "RB") { ResourceBuilding RB = (ResourceBuilding)m.buildingMap[x, y]; btn.Text = RB.Symbol; if (RB.Faction == Faction.Overwatch) { btn.BackColor = Color.Red; } else { btn.BackColor = Color.Green; } btn.Name = m.buildingMap[x, y].ToString(); btn.Click += MyButtonClick; } buttons[x, y] = btn; } } for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { gb1.Controls.Add(buttons[x, y]); } } }