public Map(int mapsizeX, int mapsizeY) //constructor /* * Generates random ranged/melee units * random pos between 20X and 20Y */ { for (int i = 0; i < 10; i++) { int newX = ran.Next(0, mapsizeX); //random X pos int newY = ran.Next(0, mapsizeY); //random Y pos int team = 1 % 2; //random team int tempAttack = 0; //temporary attack switch (ran.Next(0, 4)) //set temp attac to random between 4 values { case 0: tempAttack = 5; break; case 1: tempAttack = 10; break; case 2: tempAttack = 15; break; case 3: tempAttack = 20; break; } switch (ran.Next(0, 2)) { case 0: units[i] = new MeleeUnit(newX, newY, 100, 1, tempAttack, 1, team, i.ToString(), "UNIT NAME"); break; //generate new instance of melee/ranged unit case 1: units[i] = new RangedUnit(newX, newY, 100, 1, tempAttack, 1, team, i.ToString(), "UNIT NAME"); break; } } }
//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); }
//Checks to see if they are below 25% health so they move rather than attacking public override void CheckAttackRange(List <Unit> uni, List <Building> build) { units = uni; closestUnit = ClosestEnemy(); int enemyType = 0; int xDis = 0, yDis = 0; int uDistance = 10000; int distance = 1000; 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); } if (uDistance < distance) { distance = uDistance; enemyType = 0; } //Checks to see if they are below 25% health so they move rather than attacking if (Health > MaxHealth * 0.5) { if (distance <= AttackRange) { IsAttacking = true; Combat(enemyType); } else { IsAttacking = false; Move(enemyType); } } else { Move(enemyType); } }
//Returns a unit to be spawned public Unit SpawnUnit() { if (unitType == "Melee") { MeleeUnit knight = new MeleeUnit("Knight", SpawnPointX, SpawnPointY, factionType, 40, 1, 5, 1, "/", false); return(knight); } else { RangedUnit archer = new RangedUnit("Archer", SpawnPointX, SpawnPointY, FactionType, 30, 1, 3, 3, "{|", false); return(archer); } }
public void SpawnUnit(string unitType, int x, int y, Faction fac) { if (unitType == "Melee") { MeleeUnit knight = new MeleeUnit("Knight", x, y, fac, 40, 1, 5, 1, "/", false); meleeUnits.Add(knight); units.Add(knight); } else if (unitType == "Ranged") { RangedUnit archer = new RangedUnit("Archer", x, y, fac, 30, 1, 3, 3, "{|", false); rangedUnits.Add(archer); units.Add(archer); } }
//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"; } }
//Places the units on a string representation of the 20x20 map public void PlaceUnits() { for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { map[i, j] = " "; } } for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { unitMap[i, j] = null; } } 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; } } foreach (RangedUnit u in rangedUnits) { map[u.PosY, u.PosX] = "R"; } foreach (MeleeUnit u in meleeUnits) { map[u.PosY, u.PosX] = "M"; } }
//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 < 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 (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 //Moves in random direction to run away { 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--; } } }
//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); } 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); } }
//Deals damage to closest unit if they are in attack range public override void Combat(int type) { 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 RangedUnit) { RangedUnit R = (RangedUnit)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; } } } }
//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.5) { 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 //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); } } }
//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 < 20; x++) { for (int y = 0; y < 20; y++) { Button btn = new Button(); btn.Size = btnSize; btn.Location = new Point(x * 30, y * 30); if (m.map[x, y] == "R") { 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 { 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 (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 { btn.BackColor = Color.Green; } btn.Name = m.unitMap[x, y].ToString(); btn.Click += MyButtonClick; gbMap.Controls.Add(btn); } } else if (m.map[x, y] == "FB") { 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 { 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 (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 { btn.BackColor = Color.Green; } btn.Name = m.buildingMap[x, y].ToString(); btn.Click += MyButtonClick; gbMap.Controls.Add(btn); } } } } }