public IEnumerator MoveLongerDistance() { longMoveRunning = true; while (playerCellPosition != clickCellPosition) { List <Vector3Int> neighbours = mapManager.GetNeighbours(playerCellPosition, 1); int distToTarget = 9999; Vector3Int nearestNeighbourToTarget = new Vector3Int(); foreach (Vector3Int neighbour in neighbours) { int calcDist = mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), mapManager.evenq2cube(clickCellPosition)); if (calcDist < distToTarget) { nearestNeighbourToTarget = neighbour; distToTarget = calcDist; } } MovePlayer(nearestNeighbourToTarget, false); MoveCount++; resourceAndUpgradeManager.AdjustThreatLevel(MoveCount); uiController.SetThreatLevelSlider(resourceAndUpgradeManager.ThreatLevel); if (resourceAndUpgradeManager.ThreatLevel >= 1) { resourceAndUpgradeManager.MaxThreatLevelAssault(); } if (!movementState) { if (mapManager.saveName != "TutorialFile") { movementState = true; } break; } yield return(new WaitForSeconds(0.2f)); } longMoveRunning = false; yield return(null); }
public void TakeTurn() { if (!shotIncoming) { int distToPlayer = mapManager.HexCellDistance(mapManager.evenq2cube(enemyCellPosition), mapManager.evenq2cube(player.gameObject.GetComponent <MovementController>().playerCellPosition)); GameObject[] rockets = GameObject.FindGameObjectsWithTag("Rocket"); foreach (GameObject rocket in rockets) { int distToRocket = mapManager.HexCellDistance(mapManager.evenq2cube(enemyCellPosition), mapManager.evenq2cube(gridLayout.WorldToCell(rocket.transform.position))); Debug.Log(thisEnemyName + " is " + distToRocket + " hexes from a rocket"); if (distToRocket < resourceAndUpgradeManager.CurrentMaxRocketYield + 2) { runAway = true; } } if (rockets.Length <= 0) { runAway = false; } switch (thisEnemyName) { case "EnemyA": List <Vector3Int> playerFlats = mapManager.GetFlats(3, movementController.playerCellPosition, false); List <Vector3Int> playerEndFlats = mapManager.GetFlats(3, movementController.playerCellPosition, true); Vector3Int nearestEndFlat = playerEndFlats[0]; int distToNearestEndFlat = 999999; foreach (Vector3Int flat in playerEndFlats) { int distToFlat = mapManager.HexCellDistance(mapManager.evenq2cube(enemyCellPosition), mapManager.evenq2cube(flat)); if (distToFlat < distToNearestEndFlat) { distToNearestEndFlat = distToFlat; nearestEndFlat = flat; } } Vector3Int nearestFlat = playerFlats[0]; int distToNearestFlat = 999999; foreach (Vector3Int flat in playerFlats) { int distToFlat = mapManager.HexCellDistance(mapManager.evenq2cube(enemyCellPosition), mapManager.evenq2cube(flat)); if (distToFlat < distToNearestFlat) { distToNearestFlat = distToFlat; nearestFlat = flat; } if (flat == enemyCellPosition) { inFlats = true; break; } else { inFlats = false; } } if (enemyCellPosition != nearestEndFlat && !inFlats || runAway) { if (distToPlayer < 3) { nearestEndFlat = nearestFlat; } List <Vector3Int> neighbours = mapManager.GetNeighbours(enemyCellPosition, 1); Vector3Int shortestMove = new Vector3Int(0, 0, 0); int shortestMoveDist = 100; Vector3Int furthestFromRockets = new Vector3Int(0, 0, 0); int sumFurthestFromRocketsDist = 0; int i = 1; foreach (Vector3Int neighbour in neighbours) { if (i == 1) { shortestMove = neighbour; shortestMoveDist = mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), mapManager.evenq2cube(nearestEndFlat)); if (rockets.Length > 0) { furthestFromRockets = neighbour; sumFurthestFromRocketsDist = mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), mapManager.evenq2cube(gridLayout.WorldToCell(rockets[0].transform.position))); } } else { if (mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), mapManager.evenq2cube(nearestEndFlat)) < shortestMoveDist) { shortestMove = neighbour; shortestMoveDist = mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), mapManager.evenq2cube(nearestEndFlat)); } int thisSumFurthestFromRocketsDist = 0; foreach (GameObject rocket in rockets) { thisSumFurthestFromRocketsDist += mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), mapManager.evenq2cube(gridLayout.WorldToCell(rocket.transform.position))); } if (thisSumFurthestFromRocketsDist > sumFurthestFromRocketsDist) { furthestFromRockets = neighbour; sumFurthestFromRocketsDist = thisSumFurthestFromRocketsDist; } } i++; } foreach (EnemyObject listEnemy in mapManager.spawnedEnemies) { if (listEnemy.xCoordinate == thisEnemyObject.xCoordinate && listEnemy.yCoordinate == thisEnemyObject.yCoordinate) { mapManager.spawnedEnemies.Remove(listEnemy); break; } } if (runAway) { SetOrientation(gridLayout.CellToWorld(furthestFromRockets)); transform.position += (gridLayout.CellToWorld(furthestFromRockets) - gridLayout.CellToWorld(enemyCellPosition)); } else { SetOrientation(gridLayout.CellToWorld(shortestMove)); transform.position += (gridLayout.CellToWorld(shortestMove) - gridLayout.CellToWorld(enemyCellPosition)); } enemyCellPosition = gridLayout.WorldToCell(transform.position); thisEnemyObject = new EnemyObject(enemyCellPosition.x, enemyCellPosition.y, thisEnemyObject.enemyString); mapManager.spawnedEnemies.Add(thisEnemyObject); } else { Instantiate(enemyLaser, new Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity); } break; case "EnemyB": if (distToPlayer > 1 || runAway) { List <Vector3Int> neighbours = mapManager.GetNeighbours(enemyCellPosition, 1); Vector3Int shortestMove = new Vector3Int(0, 0, 0); int shortestMoveDist = 100; Vector3Int furthestFromRockets = new Vector3Int(0, 0, 0); int sumFurthestFromRocketsDist = 0; int i = 1; foreach (Vector3Int neighbour in neighbours) { if (i == 1) { shortestMove = neighbour; shortestMoveDist = mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), movementController.playerCellPositionCubeCoords); if (rockets.Length > 0) { furthestFromRockets = neighbour; sumFurthestFromRocketsDist = mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), mapManager.evenq2cube(gridLayout.WorldToCell(rockets[0].transform.position))); } } else { if (mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), movementController.playerCellPositionCubeCoords) < shortestMoveDist) { shortestMove = neighbour; shortestMoveDist = mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), movementController.playerCellPositionCubeCoords); } int thisSumFurthestFromRocketsDist = 0; foreach (GameObject rocket in rockets) { thisSumFurthestFromRocketsDist += mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), mapManager.evenq2cube(gridLayout.WorldToCell(rocket.transform.position))); } if (thisSumFurthestFromRocketsDist > sumFurthestFromRocketsDist) { furthestFromRockets = neighbour; sumFurthestFromRocketsDist = thisSumFurthestFromRocketsDist; } } i++; } foreach (EnemyObject listEnemy in mapManager.spawnedEnemies) { if (listEnemy.xCoordinate == thisEnemyObject.xCoordinate && listEnemy.yCoordinate == thisEnemyObject.yCoordinate) { mapManager.spawnedEnemies.Remove(listEnemy); break; } } if (runAway) { SetOrientation(gridLayout.CellToWorld(furthestFromRockets)); transform.position += (gridLayout.CellToWorld(furthestFromRockets) - gridLayout.CellToWorld(enemyCellPosition)); } else { SetOrientation(gridLayout.CellToWorld(shortestMove)); transform.position += (gridLayout.CellToWorld(shortestMove) - gridLayout.CellToWorld(enemyCellPosition)); } enemyCellPosition = gridLayout.WorldToCell(transform.position); thisEnemyObject = new EnemyObject(enemyCellPosition.x, enemyCellPosition.y, thisEnemyObject.enemyString); mapManager.spawnedEnemies.Add(thisEnemyObject); } else { Instantiate(enemyLaser, new Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity); } break; case "EnemyC": if (distToPlayer > 2 || runAway) { List <Vector3Int> neighbours = mapManager.GetNeighbours(enemyCellPosition, 1); Vector3Int shortestMove = new Vector3Int(0, 0, 0); int shortestMoveDist = 100; Vector3Int longestJump = new Vector3Int(0, 0, 0); int longestJumpDist = 0; Vector3Int furthestFromRockets = new Vector3Int(0, 0, 0); int sumFurthestFromRocketsDist = 0; int i = 1; int randJump = Random.Range(0, 2); if (jumpChargeTracker < 1 && randJump > 0.1) { jumpChargeTracker = 1; List <Vector3Int> playerNeighbours = mapManager.GetNeighbours(movementController.playerCellPosition, 2); foreach (Vector3Int playerNeighbour in playerNeighbours) { if (mapManager.HexCellDistance(mapManager.evenq2cube(playerNeighbour), mapManager.evenq2cube(enemyCellPosition)) > longestJumpDist) { longestJump = playerNeighbour; longestJumpDist = mapManager.HexCellDistance(mapManager.evenq2cube(playerNeighbour), mapManager.evenq2cube(enemyCellPosition)); } } foreach (EnemyObject listEnemy in mapManager.spawnedEnemies) { if (listEnemy.xCoordinate == thisEnemyObject.xCoordinate && listEnemy.yCoordinate == thisEnemyObject.yCoordinate) { mapManager.spawnedEnemies.Remove(listEnemy); break; } } SetOrientation(gridLayout.CellToWorld(longestJump)); transform.position += (gridLayout.CellToWorld(longestJump) - gridLayout.CellToWorld(enemyCellPosition)); enemyCellPosition = gridLayout.WorldToCell(transform.position); thisEnemyObject = new EnemyObject(enemyCellPosition.x, enemyCellPosition.y, thisEnemyObject.enemyString); mapManager.spawnedEnemies.Add(thisEnemyObject); } else { foreach (Vector3Int neighbour in neighbours) { if (i == 1) { shortestMove = neighbour; shortestMoveDist = mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), movementController.playerCellPositionCubeCoords); if (rockets.Length > 0) { furthestFromRockets = neighbour; sumFurthestFromRocketsDist = mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), mapManager.evenq2cube(gridLayout.WorldToCell(rockets[0].transform.position))); } } else { if (mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), movementController.playerCellPositionCubeCoords) < shortestMoveDist) { shortestMove = neighbour; shortestMoveDist = mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), movementController.playerCellPositionCubeCoords); } int thisSumFurthestFromRocketsDist = 0; foreach (GameObject rocket in rockets) { thisSumFurthestFromRocketsDist += mapManager.HexCellDistance(mapManager.evenq2cube(neighbour), mapManager.evenq2cube(gridLayout.WorldToCell(rocket.transform.position))); } if (thisSumFurthestFromRocketsDist > sumFurthestFromRocketsDist) { furthestFromRockets = neighbour; sumFurthestFromRocketsDist = thisSumFurthestFromRocketsDist; } } i++; } foreach (EnemyObject listEnemy in mapManager.spawnedEnemies) { if (listEnemy.xCoordinate == thisEnemyObject.xCoordinate && listEnemy.yCoordinate == thisEnemyObject.yCoordinate) { mapManager.spawnedEnemies.Remove(listEnemy); break; } } if (runAway) { SetOrientation(gridLayout.CellToWorld(furthestFromRockets)); transform.position += (gridLayout.CellToWorld(furthestFromRockets) - gridLayout.CellToWorld(enemyCellPosition)); } else { SetOrientation(gridLayout.CellToWorld(shortestMove)); transform.position += (gridLayout.CellToWorld(shortestMove) - gridLayout.CellToWorld(enemyCellPosition)); } enemyCellPosition = gridLayout.WorldToCell(transform.position); thisEnemyObject = new EnemyObject(enemyCellPosition.x, enemyCellPosition.y, thisEnemyObject.enemyString); mapManager.spawnedEnemies.Add(thisEnemyObject); if (jumpChargeTracker >= 1) { jumpChargeTracker--; } } } else { Instantiate(enemyLaser, new Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity); if (jumpChargeTracker >= 1) { jumpChargeTracker--; } } break; default: break; } } }