public static HashSet <Tile> ComputeTileInRange(SpaceObject ship, bool blockingTile) { Tile startTile = ship.GetReferenceTile(); List <Tile> shipTiles = ship.GetTiles(); if (startTile.GetComponent <Tile> () == null || ship.GetMoveRange() < 0) { Debug.LogError("invalid argument"); return(null); } HashSet <Tile> TilesInRange = new HashSet <Tile> (); HashSet <Tile> TilesInRangeTemp = new HashSet <Tile> (); HashSet <Tile> OpenTiles = new HashSet <Tile> (); /*foreach (GameObject shipTile in shipTiles) * OpenTiles.Add (shipTile);*/ OpenTiles.Add(startTile); for (int range = 0; range <= ship.GetMoveRange(); range++) { HashSet <Tile> OpenTilesTemp = new HashSet <Tile> (OpenTiles); foreach (Tile currentOpenTile in OpenTilesTemp) { List <Tile> currentOpenTileNeighboors = currentOpenTile.GetNeighboors(ship.GetClearance()); OpenTiles.Remove(currentOpenTile); TilesInRange.Add(currentOpenTile); //Debug.Log("TilesInRange.count = " +TilesInRange.Count); //Debug.Log("currentOpenTileNeighboors.count = " +currentOpenTileNeighboors.Count); foreach (Tile TileNeighboor in currentOpenTileNeighboors) { if ((TileNeighboor.GetFree() == true || TileNeighboor.GetSpaceObjectOnTile() == ship || blockingTile == false) && OpenTiles.Contains(TileNeighboor) == false) { OpenTiles.Add(TileNeighboor); } } } } TilesInRangeTemp = new HashSet <Tile> (TilesInRange); foreach (Tile rangeTile in TilesInRangeTemp) { if (ship.GetClearance() > 1) { foreach (Tile boxTile in Utilities.RecoverBox(rangeTile, ship.GetClearance())) { if (TilesInRange.Contains(boxTile) == false && (boxTile.GetFree() == true || blockingTile == false)) { TilesInRange.Add(boxTile); } } } else { TilesInRange.Add(rangeTile); } } return(TilesInRange); }
public void StartAction() { if (m_EngineSelected == true) { int clearance = m_SpaceObjectSelected.GetClearance(); Tile destinationTile = GridPathFinder.Instance.RecomputingDestination(m_TileUnderMouse, clearance); StartCoroutine(MoveManager.Instance.StartMove(destinationTile, m_SpaceObjectSelected)); } else if (m_WeaponSelected != null) { if (m_TileUnderMouse != null) { Debug.Log("changing target"); AttackManager.Instance.ChangeTarget(m_TileUnderMouse.GetSpaceObjectOnTile()); } } }
public void ReleaseTile(Tile referenceTile, SpaceObject spaceObject) { Debug.Log("releasing tile = " + referenceTile.gameObject.name); List <Tile> releaseTiles = Utilities.RecoverClearanceBox(referenceTile, spaceObject.GetClearance()); spaceObject.GetTiles().Clear(); foreach (Tile releaseTile in releaseTiles) { releaseTile.SetFree(true); GridPathFinder.Instance.ComputeTileClearance(releaseTile); GridPathFinder.Instance.ComputeNeighboorsClearance(releaseTile); } }
public void LockTile(Tile referenceTile, SpaceObject spaceObject) { List <Tile> lockTileList = Utilities.RecoverClearanceBox(referenceTile, spaceObject.GetClearance()); spaceObject.SetReferenceTile(referenceTile); spaceObject.GetTiles().Clear(); foreach (Tile lockTile in lockTileList) { lockTile.SetFree(false, spaceObject.gameObject); spaceObject.SetTiles(lockTile); GridPathFinder.Instance.ComputeNeighboorsClearance(lockTile); } }
public static List <Tile> FindDirectPath(SpaceObject ship, Tile EndTile) { //Debug.Log ("Starting pathfinding for destination : " + EndTile.name); bool noObstacle = false; int clearance = ship.GetClearance(); Tile StartTile = ship.GetReferenceTile(); List <Tile> TotalPath = new List <Tile>(); List <Node> OpenNodes = new List <Node>(); List <Node> CloseNodes = new List <Node>(); HashSet <Tile> OpenTileHashSet = new HashSet <Tile>(); HashSet <Tile> CloseTileHashSet = new HashSet <Tile>(); float Gscore = 0; float Fscore = Gscore + Vector3.Distance(StartTile.transform.position, EndTile.transform.position); Node CurrentNode = new Node(StartTile, null, Fscore, Gscore); OpenNodes.Add(CurrentNode); while (OpenNodes.Count != 0) { CurrentNode = NodeWithLowestFscore(OpenNodes); if (CheckNoObstacle(CurrentNode.GetTile(), EndTile, clearance) == true) { noObstacle = true; CurrentNode = new Node(EndTile, CurrentNode, 0, 0); } if (CurrentNode.GetTile() == EndTile || noObstacle == true) { TotalPath = ReconstructDirectPath(clearance, CurrentNode, StartTile); return(TotalPath); } OpenNodes.Remove(CurrentNode); OpenTileHashSet.Remove(CurrentNode.GetTile()); CloseNodes.Add(CurrentNode); CloseTileHashSet.Add(CurrentNode.GetTile()); foreach (Tile tile in CurrentNode.GetTile().GetNeighboors(clearance)) { if (TileInListNodes(tile, CloseNodes)) { if (CloseTileHashSet.Contains(tile)) { continue; } } float tentative_Gscore = CurrentNode.GetGscore() + Vector3.Distance(tile.transform.position, CurrentNode.GetTile().transform.position); if (!OpenTileHashSet.Contains(tile) || OpenNodes.Find(x => x.GetTile() == tile).GetGscore() > tentative_Gscore) { if (!OpenTileHashSet.Contains(tile) && tile.GetFree() && tile.GetMoveThrough()) { Fscore = tentative_Gscore + Vector3.Distance(tile.transform.position, EndTile.transform.position); OpenNodes.Add(new Node(tile, CurrentNode, Fscore, tentative_Gscore)); OpenTileHashSet.Add(tile); } } } } return(TotalPath); }