Example #1
0
    public MapSegmentPathTile GetPathTileForCordinate(Point point, List <MapSegmentLayer> layers)
    {
        var result = new MapSegmentPathTile();

        var isWalkable = false;

        foreach (var layer in layers)
        {
            var tileType = layer.TilesCollection.GetTileType(point.ToInt2Vector());

            // Base layer overrides
            if (layer.TileSetLayer.Tiles[tileType].Pathing == TilePathing.BaseWalkable)
            {
                isWalkable = true;
            }
            else if (layer.TileSetLayer.Tiles[tileType].Pathing == TilePathing.OverlayUnwalkable)
            {
                isWalkable = false;
            }
            else if (layer.TileSetLayer.Tiles[tileType].Pathing == TilePathing.OverlayWalkable)
            {
                isWalkable = true;
            }
        }

        result.IsWalkable = isWalkable;
        return(result);
    }
    public HashSet <MapSegmentPathTile> DepthFirstSearch(MapSegmentPathTile startTile, bool isWalkable)
    {
        var result = new HashSet <MapSegmentPathTile>();

        DepthFirstSearch_r(result, startTile, isWalkable);

        return(result);
    }
 public static bool IsFree(MapSegmentPathTile tile)
 {
     if (tile == null)
     {
         return(NULL_TILE_IS_WALKABLE);
     }
     else
     {
         return(tile.IsWalkable);
     }
 }
    public ICollection <MapSegmentPathTile> GetOwningTileGroup(MapSegmentPathTile tile, List <HashSet <MapSegmentPathTile> > tileGroups)
    {
        foreach (var tileGroup in tileGroups)
        {
            if (tileGroup.Contains(tile))
            {
                return(tileGroup);
            }
        }

        return(null);
    }
    public MapSegmentPathTile FindStartTile(ICollection <MapSegmentPathTile> tiles, MapSegmentDirection direction)
    {
        foreach (var tile in tiles)
        {
            if (MapSegmentPathTile.IsFree(tile.NeighbourUp))
            {
                return(tile);
            }
        }

        // In theory; this is not possible
        return(null);
    }
    public void CheckWalkableGroups(MapSegmentPathTile tile, List <HashSet <MapSegmentPathTile> > walkableGroups)
    {
        if (tile == null)
        {
            return;
        }

        foreach (var walkableGroup in walkableGroups.ToList())
        {
            if (walkableGroup.Contains(tile))
            {
                walkableGroups.Remove(walkableGroup);
            }
        }
    }
    public List <Vector2> GetGroupPoints(ICollection <MapSegmentPathTile> tiles, MapSegmentPathTile startTile, MapSegmentDirection startDirection)
    {
        var result    = new List <Vector2>();
        var startNode = new MapSegmentTraverseResult {
            PathTile = startTile, Direction = startDirection
        };

        var currentNode = startNode;
        var point       = startNode.PathTile.GetStartPoint(startDirection).ToVector2();

        result.Add(point);

        while (!CheckTerminationCondition(currentNode, startNode, result))
        {
            point = currentNode.PathTile.GetEndPoint(currentNode.Direction).ToVector2();
            result.Add(point);
            currentNode = GetNextNode(currentNode);
        }

        return(result);
    }
    public MapSegmentPathTile GetNeighbour(MapSegmentDirection direction)
    {
        MapSegmentPathTile result = null;

        if (direction == MapSegmentDirection.Up)
        {
            result = NeighbourUp;
        }
        else if (direction == MapSegmentDirection.Down)
        {
            result = NeighbourDown;
        }
        else if (direction == MapSegmentDirection.Left)
        {
            result = NeighbourLeft;
        }
        else if (direction == MapSegmentDirection.Right)
        {
            result = NeighbourRight;
        }

        return(result);
    }
    protected void DepthFirstSearch_r(HashSet <MapSegmentPathTile> addedTiles, MapSegmentPathTile currentTile, bool isWalkable)
    {
        if (currentTile == null)
        {
            return;
        }

        if (addedTiles.Contains(currentTile))
        {
            return;
        }

        if (currentTile.IsWalkable != isWalkable)
        {
            return;
        }

        addedTiles.Add(currentTile);

        foreach (var tile in currentTile.GetNeighbours())
        {
            DepthFirstSearch_r(addedTiles, tile, isWalkable);
        }
    }
 public MapSegmentPathTile SetCordinate(int x, int y, MapSegmentPathTile tile)
 {
     PathingMap[x, y] = tile;
     tile.Point       = new Point(x, y);
     return(tile);
 }
 public MapSegmentPathTile SetCordinate(Point point, MapSegmentPathTile tile)
 {
     return(SetCordinate(point.X, point.Y, tile));
 }
    public MapSegmentTraverseResult GetNextNode(MapSegmentTraverseResult lastStep)
    {
        if (lastStep.Direction == MapSegmentDirection.Up)
        {
            var nextTile = lastStep.PathTile.NeighbourRight;
            if (MapSegmentPathTile.IsFree(nextTile))
            {
                return(new MapSegmentTraverseResult {
                    PathTile = lastStep.PathTile, Direction = MapSegmentDirection.Right
                });
            }
            else
            {
                if (MapSegmentPathTile.IsFree(nextTile.NeighbourUp))
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile, Direction = lastStep.Direction
                    });
                }
                else
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile.NeighbourUp, Direction = MapSegmentDirection.Left
                    });
                }
            }
        }
        else if (lastStep.Direction == MapSegmentDirection.Right)
        {
            var nextTile = lastStep.PathTile.NeighbourDown;
            if (MapSegmentPathTile.IsFree(nextTile))
            {
                return(new MapSegmentTraverseResult {
                    PathTile = lastStep.PathTile, Direction = MapSegmentDirection.Down
                });
            }
            else
            {
                if (MapSegmentPathTile.IsFree(nextTile.NeighbourRight))
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile, Direction = lastStep.Direction
                    });
                }
                else
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile.NeighbourRight, Direction = MapSegmentDirection.Up
                    });
                }
            }
        }
        else if (lastStep.Direction == MapSegmentDirection.Down)
        {
            var nextTile = lastStep.PathTile.NeighbourLeft;
            if (MapSegmentPathTile.IsFree(nextTile))
            {
                return(new MapSegmentTraverseResult {
                    PathTile = lastStep.PathTile, Direction = MapSegmentDirection.Left
                });
            }
            else
            {
                if (MapSegmentPathTile.IsFree(nextTile.NeighbourDown))
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile, Direction = lastStep.Direction
                    });
                }
                else
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile.NeighbourDown, Direction = MapSegmentDirection.Right
                    });
                }
            }
        }
        else if (lastStep.Direction == MapSegmentDirection.Left)
        {
            var nextTile = lastStep.PathTile.NeighbourUp;
            if (MapSegmentPathTile.IsFree(nextTile))
            {
                return(new MapSegmentTraverseResult {
                    PathTile = lastStep.PathTile, Direction = MapSegmentDirection.Up
                });
            }
            else
            {
                if (MapSegmentPathTile.IsFree(nextTile.NeighbourLeft))
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile, Direction = lastStep.Direction
                    });
                }
                else
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile.NeighbourLeft, Direction = MapSegmentDirection.Down
                    });
                }
            }
        }

        // Theorically impossible but catches and handles errors
        // TODO: Implement exceptions and exception handling for this kind of errors
        return(null);
    }
 public MapSegmentPathing(int width, int height)
 {
     PathingMap = new MapSegmentPathTile[width, height];
     Width      = width;
     Height     = height;
 }
 public MapSegmentPathing()
 {
     PathingMap = new MapSegmentPathTile[0, 0];
     Width      = 0;
     Height     = 0;
 }