public TileHit Scan(Vector2Int gridStartPosition, DirectionEnum _direction, int _radius, TileMapLayer _buildingLayer, IChecker _checker) { startPosition = gridStartPosition; radius = _radius; buildingLayer = _buildingLayer; checker = _checker; direction = _direction; TileSlot currentTile = gridManager.GetTileFromGrid(startPosition, buildingLayer); if (currentTile != null && checker.CheckTile(currentTile)) { return(new TileHit(currentTile, startPosition)); } for (int i = 1; i <= radius; i++) { TileHit tileHit = GetClosestTileInSquare(i); if (tileHit != null) { return(tileHit); } } return(null); }
private TileHit[] GetAllTilesInSquare(int distanceFromCenter) { List <TileHit> tiles = new List <TileHit>(); int numOfTiles = 8 * distanceFromCenter; Vector2Int relativeCheckPosition; switch (direction) { case DirectionEnum.Up: relativeCheckPosition = new Vector2Int(0, 1) * distanceFromCenter; break; case DirectionEnum.Down: relativeCheckPosition = new Vector2Int(0, -1) * distanceFromCenter; break; case DirectionEnum.Left: relativeCheckPosition = new Vector2Int(-1, 0) * distanceFromCenter; break; case DirectionEnum.Right: relativeCheckPosition = new Vector2Int(1, 0) * distanceFromCenter; break; default: relativeCheckPosition = new Vector2Int(0, 1) * distanceFromCenter; Debug.LogError("Added new not existing direction, this is not a 3d game..."); break; } DirectionEnum currentDirection = direction; for (int i = 0; i < numOfTiles; i++) { //Check tile TileSlot currentTile = gridManager.GetTileFromGrid(relativeCheckPosition + startPosition, buildingLayer); if (currentTile != null && checker.CheckTile(currentTile)) { tiles.Add(new TileHit(currentTile, relativeCheckPosition + startPosition)); } //Check if at corner if (Mathf.Abs(relativeCheckPosition.x) == Mathf.Abs(relativeCheckPosition.y)) { switch (currentDirection) { case DirectionEnum.Up: currentDirection = DirectionEnum.Right; break; case DirectionEnum.Down: currentDirection = DirectionEnum.Left; break; case DirectionEnum.Left: currentDirection = DirectionEnum.Up; break; case DirectionEnum.Right: currentDirection = DirectionEnum.Down; break; } } //Update position switch (currentDirection) { case DirectionEnum.Up: relativeCheckPosition += new Vector2Int(1, 0); break; case DirectionEnum.Down: relativeCheckPosition += new Vector2Int(-1, 0); break; case DirectionEnum.Left: relativeCheckPosition += new Vector2Int(0, 1); break; case DirectionEnum.Right: relativeCheckPosition += new Vector2Int(0, -1); break; } } return(tiles.ToArray()); }