public override bool CreateUnitController(Vector3 position) { if (_gridController.GetGridTile(position).IsLocked) { return(false); } UnitBehaviour behaviourInstance = UnityEngine.Object.Instantiate(_unitBehaviourPrefab, position + GlobalData.GlobalVectors.POSITION_OFFSET, Quaternion.identity); IdleState idleState = new IdleState() { CurrentTile = _gridController.GetGridTile(position) }; AIUnitController unit = new AIUnitController(null, behaviourInstance, idleState); idleState.SetParent(unit); idleState.OnStateEnter(); //idleState.CurrentTile.LockTile(); unit.UnitIndex = _units.Count; _units.Add(unit); return(true); }
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(); }
void SpawnActors() { aucList = new List <AIUnitController>(); AIUnitController newAuc = Instantiate(auc, new Vector3(0, 0, 0), Quaternion.identity).GetComponent <AIUnitController>(); aucList.Add(newAuc); Unit newUnit = Instantiate(unit, new Vector3(0, 0, 0), Quaternion.identity).GetComponent <Unit>(); newUnit.SetTeam("none"); newAuc.SetUnit(newUnit); newAuc.Move(2, 2); }
public void ActivateUnit(int index) { AIUnitController controller = _units[index]; int count = controller.GetLocationCount(); if (count == 0) { } else if (count > 0) { MoveUnit(controller, controller.GetCurrentLocation()); } }
public void OnUnitPathFinished(AIUnitController unit) { Vector3 location = unit.GetCurrentLocation(); unit.SetNextLocation(); if (location != unit.GetCurrentLocation()) { MoveUnit(unit, unit.GetCurrentLocation()); } else { IdleState state = new IdleState(unit); unit.ChangeState(state); } }
void Update() { if (UnitManager.gameUnits[UnitManager.currUnitTurn].playerControlled & UnitManager.actionPoints > 0) { if (Input.GetMouseButtonDown(0)) { mousePos = Input.mousePosition; tilePos = tileMap.WorldToCell(Camera.main.ScreenToWorldPoint(mousePos)); //Thorws a raycast in the direction of the target RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(mousePos), Vector2.zero, Mathf.Infinity, mask); if (hit.collider != null) { /* * Calculate pathfinding and apply * * Insert Code Here * */ Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3Int clickPos = tileMap.WorldToCell(mouseWorldPos); // Temporary movment code // jumpt to mouse click StartCoroutine(moveOverSeconds(UnitManager.gameUnits[UnitManager.currUnitTurn].gameObject, tilePos)); // get cselected tile ATile aTile = (ATile)tileMap.GetTile(tilePos); //apply tile movement cost to available action points UnitManager.actionPoints -= aTile.movementSpeed; } } } // if is AI controlled unit's turn and still has action points else if (UnitManager.actionPoints > 0)//AI's turn { AIUnitController.CalculateAITurn(); } // if is AI controlled unit's turn and has no more action points else if (!UnitManager.gameUnits[UnitManager.currUnitTurn].playerControlled & UnitManager.actionPoints <= 0) { //Initiate next unit turn } // else wait for player to click end turn button }
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); }
//Assisting functions unitScript targetPrioritisation(unitScript unit, List <unitScript> enemyList) { AIUnitController unitControl = unit.GetComponent <AIUnitController>(); List <float> priorites = new List <float>(); foreach (unitScript otherUnit in enemyList) { //Add factors float priority = 0; if (otherUnit == null || otherUnit.getOwner() == controllerID || otherUnit.getHealth() <= 0) { priorites.Add(-1000000); break; } //distance - wrecklessness (negative if over wrecklessness priority -= Mathf.Max(0, Vector2.Distance(unit.GetComponent <GridItem>().getPos(), otherUnit.GetComponent <GridItem>().getPos()) - unitControl.wrecklessness); //deuling preference bool weaponsMatch = unit.getCurrentWeapon().type == otherUnit.getCurrentWeapon().type; if ((weaponsMatch && unitControl.deulingPreference) || (!weaponsMatch && !unitControl.deulingPreference)) { priority += 2; } //Agro multiplied by anger if (unitControl.agroTarget == otherUnit) { priority += 10 * unitControl.anger; } priorites.Add(priority); } priorites.ForEach(e => print("Inlist" + e)); int maxIndex = priorites.IndexOf(priorites.Max()); unitControl.target = enemyList[maxIndex]; print("Selected : " + enemyList[maxIndex]); return(enemyList[maxIndex]); }
public void MoveUnit(AIUnitController unit, Vector3 location, int checkCount = 0) { Vector3 startingPosition = unit.GetCurrentTile().LocalLocation; List <GridTile> path = _gridController.FindPath(startingPosition, location); int count = checkCount + 1; int totalLocations = unit.GetLocationCount(); if (path.Count > 0) { UnitState nextState = new CallbackUnitState <AIUnitController>(unit, OnUnitPathFinished, unit as AIUnitController); WalkingState state = new WalkingState(unit, nextState, path, OnUnitPathFail); unit.ChangeState(state); return; } else if (count < totalLocations) { unit.SetNextLocation(); MoveUnit(unit, unit.GetCurrentLocation(), count); } }
// Update is called once per frame void Update() { if (map == null) { initiateController(); } if (isTurn()) { if (currentIndex == playerUnitList.Length) { endTurn(); map.clearDisplay(); currentIndex = 0; return; } if (!(playerUnitList[currentIndex] != null)) { currentIndex++; return; } AIUnitController currentUnitAI = playerUnitList[currentIndex].GetComponent <AIUnitController>(); if (!(currentUnitAI.moved && currentUnitAI.attacked)) { unitScript unit = playerUnitList[currentIndex]; // issue commands to current unit switch (unit.GetComponent <AIUnitController>().state) { case AIState.test: testMove(unit); break; case AIState.hunt: hunt(unit); break; case AIState.fallback: fallBack(unit); break; case AIState.groupUp: groupUp(unit); break; case AIState.recover: recover(unit); break; case AIState.support: support(unit); break; } } else if (playerUnitList[currentIndex].canEndTurn()) { playerUnitList[currentIndex].GetComponent <AIUnitController>().endTurn(); currentIndex++; } } }