public Direction Directionto(Unit u) // this will detect in which direction to move towards { if (u.GetType() == typeof(WizardUnit)) { WizardUnit m = (WizardUnit)u; if (m.Xpos < Xpos) { return(Direction.North); } else if (m.Xpos > Xpos) { return(Direction.South); } else if (m.Ypos < Ypos) { return(Direction.West); } else { return(Direction.East); } } else { return(Direction.North); } }
public void UpdateDisplay() { DrawBlankMap(); foreach (GameObject unit in units) { if (unit.GetComponent <Unit>() != null) { Unit lUnit = unit.GetComponent <Unit>(); PlaceUnit(unit, lUnit.xPosition, lUnit.yPosition); } else if (unit.GetComponent <WizardUnit>() != null) { WizardUnit lUnit = unit.GetComponent <WizardUnit>(); PlaceUnit(unit, lUnit.xPosition, lUnit.yPosition); } } foreach (GameObject building in buildings) { if (building.GetComponent <FactoryBuilding>() != null) {//place Factories FactoryBuilding lBuilding = building.GetComponent <FactoryBuilding>(); PlaceUnit(building, lBuilding.xPosition, lBuilding.yPosition); } if (building.GetComponent <ResourceBuilding>() != null) {//Place Resource Buildings ResourceBuilding lBuilding = building.GetComponent <ResourceBuilding>(); PlaceUnit(building, lBuilding.xPosition, lBuilding.yPosition); } } }
public Map() { marrUnits = new MeleeUnit[10]; rarrUnits = new RangedUnit[10]; warrUnits = new WizardUnit[10]; for (int k = 0; k < 10; k++) { { marrUnits[k] = new MeleeUnit(r.Next(0, 20), r.Next(0, 20), "rogue", "m", "def"); rarrUnits[k] = new RangedUnit(r.Next(0, 20), r.Next(0, 20), "viking", "r", "def"); warrUnits[k] = new WizardUnit(r.Next(0, 20), r.Next(0, 20), "wizard", "w", "def"); } } res = new ResourceBuilding[5]; fact = new FactoryBuilding[5]; for (int j = 0; j < 5; j++) { { res[j] = new ResourceBuilding(r.Next(0, 20), r.Next(0, 20), "rogue", "r"); fact[j] = new FactoryBuilding(r.Next(0, 20), r.Next(0, 20), "viking", "f"); res[j] = new ResourceBuilding(r.Next(0, 20), r.Next(0, 20), "wizard", "r"); } } }
//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; } } }
public Map(int maxX, int maxY, int numUnits, int numBuildings) // these will create the archer and melee units and place them within the units array { int buildingX, buildingY; units = new Unit[numUnits]; buildings = new Building[numBuildings]; for (int i = 0; i < numUnits; i++) { if (i <= 10) { MeleeUnit M = new MeleeUnit(r.Next(0, maxX), r.Next(0, maxY), r.Next(10, 20) * 10, r.Next(10, 30), 1, 1, i % 2, "M", "Knight"); Units[i] = M; } if (i > 10 && i <= 20) { RangedUnit R = new RangedUnit(r.Next(0, maxX), r.Next(0, maxY), r.Next(10, 20) * 10, r.Next(10, 30), 1, 1, i % 2, "R", "Archer"); Units[i] = R; } if (i == 21) { WizardUnit W = new WizardUnit(r.Next(0, maxX), r.Next(0, maxY), r.Next(25, 35) * 10, r.Next(20, 40), 1, 1, 0, "W", "Wizard"); Units[i] = W; } if (i == 22) { WizardUnit W = new WizardUnit(r.Next(0, maxX), r.Next(0, maxY), r.Next(25, 35) * 10, r.Next(20, 40), 1, 1, 1, "W", "Wizard"); Units[i] = W; } if (i > 22 && i <= 27) { MeleeUnit MN = new MeleeUnit(r.Next(0, maxX), r.Next(0, maxY), r.Next(10, 20) * 10, r.Next(10, 30), 1, 1, 2, "M", "Knight"); Units[i] = MN; } if (i > 27) { RangedUnit RN = new RangedUnit(r.Next(0, maxX), r.Next(0, maxY), r.Next(10, 20) * 10, r.Next(10, 30), 1, 1, 2, "R", "Archer"); Units[i] = RN; } } for (int i = 0; i < numBuildings; i++) // these will create the buildings that will then be put into the buildings array { if (i <= 5) { buildingX = r.Next(0, maxX); buildingY = r.Next(0, maxX); FactoryBuilding fb = new FactoryBuilding(buildingX, r.Next(0, maxY), r.Next(5, 10) * 10, i % 2, "FB", r.Next(0, 1), r.Next(5, 10), buildingX + 1, buildingY + 1); Buildings[i] = fb; } if (i > 5 && i <= 10) { ResourceBuilding rb = new ResourceBuilding(r.Next(0, maxX), r.Next(0, maxY), r.Next(5, 10) * 10, i % 2, "RB", "Gold", r.Next(5, 15), r.Next(100, 400)); Buildings[i] = rb; } } }
//if stop colliding w wizards public void OnTriggerExit(Collider col) { if (col.gameObject.CompareTag("Wizards")) { //remove this gameobject from the inrange list to not take damage anymore WizardUnit wizard = col.gameObject.GetComponent <WizardUnit>(); wizard.inRange.Remove(gameObject); } }
//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 = Mathf.Abs((PosX - b.PosX) * (PosX - b.PosX)); yDis = Mathf.Abs((PosY - b.PosY) * (PosY - b.PosY)); distance = Mathf.Round(Mathf.Sqrt(xDis + yDis)); } } else if (u is MeleeUnit) { MeleeUnit b = (MeleeUnit)u; if (FactionType != b.FactionType) { xDis = Mathf.Abs((PosX - b.PosX) * (PosX - b.PosX)); yDis = Mathf.Abs((PosY - b.PosY) * (PosY - b.PosY)); distance = Mathf.Round(Mathf.Sqrt(xDis + yDis)); } } if (u is WizardUnit) { WizardUnit b = (WizardUnit)u; if (FactionType != b.FactionType) { xDis = Mathf.Abs((PosX - b.PosX) * (PosX - b.PosX)); yDis = Mathf.Abs((PosY - b.PosY) * (PosY - b.PosY)); distance = Mathf.Round(Mathf.Sqrt(xDis + yDis)); } } if (distance < temp) { temp = distance; target = u; } } return(target); }
//for the wizard aoe - when entering the trigger event that is the sphere collider public void OnTriggerEnter(Collider col) { //if collide w wizards if (col.gameObject.CompareTag("Wizards")) { //add this gameobject to the inrange list to take damage WizardUnit wizard = col.gameObject.GetComponent <WizardUnit>(); wizard.inRange.Add(gameObject); } }
private int DistanceTo(Unit u) // this uses the manhatten distance method to calculate distance { if (u.GetType() == typeof(WizardUnit)) { WizardUnit w = (WizardUnit)u; int d = Math.Abs(Xpos - w.Xpos) + Math.Abs(Ypos - w.Ypos); return(d); } else { return(0); } }
//Determines the distance between two units public int DistanceTo(Unit a, Unit b) { int distance = 0; if (a is MeleeUnit && b is MeleeUnit) { MeleeUnit start = (MeleeUnit)a; MeleeUnit end = (MeleeUnit)b; distance = Mathf.Abs(start.PosX - end.PosX) + Mathf.Abs(start.PosY - end.PosY); } else if (a is RangedUnit && b is MeleeUnit) { RangedUnit start = (RangedUnit)a; MeleeUnit end = (MeleeUnit)b; distance = Mathf.Abs(start.PosX - end.PosX) + Mathf.Abs(start.PosY - end.PosY); } else if (a is RangedUnit && b is RangedUnit) { RangedUnit start = (RangedUnit)a; RangedUnit end = (RangedUnit)b; distance = Mathf.Abs(start.PosX - end.PosX) + Mathf.Abs(start.PosY - end.PosY); } else if (a is MeleeUnit && b is RangedUnit) { MeleeUnit start = (MeleeUnit)a; RangedUnit end = (RangedUnit)b; distance = Mathf.Abs(start.PosX - end.PosX) + Mathf.Abs(start.PosY - end.PosY); } //Add wizards else if (a is WizardUnit && b is WizardUnit) { WizardUnit start = (WizardUnit)a; WizardUnit end = (WizardUnit)b; distance = Mathf.Abs(start.PosX - end.PosX) + Mathf.Abs(start.PosY - end.PosY); } else if (a is MeleeUnit && b is WizardUnit) { WizardUnit start = (WizardUnit)a; MeleeUnit end = (MeleeUnit)b; distance = Mathf.Abs(start.PosX - end.PosX) + Mathf.Abs(start.PosY - end.PosY); } else if (a is RangedUnit && b is WizardUnit) { WizardUnit start = (WizardUnit)a; RangedUnit end = (RangedUnit)b; distance = Mathf.Abs(start.PosX - end.PosX) + Mathf.Abs(start.PosY - end.PosY); } return(distance); }
public override bool withinAttackRange(Unit u) // this will see what enemies are in attack range { if (u.GetType() == typeof(WizardUnit)) { WizardUnit W = (WizardUnit)u; if (DistanceTo(u) <= attackRange) { return(true); } else { return(false); } } return(false); }
public GameObject GenerateWizard(int aUnitNumber, int aXPos, int aYPos) { int unitType = Random.Range(0, redUnits.Capacity); GameObject lUnit = Instantiate(grayUnits[unitType]); WizardUnit unit = lUnit.GetComponent <WizardUnit>(); unit.Name = $"{aUnitNumber}"; unit.attackRange = 1; unit.map = this; unit.xPosition = aXPos; unit.yPosition = aYPos; unit.health = 12; unit.speed = Random.Range(1, 4); unit.attack = Random.Range(1, 6); unit.faction = "Red"; unit.symbol = 'R'; return(lUnit); }
//Handles generation of the units public void Generate() { //PvP Teams for (int i = 0; i < numUnits; i++) { int holdMe; holdMe = Random.Range(0, 2); if (holdMe == 0) //Generate Melee Unit { MeleeUnit m = new MeleeUnit("Barbarian", Random.Range(0, width), Random.Range(0, height), (i % 2 == 0 ? 1 : 0), 30, 1, 5, 1, false ); units.Add(m); } else if (holdMe == 1) // Generate Ranged Unit { RangedUnit ru = new RangedUnit("Tank", Random.Range(0, width), Random.Range(0, height), (i % 2 == 0 ? 1 : 0), 20, 1, 3, 2, false ); units.Add(ru); } else { WizardUnit wu = new WizardUnit("Lich", Random.Range(0, width), Random.Range(0, height), 3, 6, 1, 5, 1, false ); units.Add(wu); } } for (int l = 0; l < 6; l++) { WizardUnit wu = new WizardUnit("Lich", Random.Range(0, width), Random.Range(0, height), 3, 15, 1, 5, 1, false ); units.Add(wu); } for (int k = 0; k < numBuildings; k++) { if (Random.Range(0, 2) == 0) //Generate Resource Building { ResourceBuilding rb = new ResourceBuilding(Random.Range(0, width), Random.Range(0, height), 20, (k % 2 == 0 ? 1 : 0), 10); buildings.Add(rb); } else //Generate Unit Building { FactoryBuilding fb = new FactoryBuilding(Random.Range(0, width), Random.Range(0, height), 20, (k % 2 == 0 ? 1 : 0), 3, (Random.Range(0, 2) == 1 ? "Melee" : "Ranged"), 35); buildings.Add(fb); } } }
//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 void UpdateUnitPosition(GameObject aUnitToMove, int aXNewPos, int aYNewPos) { int newX = aXNewPos; int newY = aYNewPos; if (!CheckPositionAgainstGridSize(newY, newY)) { if (newX >= gridSizeX) { newX = gridSizeX - 1; } if (newY >= gridSizeY) { newY = gridSizeY - 1; } if (newX < 0) { newX = 0; } if (newY < 0) { newY = 0; } } if (aUnitToMove.GetComponent <Unit>() != null) { if (Battlefield[newX, newY] == blankTile) { Unit lUnitToMove = aUnitToMove.GetComponent <Unit>(); CheckPositionAgainstGridSize(lUnitToMove.xPosition, lUnitToMove.yPosition); int currentPosX = lUnitToMove.xPosition; int currentPosY = lUnitToMove.yPosition; GameObject unit = Battlefield[currentPosX, currentPosY]; Battlefield[currentPosX, currentPosY] = Battlefield[newX, newY]; Battlefield[newX, newY] = unit; Battlefield[currentPosX, currentPosY].transform.position = GetDisplayCoord(currentPosX, currentPosY); Battlefield[newX, newY].transform.position = GetDisplayCoord(newX, newY); } } else if (aUnitToMove.GetComponent <WizardUnit>() != null) { WizardUnit lUnitToMove = aUnitToMove.GetComponent <WizardUnit>(); CheckPositionAgainstGridSize(lUnitToMove.xPosition, lUnitToMove.yPosition); int currentPosX = lUnitToMove.xPosition; int currentPosY = lUnitToMove.yPosition; GameObject unit = Battlefield[currentPosX, currentPosY]; Battlefield[currentPosX, currentPosY] = Battlefield[aXNewPos, aYNewPos]; Battlefield[aXNewPos, aYNewPos] = unit; Battlefield[currentPosX, currentPosY].transform.position = GetDisplayCoord(currentPosX, currentPosY); Battlefield[aXNewPos, aYNewPos].transform.position = GetDisplayCoord(aXNewPos, aYNewPos); } }
//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 = 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 RangedUnit) { RangedUnit R = (RangedUnit)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 WizardUnit) { WizardUnit W = (WizardUnit)closestUnit; xDis = Mathf.Abs((PosX - W.PosX) * (PosX - W.PosX)); yDis = Mathf.Abs((PosY - W.PosY) * (PosY - W.PosY)); uDistance = (int)Mathf.Round(Mathf.Sqrt(xDis + yDis)); } if (closestBuilding is FactoryBuilding) { FactoryBuilding FB = (FactoryBuilding)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 ResourceBuilding) { ResourceBuilding RB = (ResourceBuilding)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 castRay() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity); if (hit) { float x = hit.collider.transform.position.x; float y = hit.collider.transform.position.y; Debug.Log("" + x + " " + y); foreach (Unit u in map.Units) { if (u.GetType() == typeof(RangedUnit)) { RangedUnit r = (RangedUnit)u; if (r.Xpos == x && r.Ypos == y) { info.text = "" + r.toString(); } } else if (u.GetType() == typeof(MeleeUnit)) { MeleeUnit m = (MeleeUnit)u; if (m.Xpos == x && m.Ypos == y) { info.text = "" + m.toString(); } } else if (u.GetType() == typeof(WizardUnit)) { WizardUnit m = (WizardUnit)u; if (m.Xpos == x && m.Ypos == y) { info.text = "" + m.toString(); } } } foreach (Building b in map.Buildings) { if (b.GetType() == typeof(FactoryBuilding)) { FactoryBuilding fb = (FactoryBuilding)b; if (fb.Xpos == x && fb.Ypos == y) { info.text = "" + fb.toString(); } } else if (b.GetType() == typeof(ResourceBuilding)) { ResourceBuilding rb = (ResourceBuilding)b; if (rb.Xpos == x && rb.Ypos == y) { info.text = "" + rb.toString(); } } } } }
public void DisplayMap() { foreach (Unit u in map.Units) { if (u.GetType() == typeof(MeleeUnit)) { int start_x = 20; int start_Y = 20; MeleeUnit m = (MeleeUnit)u; if (m.health > 0) { if (m.faction == 1) { GameObject dup = Instantiate(RedMelee, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueMelee, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else { GameObject dup = Instantiate(GreyMelee, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } if (m.isDead()) { m.symbol = "X"; if (m.faction == 1) { GameObject dup = Instantiate(RedDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else { GameObject dup = Instantiate(NeutralDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } } } foreach (Unit u in map.Units) { if (u.GetType() == typeof(RangedUnit)) { int start_x = 20; int start_Y = 20; RangedUnit m = (RangedUnit)u; if (m.health > 0) { if (m.faction == 1) { GameObject dup = Instantiate(RedRanged, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueRanged, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else { GameObject dup = Instantiate(GreyRanged, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } if (m.isDead()) { m.symbol = "X"; if (m.faction == 1) { GameObject dup = Instantiate(RedDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else { GameObject dup = Instantiate(NeutralDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } } } foreach (Unit u in map.Units) { if (u.GetType() == typeof(WizardUnit)) { int start_x = 20; int start_Y = 20; WizardUnit m = (WizardUnit)u; if (m.health > 0) { if (m.faction == 1) { GameObject dup = Instantiate(RedWizard, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueWizard, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } if (m.isDead()) { m.symbol = "X"; if (m.faction == 1) { GameObject dup = Instantiate(RedDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } } } foreach (Building u in map.Buildings) { if (u.GetType() == typeof(FactoryBuilding)) { int start_x = 20; int start_Y = 20; FactoryBuilding m = (FactoryBuilding)u; if (m.health > 0) { if (m.faction == 1) { GameObject dup = Instantiate(RedFactory, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueFactory, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } if (m.isDead()) { m.symbol = "X"; if (m.faction == 1) { GameObject dup = Instantiate(RedDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } } } foreach (Building u in map.Buildings) { if (u.GetType() == typeof(ResourceBuilding)) { int start_x = 20; int start_Y = 20; ResourceBuilding m = (ResourceBuilding)u; if (m.health > 0) { if (m.faction == 1) { GameObject dup = Instantiate(RedMine, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueMine, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } if (m.isDead()) { m.symbol = "X"; if (m.faction == 1) { GameObject dup = Instantiate(RedDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } } } }
private void UpdateMap() // this will cycle through all the units and buildisngs to see if they need to move and whether or not they have been killed in combat { foreach (Unit u in map.Units) { if (u.GetType() == typeof(MeleeUnit)) { MeleeUnit m = (MeleeUnit)u; if (m.health > 1) { if (m.health < 25) { switch (r.Next(0, 4)) { case 0: ((MeleeUnit)u).NewPos(Direction.North); break; case 1: ((MeleeUnit)u).NewPos(Direction.East); break; case 2: ((MeleeUnit)u).NewPos(Direction.South); break; case 3: ((MeleeUnit)u).NewPos(Direction.West); break; } } else if (m.health < 10) // this method will check a units health to see if it below 10 and if it is there will be a 50% chance of that unit changing alleigance to the other team and the unit will gain a small amount of health as compensation for changing team { int teamChange = r.Next(0, 2); if (teamChange == 0) { if (m.faction == 1) { m.faction = 0; m.health = m.health + 5; } else { m.faction = 1; m.health = m.health + 5; } } } else { bool inCombat = false; foreach (Unit e in map.Units) { if (u.withinAttackRange(e)) { u.combatWithUnit(e); inCombat = true; } } if (!inCombat) { Unit c = u.UnitDistance(map.Units); m.NewPos(m.Directionto(c)); } } } else { m.symbol = "X"; if (m.faction == 1) { GameObject dup = Instantiate(RedDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else { GameObject dup = Instantiate(NeutralDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } } } foreach (Unit u in map.Units) { if (u.GetType() == typeof(RangedUnit)) { RangedUnit m = (RangedUnit)u; if (m.health > 1) { if (m.health < 25) { switch (r.Next(0, 4)) { case 0: ((RangedUnit)u).NewPos(Direction.North); break; case 1: ((RangedUnit)u).NewPos(Direction.East); break; case 2: ((RangedUnit)u).NewPos(Direction.South); break; case 3: ((RangedUnit)u).NewPos(Direction.West); break; } } else if (m.health < 10) // this method will check a units health to see if it below 10 and if it is there will be a 50% chance of that unit changing alleigance to the other team and the unit will gain a small amount of health as compensation for changing team { int teamChange = r.Next(0, 2); if (teamChange == 0) { if (m.faction == 1) { m.faction = 0; m.health = m.health + 5; } else { m.faction = 1; m.health = m.health + 5; } } } else { bool inCombat = false; foreach (Unit e in map.Units) { if (u.withinAttackRange(e)) { u.combatWithUnit(e); inCombat = true; } } if (!inCombat) { Unit c = u.UnitDistance(map.Units); m.NewPos(m.Directionto(c)); } } } else { m.symbol = "X"; if (m.faction == 1) { GameObject dup = Instantiate(RedDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (m.faction == 0) { GameObject dup = Instantiate(BlueDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } else { GameObject dup = Instantiate(NeutralDeath, new Vector2(m.Xpos, m.Ypos), Quaternion.identity); dup.transform.parent = transform; } } } } foreach (Unit u in map.Units) { if (u.GetType() == typeof(WizardUnit)) { WizardUnit w = (WizardUnit)u; if (w.health > 1) { if (w.health < 25) { switch (r.Next(0, 4)) { case 0: ((WizardUnit)u).NewPos(Direction.North); break; case 1: ((WizardUnit)u).NewPos(Direction.East); break; case 2: ((WizardUnit)u).NewPos(Direction.South); break; case 3: ((WizardUnit)u).NewPos(Direction.West); break; } } else { bool inCombat = false; foreach (Unit e in map.Units) { if (u.withinAttackRange(e)) { u.combatWithUnit(e); inCombat = true; } } if (!inCombat) { Unit c = u.UnitDistance(map.Units); w.NewPos(w.Directionto(c)); } } } else { w.symbol = "X"; if (w.faction == 1) { GameObject dup = Instantiate(RedDeath, new Vector2(w.Xpos, w.Ypos), Quaternion.identity); dup.transform.parent = transform; } else if (w.faction == 0) { GameObject dup = Instantiate(BlueDeath, new Vector2(w.Xpos, w.Ypos), Quaternion.identity); dup.transform.parent = transform; } else { GameObject dup = Instantiate(NeutralDeath, new Vector2(w.Xpos, w.Ypos), Quaternion.identity); dup.transform.parent = transform; } } } } foreach (Building b in map.Buildings) { if (b.GetType() == typeof(ResourceBuilding)) { ResourceBuilding rb = (ResourceBuilding)b; rb.ResourceGenerate(); } } foreach (Building b in map.Buildings) { if (b.GetType() == typeof(FactoryBuilding)) { FactoryBuilding fb = (FactoryBuilding)b; } } #region SpawningOfUnits foreach (Unit u in map.Units) { if (u.GetType() == typeof(MeleeUnit)) { MeleeUnit mu = (MeleeUnit)u; if (mu.symbol == "X") { int faction = mu.faction; int rand = r.Next(0, 5); FactoryBuilding fb = (FactoryBuilding)map.Buildings[rand]; fb.Spawner(20, 20, faction); } } } foreach (Unit u in map.Units) { if (u.GetType() == typeof(RangedUnit)) { RangedUnit mu = (RangedUnit)u; if (mu.symbol == "X") { int faction = mu.faction; int rand = r.Next(0, 5); FactoryBuilding fb = (FactoryBuilding)map.Buildings[rand]; fb.Spawner(20, 20, faction); } } } #endregion }
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); } } }
//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); } } } }
private void PerformAction(GameObject aUnit) { if (aUnit.GetComponent <Unit>() != null) { Unit lUnit = aUnit.GetComponent <Unit>(); GameObject tClosestEnemy = lUnit.FindClosestEnemy(map.Battlefield); if (tClosestEnemy != null && lUnit.health >= 0.25 * lUnit.maxHealth) { Unit closestEnemy = tClosestEnemy.GetComponent <Unit>(); if (lUnit.RangeCheck(closestEnemy) && closestEnemy != null) { lUnit.EngageUnit(closestEnemy); lUnit.isAttacking = true; } else if (roundsCompleted % lUnit.speed == 0) { //Move toward the enemy int differenceInXPosition = Math.Abs(lUnit.xPosition - closestEnemy.xPosition); int differenceInYPosition = Math.Abs(lUnit.yPosition - closestEnemy.yPosition); if (differenceInXPosition > differenceInYPosition) { //Move vertical if (lUnit.yPosition <= closestEnemy.yPosition) { lUnit.Move(Map.Direction.Up); } else if (lUnit.yPosition > closestEnemy.yPosition) { lUnit.Move(Map.Direction.Down); } } else if (differenceInXPosition > differenceInYPosition) { //Move horizontal if (lUnit.xPosition <= closestEnemy.xPosition) { lUnit.Move(Map.Direction.Right); } else if (lUnit.xPosition > closestEnemy.xPosition) { lUnit.Move(Map.Direction.Left); } } else { lUnit.Move(Map.Direction.Up); } } else if (lUnit.health < 0.25 * lUnit.maxHealth) { lUnit.Move(RandomDirection()); } } } if (aUnit.GetComponent <WizardUnit>() != null) { WizardUnit lUnit = aUnit.GetComponent <WizardUnit>(); GameObject tClosestEnemy = lUnit.FindClosestEnemy(map.Battlefield); if (tClosestEnemy != null && lUnit.health >= 0.25 * lUnit.maxHealth) { Unit closestEnemy = tClosestEnemy.GetComponent <Unit>(); if (lUnit.RangeCheck(closestEnemy)) { lUnit.EngageUnit(closestEnemy); lUnit.isAttacking = true; } else if (roundsCompleted % lUnit.speed == 0) { //Move toward the enemy int differenceInXPosition = Math.Abs(lUnit.xPosition - closestEnemy.xPosition); int differenceInYPosition = Math.Abs(lUnit.yPosition - closestEnemy.yPosition); if (differenceInXPosition > differenceInYPosition) { //Move vertical if (lUnit.yPosition <= closestEnemy.yPosition) { lUnit.Move(Map.Direction.Up); } else if (lUnit.yPosition > closestEnemy.yPosition) { lUnit.Move(Map.Direction.Down); } } else if (differenceInXPosition > differenceInYPosition) { //Move horizontal if (lUnit.xPosition <= closestEnemy.xPosition) { lUnit.Move(Map.Direction.Right); } else if (lUnit.xPosition > closestEnemy.xPosition) { lUnit.Move(Map.Direction.Left); } } else { lUnit.Move(Map.Direction.Up); } } else if (lUnit.health < 0.25 * lUnit.maxHealth) { lUnit.Move(RandomDirection()); } } } }
public void CheckDeath() { //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.rangedUnits.RemoveAt(i); } } for (int i = 0; i < m.meleeUnits.Count; i++) { if (m.meleeUnits[i].Death()) { m.meleeUnits.RemoveAt(i); } } for (int i = 0; i < m.wizardUnits.Count; i++) { if (m.wizardUnits[i].Death()) { 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]; } else if (m.units[i] is RangedUnit) { RangedUnit R = (RangedUnit)m.units[i]; } else if (m.units[i] is WizardUnit) { WizardUnit W = (WizardUnit)m.units[i]; } m.units.RemoveAt(i); } } for (int i = 0; i < m.factories.Count; i++) { if (m.factories[i].Death()) { m.factories.RemoveAt(i); } } for (int i = 0; i < m.mines.Count; i++) { if (m.mines[i].Death()) { 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]; } else if (m.buildings[i] is ResourceBuilding) { ResourceBuilding RB = (ResourceBuilding)m.buildings[i]; } m.buildings.RemoveAt(i); } } }
//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 = Random.Range(0, 2); string unitName; if (unitTypeN == 0) { unitName = "Ranged"; } else { unitName = "Melee"; } FactoryBuilding factory = new FactoryBuilding(0, 0, 100, Faction.Dire, Random.Range(3, 10), unitName, Random.Range(50, 71)); factories.Add(factory); ResourceBuilding mine = new ResourceBuilding(0, 0, 100, Faction.Dire, Random.Range(3, 10), ResourceType.Gold); mines.Add(mine); } for (int i = 0; i < buildingNum; i++) { int unitTypeN = Random.Range(0, 2); string unitName; if (unitTypeN == 0) { unitName = "Ranged"; } else { unitName = "Melee"; } FactoryBuilding factory = new FactoryBuilding(0, 0, 100, Faction.Radient, Random.Range(3, 10), unitName, Random.Range(50, 71)); factories.Add(factory); ResourceBuilding mine = new ResourceBuilding(0, 0, 100, Faction.Radient, Random.Range(3, 10), ResourceType.Gold); mines.Add(mine); } for (int i = 0; i < (buildingNum * 2); i++) { WizardUnit wizard = new WizardUnit(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 = Random.Range(0, mapHeight); int yPos = Random.Range(0, mapWidth); while ((xPos == factories[i].PosX && yPos == factories[i].PosY) || (xPos == 0 && yPos == 0)) { xPos = Random.Range(0, mapHeight); yPos = Random.Range(0, mapWidth); } u.PosX = xPos; u.PosY = yPos; } 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 = Random.Range(0, mapHeight); int yPos = Random.Range(0, mapWidth); while ((xPos == mines[i].PosX && yPos == mines[i].PosY) || (xPos == factories[i].PosX && yPos == factories[i].PosY)) { xPos = Random.Range(0, mapHeight); yPos = Random.Range(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 = Random.Range(0, mapHeight); int yPos = Random.Range(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 = Random.Range(0, mapHeight); yPos = Random.Range(0, mapWidth); } u.PosX = xPos; u.PosY = yPos; } //unitMap[u.PosY, u.PosX] = (Unit)u; units.Add(u); } }