/// <summary> /// Executes all hitboxes at a given unit's position. /// </summary> /// <param name="unit">The unit to be affected.</param> private void CheckHitboxesAtUnit(Unit unit) { foreach (Hitbox hitbox in hitboxes) { if (UtilityFunctions.CompareVector3Ints(hitbox.GetPosition(), unit.GetPosition())) { hitbox.ExecuteHitbox(unit); } } }
/// <summary> /// Gets the <see cref="LoadingZone"/> at a given position, if it exists. /// </summary> /// <param name="position">The position of the LoadingZone.</param> /// <returns>Returns the LoadingZone at the location, or null if there is none.</returns> private LoadingZone GetLoadingZoneAtPosition(Vector3Int position) { foreach (LoadingZone loadingZone in loadingZones) { if (UtilityFunctions.CompareVector3Ints(loadingZone.position, position)) { return(loadingZone); } } return(null); }
/// <summary> /// Checks if a given location is within the effect area. /// </summary> /// <param name="location">The location to check for.</param> /// <returns>Returns true if the location is within the effect area, and false otherwise.</returns> public bool CheckForLocation(Vector3Int location) { List <Vector3Int> allTiles = GetAllTiles(); foreach (Vector3Int tile in allTiles) { if (UtilityFunctions.CompareVector3Ints(location, tile)) { return(true); } } return(false); }
/// <summary> /// /// </summary> /// <param name="position"></param> /// <returns></returns> public List <OverworldObject> GetOverworldObjectsAtPosition(Vector3Int position) { List <OverworldObject> owObjects = new List <OverworldObject>(); for (int i = 0; i < overworldObjects.Count; i++) { if (UtilityFunctions.CompareVector3Ints(overworldObjects[i].GetPosition(), position)) { owObjects.Add(overworldObjects[i]); } } return(owObjects); }
/// <summary> /// Finds the shortest traversable path from one location to another. /// </summary> /// <param name="unit">Unit attempting to travel.</param> /// <param name="to">Location to pathfind to.</param> /// <returns>Returns the direction for the unit to move to next to reach the desired location, or returns /// <see cref="Directions.Down"/> if there is none.</returns> public Directions PathfindTo(Unit unit, Vector3Int to) { Vector3Int from = unit.position; List <AStarNode> openList = new List <AStarNode>(); List <AStarNode> closedList = new List <AStarNode>(); AStarNode start = new AStarNode(null, from, 0, 0); openList.Add(start); AStarNode endNode = null; int passCount = 0; //keep searching for route while (openList.Count > 0) { passCount++; AStarNode shortestNode = null; for (int i = 0; i < openList.Count; i++) { AStarNode node = openList[i]; if (shortestNode == null || shortestNode.GetTotalDistance() > node.GetTotalDistance()) { shortestNode = node; } } openList.Remove(shortestNode); List <AStarNode> neighbours = new List <AStarNode>(); List <Vector3Int> positions = GetSurroundingTiles(unit, shortestNode.position); //find valid tiles of movement for the unit foreach (Vector3Int position in positions) { AStarNode n = new AStarNode(shortestNode, position, shortestNode.GetDistanceFromStart() + 1, CalculateTileDistance(position, to)); neighbours.Add(n); } //checking for endpoint, adding valid tiles to openList for next pass foreach (AStarNode neighbour in neighbours) { if (UtilityFunctions.CompareVector3Ints(neighbour.position, to)) { endNode = neighbour; break; } bool shouldSkip = false; for (int i = 0; i < openList.Count; i++) { if (UtilityFunctions.CompareVector3Ints(neighbour.position, openList[i].position) && neighbour.GetTotalDistance() >= openList[i].GetTotalDistance()) { shouldSkip = true; } } for (int i = 0; i < closedList.Count; i++) { if (UtilityFunctions.CompareVector3Ints(neighbour.position, closedList[i].position) && neighbour.GetTotalDistance() >= closedList[i].GetTotalDistance()) { shouldSkip = true; } } if (shouldSkip) { continue; } else { openList.Add(neighbour); } } if (endNode != null) { break; } else { closedList.Add(shortestNode); } } //if there is a path, figure out how to move the unit 1 step closer if (endNode != null) { AStarNode firstMovement = endNode; while (firstMovement.parent != null) { AStarNode temp = firstMovement.parent; if (temp.parent != null) { firstMovement = firstMovement.parent; } else { break; } } Vector3Int moveTo = firstMovement.position; //TODO: need to adjust for z-y offset; int dx = unit.position.x - moveTo.x; int dy = unit.position.y - moveTo.y; int dz = unit.position.z - moveTo.z; if (dx > 0) { return(Directions.SW); } else if (dx < 0) { return(Directions.NE); } if (dy > 0) { return(Directions.SE); } else if (dy < 0) { return(Directions.NW); } return(Directions.Up); } else { Debug.Log("no path found"); return(Directions.None); } }