public override void spawnTeam() { controllerID = 1; List <unitScript> team = new List <unitScript>(); EnemyType[] types = map.getEnemyTypes(); Dictionary <Vector2, int> spawnData = map.getSpawnData(); foreach (Vector2 position in spawnData.Keys) { if (spawnData[position] >= 2) // If greater than 2 then spawn enemy { EnemyType type = types[spawnData[position]]; GameObject newUnit = (GameObject)Instantiate(Resources.Load(type.source), transform, true); unitScript unit = newUnit.GetComponent <unitScript>(); unit.setData(type.unitName, type.unitClass, type.health, type.speed, type.skill, type.strength, type.smarts, "", new List <string>(type.weapons)); unit.gameObject.layer = 8; unit.setPosition((int)position.x, (int)position.y); unit.setOwner(1); AIUnitController ai = unit.gameObject.AddComponent <AIUnitController>(); ai.healthThreshold = type.healthThreshold; team.Add(unit); unitsInGame.Add(unit); } } playerUnitList = team.ToArray(); }
public override void spawnTeam() { Vector2[] spawns = map.getSpawnPoints(); XmlDocument xml = new XmlDocument(); xml.LoadXml(playerData.text); XmlNode data = xml.SelectSingleNode("data"); playerUnitList = new unitScript[data.ChildNodes.Count]; for (int i = 0; i < data.ChildNodes.Count; i++) { if (LevelData.party[i] != 1) { playerUnitList[i] = null; continue; } XmlNode current = data.ChildNodes[i]; string unitName = current.SelectSingleNode("name").InnerText; string unitClass = current.SelectSingleNode("class").InnerText; int health = int.Parse(current.SelectSingleNode("health").InnerText); int speed = int.Parse(current.SelectSingleNode("speed").InnerText); int skill = int.Parse(current.SelectSingleNode("skill").InnerText); int strength = int.Parse(current.SelectSingleNode("strength").InnerText); int smarts = int.Parse(current.SelectSingleNode("smarts").InnerText); string portraitString = current.SelectSingleNode("portait").InnerText; string source = current.SelectSingleNode("source").InnerText; GameObject newUnit = (GameObject)Instantiate(Resources.Load(source), transform, true); unitScript unit = newUnit.GetComponent <unitScript>(); XmlNode weaponNode = current.SelectSingleNode("weapons"); List <string> unitWeapons = new List <string>(); foreach (XmlNode weapon in weaponNode.ChildNodes) { unitWeapons.Add(weapon.InnerText); } unit.setData(unitName, unitClass, health, speed, skill, strength, smarts, portraitString, unitWeapons); unit.gameObject.layer = 8; unit.setPosition(Mathf.FloorToInt(spawns[i].x), Mathf.FloorToInt(spawns[i].y)); playerUnitList[i] = unit; unitsInGame.Add(unit); } gui.GetComponent <GUIController>().setPlayerTeam(playerUnitList); }
public void addUnit(int typeIndex, int x, int y) { EnemyType[] types = map.getEnemyTypes(); EnemyType type = types[typeIndex]; GameObject newUnit = (GameObject)Instantiate(Resources.Load(type.source), transform, true); unitScript unit = newUnit.GetComponent <unitScript>(); unit.setData(type.unitName, type.unitClass, type.health, type.speed, type.skill, type.strength, type.smarts, "", new List <string>(type.weapons)); unit.gameObject.layer = 8; unit.setPosition(x, y); unit.setOwner(1); AIUnitController ai = unit.gameObject.AddComponent <AIUnitController>(); ai.healthThreshold = type.healthThreshold; List <unitScript> newList = new List <unitScript>(playerUnitList); newList.Add(unit); this.playerUnitList = newList.ToArray(); unitsInGame.Add(unit); }
void hunt(unitScript unit) { pathFinder.getMovementPaths(unit.GetComponent <GridItem>().getPos(), 100, false); //var locations = pathFinder.getReachableLocations(); if (unit.getCurrentWeapon().type == WeaponType.melee) { if (!unit.GetComponent <AIUnitController>().moved) { //get position of all enemies unitScript[] units = getAllUnits(); List <unitScript> targetList = new List <unitScript>(units); //Pick target unitScript target = targetPrioritisation(unit, targetList); if (target == null) { unit.GetComponent <AIUnitController>().moved = true; unit.GetComponent <AIUnitController>().attacked = true; return; } unit.GetComponent <AIUnitController>().moved = true; //Get the positions one short of target var oneShort = target.GetComponent <GridItem>().getPos() - unit.GetComponent <GridItem>().getPos(); var targetPosXclose = target.GetComponent <GridItem>().getPos() - new Vector2(Mathf.Sign(oneShort.x), 0); var targetPosYclose = target.GetComponent <GridItem>().getPos() - new Vector2(0, Mathf.Sign(oneShort.y)); var targetPosXfar = target.GetComponent <GridItem>().getPos() + new Vector2(Mathf.Sign(oneShort.x), 0); var targetPosYfar = target.GetComponent <GridItem>().getPos() + new Vector2(0, Mathf.Sign(oneShort.y)); List <Vector2[]> paths = new List <Vector2[]>(); paths.Add(pathFinder.drawPath(targetPosXclose, false, true)); paths.Add(pathFinder.drawPath(targetPosYclose, false, true)); paths.Add(pathFinder.drawPath(targetPosXfar, false, true)); paths.Add(pathFinder.drawPath(targetPosYfar, false, true)); //For each potential places try to move there foreach (var path in paths) { if (path != null) { //if the path is not null attack Vector2[] newPath = new Vector2[Mathf.Min(path.Length, unit.getMovementDistance())]; for (int i = 0; i < newPath.Length; i++) { newPath[i] = path[i]; } unit.setPath(newPath); break; } } unit.GetComponent <AIUnitController>().moved = true; } else if (unit.canEndTurn()) { //Attack target MeleeAttack(unit, unit.GetComponent <AIUnitController>().target); unit.GetComponent <AIUnitController>().attacked = true; } } else if (unit.getCurrentWeapon().type == WeaponType.ranged) { if (!unit.GetComponent <AIUnitController>().moved) { Vector2 initialPosition = unit.GetComponent <GridItem>().getPos(); //Initialise heursticmap var locations = pathFinder.getReachableLocations(); Dictionary <Vector2, float> heuristicMap = new Dictionary <Vector2, float>(); RangedWeapon currentGun = (RangedWeapon)unit.getCurrentWeapon(); var enemyUnits = getEnemyUnits(); foreach (Vector2 pos in locations.Where(p => Mathf.Abs(p.x - unit.GetComponent <GridItem>().getPos().x) + Mathf.Abs(p.y - unit.GetComponent <GridItem>().getPos().y) < unit.getMovementDistance())) { unit.setPosition((int)pos.x, (int)pos.y); float h = 0.0f; //Get all cover tiles around the current var neighboringTiles = this.map.getAllNeighbours(pos).Where(p => this.map.getTileData(p).coverValue != 0); //How much will this tile hurt? h += enemyUnits.Where(e => getRangePenalty(e, Vector3.Distance(e.GetComponent <GridItem>().getVectorPostion(), unit.GetComponent <GridItem>().getVectorPostion())) == 0) .Aggregate(0, (subHeuristic, enemy) => subHeuristic - 1); //Add back all values from cover foreach (Vector2 cover in neighboringTiles) { //Direction of cover Vector2 vectorToCover = cover - pos; //If the cover is between me and an enemy then increase heurstic h += enemyUnits .Where( e => // The enemy is not melee or shield e.getCurrentWeapon().type == WeaponType.ranged && //I am within enemy range getRangePenalty(e, Vector3.Distance(e.GetComponent <GridItem>().getVectorPostion(), unit.GetComponent <GridItem>().getVectorPostion())) == 0 && //And cover is between the enemy and myself (Vector2.Angle(e.GetComponent <GridItem>().getPos() - pos, vectorToCover) < 45) ).Aggregate(0, (subHeurstic, e) => subHeurstic + (this.map.getTileData(cover).coverValue + 2)); } // How much damage can I do from this position h += enemyUnits.Select(e => getRangePenalty(unit, Vector3.Distance(e.GetComponent <GridItem>().getVectorPostion(), unit.GetComponent <GridItem>().getVectorPostion())) == 0) .Aggregate(0, (subHeurisitic, e) => subHeurisitic + (e?1:-1));//unit.getCurrentWeapon().damage); heuristicMap[pos] = h; this.map.displayDebugData((int)pos.x, (int)pos.y, h.ToString()); unit.setPosition((int)initialPosition.x, (int)initialPosition.y); } unit.setPosition((int)initialPosition.x, (int)initialPosition.y); //Pick location with highest score Vector2 moveLocation = heuristicMap.Aggregate((l, r) => l.Value >= r.Value ? l : r).Key; print("KEY/Value " + moveLocation + " " + heuristicMap[moveLocation]); //Move there var path = pathFinder.drawPath(moveLocation, false, true); //if the path is not null attack Vector2[] newPath = new Vector2[Mathf.Min(path.Length, unit.getMovementDistance())]; for (int i = 0; i < newPath.Length; i++) { newPath[i] = path[i]; } unit.setPath(newPath); unit.GetComponent <AIUnitController>().moved = true; //If an attack can be made make it } if (unit.canEndTurn()) { //Find best target unitScript target = targetPrioritisation(unit, getEnemyUnits()); if (target == null) { unit.GetComponent <AIUnitController>().attacked = true; return; } //Attack target RangedAttack(unit, unit.GetComponent <AIUnitController>().target); unit.GetComponent <AIUnitController>().attacked = true; } } else if (unit.getCurrentWeapon().type == WeaponType.shield) { if (swapTo(unit, WeaponType.melee)) { print("ready Sword"); } else if (swapTo(unit, WeaponType.ranged)) { print("ready Gun"); } } }