/// <summary> /// Moves a unit across x tiles. /// </summary> private IEnumerator MoveToTiles() { Vector3 oldTile = _pathfinder.GetGeneratedPathAt(0); Vector3 newTile = Vector3.zero; int index = 0; while (_pathfinder.GetGeneratedPath() != null && index < _pathfinder.GetGeneratedPath().Count - 1) { newTile = _pathfinder.GetGeneratedPathAt(index + 1); Vector3 startingPosition = TileMapUtil.TileMapToWorldCentered(_pathfinder.GetGeneratedPathAt(index), tileMap.TileSize); Vector3 endingPosition = TileMapUtil.TileMapToWorldCentered(newTile, tileMap.TileSize); yield return(StartCoroutine(MoveToTile(controller.HighlightedUnit, startingPosition, endingPosition))); index++; yield return(null); } _pathfinder.Clear(); // After move is finished, swap out tile unit is standing on if (!TileMapUtil.IsInvalidTile(oldTile)) { TileMapData tileMapData = tileMap.GetTileMapData(); TileData oldTileData = tileMapData.GetTileDataAt(oldTile); oldTileData.SwapUnits(tileMapData.GetTileDataAt(newTile)); controller.HighlightedUnit.Tile = newTile; } yield break; }
/// <summary> /// Raises the cancel button clicked event. /// </summary> private void OnCancelButtonClicked() { // Get active unit when cancel was invoked Unit unit = controller.TurnOrderController.GetNextUp(); // Set back to selected color unit.Highlight(); // Swap unit back to old tile TileMapData tileMapData = controller.TileMap.GetTileMapData(); TileData oldTileData = tileMapData.GetTileDataAt(controller.OldUnitPosition); TileData currentTileData = tileMapData.GetTileDataAt(controller.CurrentUnitPosition); currentTileData.SwapUnits(oldTileData); // Update tile position on Unit unit.Tile = controller.OldUnitPosition; // Swap actual world centered position to old position unit.transform.position = TileMapUtil.TileMapToWorldCentered(controller.OldUnitPosition, controller.TileMap.TileSize); // Revert to old walking animation unit.FacedDirection = controller.OldUnitTileDirection; unit.GetAnimationController().PlayWalkingAnimation(unit); // Swap cached position on controller controller.CurrentUnitPosition = controller.OldUnitPosition; // Highlight unit controller.HighlightCharacter(unit); // Highlight tiles around unit unit.TileHighlighter.HighlightTiles(unit, unit.Tile); // Clear out radial button container list controller.RadialButtonContainers.Clear(); // Destroy radial menu Destroy(_radialMenuController.gameObject); // Revert back to prior state controller.ChangeState <PlayerSelectedState> (); }
public void TestGeneratePathAvoidUnWalkableTiles() { List <Node> generatedPath = _validIgnoreUnWalkablePathfinder.GetGeneratedPath(); Assert.AreEqual(7, generatedPath.Count); foreach (Node node in generatedPath) { TileData tileData = _tileMapData.GetTileDataAt(node.x, node.z); Assert.AreEqual(TileData.TerrainTypeEnum.GRASS, tileData.TerrainType); } }
/// <summary> /// Costs to enter tile. /// </summary> /// <returns>The to enter tile.</returns> /// <param name="sourceX">Source x.</param> /// <param name="sourceZ">Source z.</param> /// <param name="targetX">Target x.</param> /// <param name="targetZ">Target z.</param> private float CostToEnterTile(int sourceX, int sourceZ, int targetX, int targetZ) { TileData tileData = _tileMapData.GetTileDataAt(targetX, targetZ); if (!tileData.IsWalkable) { return(Mathf.Infinity); } float cost = Mathf.Abs(tileData.MovementModifier); if (sourceX != targetX && sourceZ != targetZ) { // We are moving diagonally! Fudge the cost for tie-breaking // Purely a cosmetic thing! cost += 0.001f; } return(cost); }
/// <summary> /// Sets the generated path. /// </summary> /// <param name="unit">Unit.</param> /// <param name="targetTile">Target tile.</param> protected void SetGeneratedPath(Unit unit, Vector3 targetTile) { // Generate path to target tile _pathfinder.GeneratePath(unit.Tile, targetTile); // Get movement tiles for unit so unreachable tiles can be filtered from the generated path Dictionary <Vector3, Object> tiles = _tileDiscoverer.DiscoverTilesInRange(unit.Tile, (int)unit.GetMovementAttribute().CurrentValue); // Lop off tiles from path that aren't within range or that are occupied for (int index = _pathfinder.GetGeneratedPath().Count - 1; index >= 0; index--) { Vector3 tile = _pathfinder.GetGeneratedPathAt(index); TileData tileData = _tileMapData.GetTileDataAt((int)tile.x, (int)tile.z); if (!tiles.ContainsKey(_pathfinder.GetGeneratedPathAt(index)) || tileData.Unit) { _pathfinder.RemoveGeneratedPathAt(index); } else { break; } } }
public void TestGetTileAtByCoordinates() { TileData tileData = _tileMapData.GetTileDataAt(4, 4); RunTileDataAssertions(tileData); }