//Deals damage to closest unit if they are in attack range 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 WizardUnit) { WizardUnit W = (WizardUnit)closestUnit; W.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; } } }
//finds and returns the closest enemy public override Unit ClosestEnemy() { int xDis = 0, yDis = 0; double distance = 1000; double temp = 1000; Unit target = null; foreach (Unit u in units) { if (u is RangedUnit) { RangedUnit b = (RangedUnit)u; if (FactionType != b.FactionType) { 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 MeleeUnit) { MeleeUnit b = (MeleeUnit)u; if (FactionType != b.FactionType) { 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 (u is WizardUnit) { WizardUnit b = (WizardUnit)u; if (FactionType != b.FactionType) { 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); }
//Places the units on a string representation of the 20x20 map public void PlaceUnits() { for (int i = 0; i < mapWidth; i++) { for (int j = 0; j < mapHeight; j++) { map[i, j] = " "; } } foreach (Unit u in units) { if (u is RangedUnit) { RangedUnit r = (RangedUnit)u; unitMap[r.PosY, r.PosX] = u; } else if (u is MeleeUnit) { MeleeUnit m = (MeleeUnit)u; unitMap[m.PosY, m.PosX] = u; } else if (u is WizardUnit) { WizardUnit w = (WizardUnit)u; unitMap[w.PosY, w.PosX] = u; } } foreach (RangedUnit u in rangedUnits) { map[u.PosY, u.PosX] = "R"; } foreach (MeleeUnit u in meleeUnits) { map[u.PosY, u.PosX] = "M"; } foreach (WizardUnit u in wizardUnits) { map[u.PosY, u.PosX] = "W"; } }
//Creates the unit objects and randomises thier x and y positions public void GenerateBattlefeild() { //Buildings spawning for (int i = 0; i < buildingNum; i++) { int unitTypeN = rd.Next(0, 2); string unitName; if (unitTypeN == 0) { unitName = "Ranged"; } else { unitName = "Melee"; } FactoryBuilding factory = new FactoryBuilding(0, 0, 100, "|^|", Faction.Dire, rd.Next(3, 10), unitName, rd.Next(50, 71)); factories.Add(factory); ResourceBuilding mine = new ResourceBuilding(0, 0, 100, "|V|", Faction.Dire, rd.Next(3, 10), ResourceType.Gold); mines.Add(mine); } for (int i = 0; i < buildingNum; i++) { int unitTypeN = rd.Next(0, 2); string unitName; if (unitTypeN == 0) { unitName = "Ranged"; } else { unitName = "Melee"; } FactoryBuilding factory = new FactoryBuilding(0, 0, 100, "|^|", Faction.Radient, rd.Next(3, 10), unitName, rd.Next(50, 71)); factories.Add(factory); ResourceBuilding mine = new ResourceBuilding(0, 0, 100, "|V|", Faction.Radient, rd.Next(3, 10), ResourceType.Gold); mines.Add(mine); } for (int i = 0; i < buildingNum; i++) { WizardUnit wizard = new WizardUnit("Wizard", 0, 0, Faction.Neutral, 20, 2, 3, 1, "^", false); wizard.MapHeight = mapHeight; wizard.MapWidth = mapWidth; wizardUnits.Add(wizard); } foreach (FactoryBuilding u in factories) { for (int i = 0; i < factories.Count; i++) { int xPos = rd.Next(0, mapHeight); int yPos = rd.Next(0, mapWidth); while (xPos == factories[i].PosX && yPos == factories[i].PosY && xPos == 0 && yPos == 0) { 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 < mapHeight - 1) { u.SpawnPointX = u.PosX + 1; } else { u.SpawnPointX = u.PosX - 1; } } foreach (ResourceBuilding u in mines) { for (int i = 0; i < mines.Count; i++) { int xPos = rd.Next(0, mapHeight); int yPos = rd.Next(0, mapWidth); while (xPos == mines[i].PosX && yPos == mines[i].PosY || xPos == factories[i].PosX && yPos == factories[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); } foreach (WizardUnit u in wizardUnits) { for (int i = 0; i < wizardUnits.Count; i++) { int xPos = rd.Next(0, mapHeight); int yPos = rd.Next(0, mapWidth); while (xPos == mines[i].PosX && yPos == mines[i].PosY || xPos == factories[i].PosX && yPos == factories[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; } unitMap[u.PosY, u.PosX] = (Unit)u; units.Add(u); } PlaceUnits(); PlaceBuildings(); }
//Checks to see if the closest enemy is in attack range and if they are calls combat or move if they aren't public override void CheckAttackRange(List <Unit> uni, List <Building> build) { units = uni; buildings = build; closestUnit = ClosestEnemy(); closestBuilding = ClosestEnemyBuilding(); 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 WizardUnit) { WizardUnit W = (WizardUnit)closestUnit; xDis = Math.Abs((PosX - W.PosX) * (PosX - W.PosX)); yDis = Math.Abs((PosY - W.PosY) * (PosY - W.PosY)); uDistance = (int)Math.Round(Math.Sqrt(xDis + yDis), 0); } if (closestBuilding is FactoryBuilding) { FactoryBuilding FB = (FactoryBuilding)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); } }
//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 = r.Next(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--; } } }
//Places the buttons on the form and puts the units in the buttons public void Placebuttons() { gbMap.Controls.Clear(); Size btnSize = new Size(30, 30); for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { if (m.map[x, y] == "R")//If the string map representation has an 'R' for a ranged unit { Button btn = new Button(); btn.Size = btnSize; btn.Location = new Point(x * 30, y * 30); if (m.unitMap[x, y] is RangedUnit) { RangedUnit R = (RangedUnit)m.unitMap[x, y]; btn.Text = R.Symbol; if (R.FactionType == Faction.Dire) { btn.BackColor = Color.Red; } else if (R.FactionType == Faction.Radient) { btn.BackColor = Color.Green; } btn.Name = m.unitMap[x, y].ToString(); btn.Click += MyButtonClick; gbMap.Controls.Add(btn); } } else if (m.map[x, y] == "M")//If the string map representation has an 'M' for a melee unit { Button btn = new Button(); btn.Size = btnSize; btn.Location = new Point(x * 30, y * 30); if (m.unitMap[x, y] is MeleeUnit) { MeleeUnit M = (MeleeUnit)m.unitMap[x, y]; btn.Text = M.Symbol; if (M.FactionType == Faction.Dire) { btn.BackColor = Color.Red; } else if (M.FactionType == Faction.Radient) { btn.BackColor = Color.Green; } btn.Name = m.unitMap[x, y].ToString(); btn.Click += MyButtonClick; gbMap.Controls.Add(btn); } } else if (m.map[x, y] == "W")//If the string map representation has an 'W' for a wizard unit { Button btn = new Button(); btn.Size = btnSize; btn.Location = new Point(x * 30, y * 30); if (m.unitMap[x, y] is WizardUnit) { WizardUnit W = (WizardUnit)m.unitMap[x, y]; btn.Text = W.Symbol; btn.BackColor = Color.Turquoise; btn.Name = m.unitMap[x, y].ToString(); btn.Click += MyButtonClick; gbMap.Controls.Add(btn); } } else if (m.map[x, y] == "FB")//If the string map representation has an 'FB' for a factory building { Button btn = new Button(); btn.Size = btnSize; btn.Location = new Point(x * 30, y * 30); if (m.buildingMap[x, y] is FactoryBuilding) { FactoryBuilding FB = (FactoryBuilding)m.buildingMap[x, y]; btn.Text = FB.Symbol; if (FB.FactionType == Faction.Dire) { btn.BackColor = Color.Red; } else if (FB.FactionType == Faction.Radient) { btn.BackColor = Color.Green; } btn.Name = m.buildingMap[x, y].ToString(); btn.Click += MyButtonClick; gbMap.Controls.Add(btn); } } else if (m.map[x, y] == "RB")//If the string map representation has an 'RB' for a resource building { Button btn = new Button(); btn.Size = btnSize; btn.Location = new Point(x * 30, y * 30); if (m.buildingMap[x, y] is ResourceBuilding) { ResourceBuilding RB = (ResourceBuilding)m.buildingMap[x, y]; btn.Text = RB.Symbol; if (RB.FactionType == Faction.Dire) { btn.BackColor = Color.Red; } else if (RB.FactionType == Faction.Radient) { btn.BackColor = Color.Green; } btn.Name = m.buildingMap[x, y].ToString(); btn.Click += MyButtonClick; gbMap.Controls.Add(btn); } } } } }
//Runs all the logic behind the game public void GameLogic() { //Working out if both teams are alive int dire = 0; int radiant = 0; foreach (ResourceBuilding RB in m.mines) { if (RB.FactionType == Faction.Dire) { dire++; } else { radiant++; } } foreach (FactoryBuilding FB in m.factories) { if (FB.FactionType == Faction.Dire) { dire++; } else { radiant++; } } foreach (MeleeUnit u in m.meleeUnits) { if (u.FactionType == Faction.Dire) { dire++; } else { radiant++; } } foreach (RangedUnit u in m.rangedUnits) { if (u.FactionType == Faction.Dire) { dire++; } else { radiant++; } } if (dire > 0 && radiant > 0)//Checks to see if both teams are still alive { foreach (ResourceBuilding RB in m.mines) { if (RB.FactionType == Faction.Dire) { direResources += RB.GenerateResource(); } else if (RB.FactionType == Faction.Radient) { radientResources += RB.GenerateResource(); } } foreach (FactoryBuilding FB in m.factories) { Unit u = FB.SpawnUnit(); if (FB.FactionType == Faction.Dire && direResources > FB.SpawnCost) { if (m.round % FB.SpawnSpeed == 0) { m.units.Add(u); if (u is MeleeUnit) { MeleeUnit M = (MeleeUnit)u; M.MapHeight = mapHeight; M.MapWidth = mapWidth; m.meleeUnits.Add(M); } else if (u is RangedUnit) { RangedUnit R = (RangedUnit)u; R.MapHeight = mapHeight; R.MapWidth = mapWidth; m.rangedUnits.Add(R); } direResources -= FB.SpawnCost; } } else if (FB.FactionType == Faction.Radient && radientResources > FB.SpawnCost) { if (m.round % FB.SpawnSpeed == 0) { m.units.Add(u); if (u is MeleeUnit) { MeleeUnit M = (MeleeUnit)u; m.meleeUnits.Add(M); } else if (u is RangedUnit) { RangedUnit R = (RangedUnit)u; m.rangedUnits.Add(R); } radientResources -= FB.SpawnCost; } } } foreach (Unit u in m.units) { u.CheckAttackRange(m.units, m.buildings); } m.round++; m.PlaceUnits(); m.PlaceBuildings(); Placebuttons(); } else { m.PlaceUnits(); m.PlaceBuildings(); Placebuttons(); GameTick.Enabled = false; if (dire > radiant) { MessageBox.Show("Dire Wins in " + m.round + " rounds"); } else { MessageBox.Show("Radiant Wins in " + m.round + " rounds"); } } //Checks to see who has died and needs to be deleted for (int i = 0; i < m.rangedUnits.Count; i++) { if (m.rangedUnits[i].Death()) { m.map[m.rangedUnits[i].PosX, m.rangedUnits[i].PosY] = ""; 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].PosY] = ""; m.meleeUnits.RemoveAt(i); } } for (int i = 0; i < m.wizardUnits.Count; i++) { if (m.wizardUnits[i].Death()) { m.map[m.wizardUnits[i].PosX, m.wizardUnits[i].PosY] = ""; m.wizardUnits.RemoveAt(i); } } for (int i = 0; i < m.units.Count; i++) { if (m.units[i].Death()) { if (m.units[i] is MeleeUnit) { MeleeUnit M = (MeleeUnit)m.units[i]; m.map[M.PosX, M.PosY] = ""; } else if (m.units[i] is RangedUnit) { RangedUnit R = (RangedUnit)m.units[i]; m.map[R.PosX, R.PosY] = ""; } else if (m.units[i] is WizardUnit) { WizardUnit W = (WizardUnit)m.units[i]; m.map[W.PosX, W.PosY] = ""; } m.units.RemoveAt(i); } } for (int i = 0; i < m.factories.Count; i++) { if (m.factories[i].Death()) { m.map[m.factories[i].PosX, m.factories[i].PosY] = ""; m.factories.RemoveAt(i); } } for (int i = 0; i < m.mines.Count; i++) { if (m.mines[i].Death()) { m.map[m.mines[i].PosX, m.mines[i].PosY] = ""; m.mines.RemoveAt(i); } } for (int i = 0; i < m.buildings.Count; i++) { if (m.buildings[i].Death()) { if (m.buildings[i] is FactoryBuilding) { FactoryBuilding FB = (FactoryBuilding)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); } } }