Пример #1
0
    protected int FloodFill(ref GameTile tile, ref List <List <GameTile> > FloodFilledAreas)
    {
        //starting from the provided tile, mark all connected tiles (meaning stop at walls)
        //this essentially produces a cut out of a room that is reachable, i.e. no walls blocking off certian parts
        //it can also return a int count of the number of tiles in the area cut out
        List <GameTile> allMarkedCells  = new List <GameTile>();
        List <GameTile> validNeighbours = new List <GameTile>();
        int             count           = 1;

        grid[tile.GetX()][tile.GetY()].SetIsMarked(true);
        allMarkedCells.Add(grid[tile.GetX()][tile.GetY()]);
        AddValidNeighbours(ref validNeighbours, tile);
        List <GameTile> nextValidNeighbours = new List <GameTile>();

        do
        {
            nextValidNeighbours.Clear();
            count += validNeighbours.Count;
            for (int x = 0; x < validNeighbours.Count; x++)
            {
                validNeighbours[x].SetIsMarked(true);
                allMarkedCells.Add(validNeighbours[x]);
                AddValidNeighbours(ref nextValidNeighbours, validNeighbours[x]);
            }
            validNeighbours = new List <GameTile>(nextValidNeighbours);
        } while (nextValidNeighbours.Count != 0);
        FloodFilledAreas.Add(allMarkedCells);
        return(count);
    }
Пример #2
0
 public bool InBounds(GameTile tile)
 {
     if ((tile.GetX() < cols) && (tile.GetX() >= 0) && (tile.GetY() < rows) && (tile.GetY() >= 0))
     {
         return(true);
     }
     return(false);
 }
Пример #3
0
 private void BreakWall(GameTile wall)
 {
     //sets the wall sprite to water and apply data changes
     grid[wall.GetX()][wall.GetY()].SetIsDestroyed(false);
     grid[wall.GetX()][wall.GetY()].SetIsWall(false);
     grid[wall.GetX()][wall.GetY()].SetIsWalkAble(false);
     grid[wall.GetX()][wall.GetY()].SetIsOccupied(true);
     grid[wall.GetX()][wall.GetY()].GetObject().GetComponent <SpriteRenderer>().sprite = waterSprite;
     //todo:Add color settings
 }
Пример #4
0
    private GameTile FindDividerWall(GameTile isolatedWaterTile)
    {
        //return the wall that is causing isolatedWaterTile to be isolated from the main body of water
        List <GameTile> isolatedWaterTileNeighbours = board.GetTileCardinalNeighbours(isolatedWaterTile);
        List <GameTile> wallNeighbours = new List <GameTile>();

        foreach (GameTile possibleWall in isolatedWaterTileNeighbours)
        {
            if (possibleWall.IsWall())
            {
                wallNeighbours.Add(possibleWall);
            }
        }
        foreach (GameTile possibleDivider in wallNeighbours)
        {
            List <GameTile> possibleDividerNeighbours = board.GetTileCardinalNeighbours(possibleDivider);
            foreach (GameTile possibleMainWaterTile in possibleDividerNeighbours)
            {
                if (possibleMainWaterTile != isolatedWaterTile)                   //dont check the original tile
                {
                    if (possibleMainWaterTile.IsWater())
                    {
                        return(possibleDivider);
                    }
                }
            }
        }
        Debug.Log("Couldn't find divider wall for water generation error!");
        Debug.Log("Tile is: " + isolatedWaterTile.GetX() + ", " + isolatedWaterTile.GetY());
        return(isolatedWaterTile);
    }
Пример #5
0
    public GameTile GetTileWest(GameTile tile)
    {
        GameTile tileLeft = new GameTile(tile.GetX() - 1, tile.GetY(), tile.GetOriginalSprite());

        if (InBounds(tileLeft))
        {
            return(GetTile(tileLeft));
        }
        return(null);
    }
Пример #6
0
    public GameTile GetRightBotCorner(GameTile tile)
    {
        GameTile tileRightBotCorner = new GameTile(tile.GetX() + 1, tile.GetY() - 1, tile.GetOriginalSprite());

        if (InBounds(tileRightBotCorner))
        {
            return(GetTile(tileRightBotCorner));
        }
        return(null);
    }
Пример #7
0
    public GameTile GetTileSouth(GameTile tile)
    {
        GameTile tileSouth = new GameTile(tile.GetX(), tile.GetY() - 1, tile.GetOriginalSprite());

        if (InBounds(tileSouth))
        {
            return(GetTile(tileSouth));
        }
        return(null);
    }
Пример #8
0
    public GameTile GetTileEast(GameTile tile)
    {
        GameTile tileRight = new GameTile(tile.GetX() + 1, tile.GetY(), tile.GetOriginalSprite());

        if (InBounds(tileRight))
        {
            return(GetTile(tileRight));
        }
        return(null);
    }
Пример #9
0
 public override void Grass(GameTile currentTile)
 {
     if (currentTile.isTilled)
     {
         //Do nothing, thump sound
         Manager.gameTileManager.WaterTilledTile(currentTile.GetX(), currentTile.GetY());
         currentTile.SetIsWatered(true);
     }
     else
     {
         //Do nothing and still make sounds
     }
 }
Пример #10
0
    private void BreakWallsRandomlyAroundWater(ref List <List <GameTile> > waterTilesSeperatedByAreas, ref List <GameTile> waterEdgeTiles)
    {
        //grab the valid walls around our water areas (walls that are touching floor tiles) and break them (turn them into water tiles)
        //hands back a list of all the walls broken(turned into water) since they are now water they serve as the edges of our water areas
        int   divisor = 4;                      // What fraction of walls should we ensured are punched out around the water? if divisor = 3 then 1/3 (rounded down) of all walls around a water area will be removed
        float chanceForOtherWallBreaks = 0.10f; // after the ensured walls every other wall has this chance of breaking, for example if set to .45f every other wall would have a 45% chance of breaking

        board.UnMarkAllTiles();
        List <List <GameTile> > wallsAroundWaterTilesSeperatedByAreas = GetWallsSurroundingWaterAreas(ref waterTilesSeperatedByAreas);

        for (int i = 0; i < wallsAroundWaterTilesSeperatedByAreas.Count; i++)
        {
            int ensuredWallBreaks = wallsAroundWaterTilesSeperatedByAreas[i].Count / divisor;
            ensuredWallBreaks = (ensuredWallBreaks <= 0) ? 1 : ensuredWallBreaks;             //atleast always destory one wall around a water area
            //walk down all of the walls for a given area, selecting a wall at random and breaking it, repeat a number of times equal to our ensuredWallBreaks
            for (int c = 0; c < ensuredWallBreaks; c++)
            {
                int randomWall = (int)(Random.Range(0.0f, (float)wallsAroundWaterTilesSeperatedByAreas[i].Count - 1));
                BreakWall(wallsAroundWaterTilesSeperatedByAreas[i][randomWall], ref waterEdgeTiles);
                if (CheckForIsolatedWaterTile(wallsAroundWaterTilesSeperatedByAreas[i][randomWall]))
                {
                    GameTile tile = FindDividerWall(wallsAroundWaterTilesSeperatedByAreas[i][randomWall]);
                    BreakWall(tile, ref waterEdgeTiles);
                    wallsAroundWaterTilesSeperatedByAreas[i].Remove(board.GetGrid()[tile.GetX()][tile.GetY()]);
                }
                wallsAroundWaterTilesSeperatedByAreas[i].RemoveAt(randomWall);
            }
            int originalSize = wallsAroundWaterTilesSeperatedByAreas[i].Count;
            //walk down all of the walls for a given area, attempting to break each wall, the attempt success rate is based off of our chanceForOtherWallBreaks value
            for (int c = 0; c < originalSize; c++)
            {
                float breakWallCheck = Random.Range(0.0f, 1.0f);
                if ((breakWallCheck <= chanceForOtherWallBreaks) && (wallsAroundWaterTilesSeperatedByAreas[i].Count > 0))
                {
                    int randomWall = (int)(Random.Range(0.0f, (float)wallsAroundWaterTilesSeperatedByAreas[i].Count - 1));
                    BreakWall(wallsAroundWaterTilesSeperatedByAreas[i][randomWall], ref waterEdgeTiles);
                    if (CheckForIsolatedWaterTile(wallsAroundWaterTilesSeperatedByAreas[i][randomWall]))
                    {
                        GameTile tile = FindDividerWall(wallsAroundWaterTilesSeperatedByAreas[i][randomWall]);
                        BreakWall(tile, ref waterEdgeTiles);
                        wallsAroundWaterTilesSeperatedByAreas[i].Remove(board.GetGrid()[tile.GetX()][tile.GetY()]);
                    }
                    wallsAroundWaterTilesSeperatedByAreas[i].RemoveAt(randomWall);
                }
            }
        }
        board.UnMarkAllTiles();
    }
Пример #11
0
    public override void Grass(GameTile currentTile)
    {
        if (!currentTile.isTilled)
        {
            //Change the sprite
            Manager.gameTileManager.TillGrass(currentTile.GetX(), currentTile.GetY());
            //Change the tile data
            currentTile.SetIsTilled(true);

            //Sound of tilling grass
        }
        else
        {
            // TODO: Do nothing and make thump sound
        }
    }
Пример #12
0
    public List <GameTile> GetAllTilesInRange(ref GameTile tile, int range)
    {
        //Essentially a modified FloodFill that is only allowed to run a set number of times. This will return all tiles within a certain movement range away for example when handed a
        //range of 1 and the player tile, it will return the tiles in the players cardnial directions (tiles that require one move to reach)
        //This will also mark all tiles within that given range.
        UnMarkAllTiles();
        List <GameTile> allMarkedCells  = new List <GameTile>();
        List <GameTile> validNeighbours = new List <GameTile>();

        allMarkedCells.Add(grid[tile.GetX()][tile.GetY()]);
        AddAllNeighbours(ref validNeighbours, tile);
        List <GameTile> nextValidNeighbours = new List <GameTile>();

        range--;
        if (range == 0)
        {
            return(validNeighbours);
        }
        else if (range < 0)
        {
            return(null);            //error
        }
        do
        {
            nextValidNeighbours.Clear();
            for (int x = 0; x < validNeighbours.Count; x++)
            {
                validNeighbours[x].SetIsMarked(true);
                allMarkedCells.Add(validNeighbours[x]);
                AddAllNeighbours(ref nextValidNeighbours, validNeighbours[x]);
            }
            validNeighbours = new List <GameTile>(nextValidNeighbours);
            range--;
        } while (nextValidNeighbours.Count != 0 && range != 0);
        return(allMarkedCells);
    }
Пример #13
0
    private List <GameTile> ReturnFloorTouchingWallsAroundTile(GameTile tile)
    {
        //retruns walls around a given tile, will only add tiles that aren't marked and are valid neighbour's of floor tiles
        List <GameTile> allWallsList   = new List <GameTile>();
        List <GameTile> validWallsList = new List <GameTile>();

        grid[tile.GetX()][tile.GetY()].SetIsMarked(true);
        if ((tile.GetY() + 1) < rows && grid[tile.GetX()][tile.GetY() + 1].IsWall() && !grid[tile.GetX()][tile.GetY() + 1].IsMarked())
        {
            grid[tile.GetX()][tile.GetY() + 1].SetIsMarked(true);
            allWallsList.Add(grid[tile.GetX()][tile.GetY() + 1]);
        }
        if ((tile.GetY() - 1) >= 0 && grid[tile.GetX()][tile.GetY() - 1].IsWall() && !grid[tile.GetX()][tile.GetY() - 1].IsMarked())
        {
            grid[tile.GetX()][tile.GetY() - 1].SetIsMarked(true);
            allWallsList.Add(grid[tile.GetX()][tile.GetY() - 1]);
        }
        if ((tile.GetX() + 1) < cols && grid[tile.GetX() + 1][tile.GetY()].IsWall() && !grid[tile.GetX() + 1][tile.GetY()].IsMarked())
        {
            grid[tile.GetX() + 1][tile.GetY()].SetIsMarked(true);
            allWallsList.Add(grid[tile.GetX() + 1][tile.GetY()]);
        }
        if ((tile.GetX() - 1) >= 0 && grid[tile.GetX() - 1][tile.GetY()].IsWall() && !grid[tile.GetX() - 1][tile.GetY()].IsMarked())
        {
            grid[tile.GetX() - 1][tile.GetY()].SetIsMarked(true);
            allWallsList.Add(grid[tile.GetX() - 1][tile.GetY()]);
        }
        if ((tile.GetX() + 1) < cols && tile.GetY() + 1 < rows && grid[tile.GetX() + 1][tile.GetY() + 1].IsWall() && !grid[tile.GetX() + 1][tile.GetY() + 1].IsMarked())
        {
            grid[tile.GetX() + 1][tile.GetY() + 1].SetIsMarked(true);
            allWallsList.Add(grid[tile.GetX() + 1][tile.GetY() + 1]);
        }
        if ((tile.GetX() - 1) >= 0 && tile.GetY() - 1 >= 0 && grid[tile.GetX() - 1][tile.GetY() - 1].IsWall() && !grid[tile.GetX() - 1][tile.GetY() - 1].IsMarked())
        {
            grid[tile.GetX() - 1][tile.GetY() - 1].SetIsMarked(true);
            allWallsList.Add(grid[tile.GetX() - 1][tile.GetY() - 1]);
        }
        if ((tile.GetX() + 1) < cols && tile.GetY() - 1 >= 0 && grid[tile.GetX() + 1][tile.GetY() - 1].IsWall() && !grid[tile.GetX() + 1][tile.GetY() - 1].IsMarked())
        {
            grid[tile.GetX() + 1][tile.GetY() - 1].SetIsMarked(true);
            allWallsList.Add(grid[tile.GetX() + 1][tile.GetY() - 1]);
        }
        if ((tile.GetX() - 1) >= 0 && tile.GetY() + 1 < rows && grid[tile.GetX() - 1][tile.GetY() + 1].IsWall() && !grid[tile.GetX() - 1][tile.GetY() + 1].IsMarked())
        {
            grid[tile.GetX() - 1][tile.GetY() + 1].SetIsMarked(true);
            allWallsList.Add(grid[tile.GetX() - 1][tile.GetY() + 1]);
        }
        for (int i = 0; i < allWallsList.Count; i++)
        {
            int count = 0;
            if ((allWallsList[i].GetY() + 1) < rows && grid[allWallsList[i].GetX()][allWallsList[i].GetY() + 1].OpenForPlacement())
            {
                count++;
            }
            if ((allWallsList[i].GetY() - 1) >= 0 && grid[allWallsList[i].GetX()][allWallsList[i].GetY() - 1].OpenForPlacement())
            {
                count++;
            }
            if ((allWallsList[i].GetX() + 1) < cols && grid[allWallsList[i].GetX() + 1][allWallsList[i].GetY()].OpenForPlacement())
            {
                count++;
            }
            if ((allWallsList[i].GetX() - 1) >= 0 && grid[allWallsList[i].GetX() - 1][allWallsList[i].GetY()].OpenForPlacement())
            {
                count++;
            }
            if (count > 0)
            {
                validWallsList.Add(allWallsList[i]);
            }
        }
        return(validWallsList);
    }
Пример #14
0
 private void AddDestroyedNeighbours(ref List <GameTile> list, GameTile tile)
 {
     //FloodFillDestroyedTiles helper function, returns a list containing the destroyed neighbour tiles
     //of a tile that should be included in the flood fill
     grid[tile.GetX()][tile.GetY()].SetIsMarked(true);
     if ((tile.GetY() + 1) < rows && grid[tile.GetX()][tile.GetY() + 1].IsDestroyed() && !grid[tile.GetX()][tile.GetY() + 1].IsMarked())
     {
         grid[tile.GetX()][tile.GetY() + 1].SetIsMarked(true);
         list.Add(grid[tile.GetX()][tile.GetY() + 1]);
     }
     if ((tile.GetY() - 1) >= 0 && grid[tile.GetX()][tile.GetY() - 1].IsDestroyed() && !grid[tile.GetX()][tile.GetY() - 1].IsMarked())
     {
         grid[tile.GetX()][tile.GetY() - 1].SetIsMarked(true);
         list.Add(grid[tile.GetX()][tile.GetY() - 1]);
     }
     if ((tile.GetX() + 1) < cols && grid[tile.GetX() + 1][tile.GetY()].IsDestroyed() && !grid[tile.GetX() + 1][tile.GetY()].IsMarked())
     {
         grid[tile.GetX() + 1][tile.GetY()].SetIsMarked(true);
         list.Add(grid[tile.GetX() + 1][tile.GetY()]);
     }
     if ((tile.GetX() - 1) >= 0 && grid[tile.GetX() - 1][tile.GetY()].IsDestroyed() && !grid[tile.GetX() - 1][tile.GetY()].IsMarked())
     {
         grid[tile.GetX() - 1][tile.GetY()].SetIsMarked(true);
         list.Add(grid[tile.GetX() - 1][tile.GetY()]);
     }
 }
Пример #15
0
 //private
 private void AddAllNeighbours(ref List <GameTile> list, GameTile tile)
 {
     //GetAllTilesInRange helper function, returns a list containing the neighbours surrounding a tile
     grid[tile.GetX()][tile.GetY()].SetIsMarked(true);
     if ((tile.GetY() + 1) < rows && !grid[tile.GetX()][tile.GetY() + 1].IsMarked())
     {
         list.Add(grid[tile.GetX()][tile.GetY() + 1]);
         grid[tile.GetX()][tile.GetY() + 1].SetIsMarked(true);
     }
     if ((tile.GetY() - 1) > 0 && !grid[tile.GetX()][tile.GetY() - 1].IsMarked())
     {
         list.Add(grid[tile.GetX()][tile.GetY() - 1]);
         grid[tile.GetX()][tile.GetY() - 1].SetIsMarked(true);
     }
     if ((tile.GetX() + 1) < cols && !grid[tile.GetX() + 1][tile.GetY()].IsMarked())
     {
         list.Add(grid[tile.GetX() + 1][tile.GetY()]);
         grid[tile.GetX() + 1][tile.GetY()].SetIsMarked(true);
     }
     if ((tile.GetX() - 1) > 0 && !grid[tile.GetX() - 1][tile.GetY()].IsMarked())
     {
         list.Add(grid[tile.GetX() - 1][tile.GetY()]);
         grid[tile.GetX() - 1][tile.GetY()].SetIsMarked(true);
     }
 }
Пример #16
0
 private void AddValidNeighbours(ref List <GameTile> list, GameTile tile)
 {
     //FloodFill helper function, returns a list containing the neighbours
     //of a tile that should be included in the flood fill
     grid[tile.GetX()][tile.GetY()].SetIsMarked(true);
     if ((tile.GetY() + 1) < rows && !grid[tile.GetX()][tile.GetY() + 1].IsWall() && !grid[tile.GetX()][tile.GetY() + 1].IsMarked())
     {
         list.Add(grid[tile.GetX()][tile.GetY() + 1]);
         grid[tile.GetX()][tile.GetY() + 1].SetIsMarked(true);
     }
     if ((tile.GetY() - 1) > 0 && !grid[tile.GetX()][tile.GetY() - 1].IsWall() && !grid[tile.GetX()][tile.GetY() - 1].IsMarked())
     {
         list.Add(grid[tile.GetX()][tile.GetY() - 1]);
         grid[tile.GetX()][tile.GetY() - 1].SetIsMarked(true);
     }
     if ((tile.GetX() + 1) < cols && !grid[tile.GetX() + 1][tile.GetY()].IsWall() && !grid[tile.GetX() + 1][tile.GetY()].IsMarked())
     {
         list.Add(grid[tile.GetX() + 1][tile.GetY()]);
         grid[tile.GetX() + 1][tile.GetY()].SetIsMarked(true);
     }
     if ((tile.GetX() - 1) > 0 && !grid[tile.GetX() - 1][tile.GetY()].IsWall() && !grid[tile.GetX() - 1][tile.GetY()].IsMarked())
     {
         list.Add(grid[tile.GetX() - 1][tile.GetY()]);
         grid[tile.GetX() - 1][tile.GetY()].SetIsMarked(true);
     }
 }
Пример #17
0
    private void CreateCircularVisibility(ref GameTile visionOriginTile, ref Board map, int maxView)
    {
        /*
         * Smooths out the diamond field of vision created by LimitVisibilityToMaxView() in order to give the entity a circular field of vision
         */

        //The X and Y point of the entities current tile
        int originXPoint = visionOriginTile.GetX();
        int originYPoint = visionOriginTile.GetY();

        //Each of these corresponds to a point (or edge if you prefer) of the diamond vision field
        int topPoint   = visionOriginTile.GetX() + maxView;
        int rightPoint = visionOriginTile.GetY() + maxView;
        int botPoint   = visionOriginTile.GetX() - maxView;
        int leftPoint  = visionOriginTile.GetY() - maxView;

        //These bools are true if the corresponding points (or edges if you prefer) of the diamond are actually on the map grid
        bool hasTopPoint    = topPoint < map.GetCols();
        bool hasBottomPoint = botPoint - maxView > 0;
        bool hasRightPoint  = rightPoint < map.GetRows();
        bool hasLeftPoint   = leftPoint - maxView > 0;

        //These bools are true if the corresponding points on the second to last top and bottom row of the diamond are actually on the map grid
        //For example these bools would correspond to points found on:
        //      *
        //    * * * <- this row
        //   * * * *
        //  * * * * *
        //   * * * *
        //    * * * <- and this row
        //      *
        bool hasTopLeft   = originYPoint + (maxView - 1) < map.GetRows() && originXPoint - 1 > 0;
        bool hasTopMiddle = originYPoint + (maxView - 1) < map.GetRows();
        bool hasTopRight  = originYPoint + (maxView - 1) < map.GetRows() && originXPoint + 1 < map.GetCols();
        bool hasBotLeft   = originYPoint - (maxView - 1) > 0 && originXPoint - 1 > 0;
        bool hasBotMid    = originYPoint - (maxView - 1) > 0;
        bool hasBotRight  = originYPoint - (maxView - 1) > 0 && originXPoint + 1 < map.GetCols();

        //remove the 4 extreme points (or edges) of the diamond
        if (hasTopPoint)
        {
            map.GetGrid()[topPoint][originYPoint].SetIsVisible(false);
        }
        if (hasBottomPoint)
        {
            map.GetGrid()[botPoint][originYPoint].SetIsVisible(false);
        }
        if (hasRightPoint)
        {
            map.GetGrid()[originXPoint][rightPoint].SetIsVisible(false);
        }
        if (hasLeftPoint)
        {
            map.GetGrid()[originXPoint][leftPoint].SetIsVisible(false);
        }

        //smooth out the diamond effect even more by removing the second to last top and bottom row
        if (hasTopMiddle)
        {
            map.GetGrid()[originXPoint][originYPoint + (maxView - 1)].SetIsVisible(false);
        }
        if (hasTopLeft)
        {
            map.GetGrid()[originXPoint - 1][originYPoint + (maxView - 1)].SetIsVisible(false);
        }
        if (hasTopRight)
        {
            map.GetGrid()[originXPoint + 1][originYPoint + (maxView - 1)].SetIsVisible(false);
        }
        if (hasBotMid)
        {
            map.GetGrid()[originXPoint][originYPoint - (maxView - 1)].SetIsVisible(false);
        }
        if (hasBotLeft)
        {
            map.GetGrid()[originXPoint - 1][originYPoint - (maxView - 1)].SetIsVisible(false);
        }
        if (hasBotRight)
        {
            map.GetGrid()[originXPoint + 1][originYPoint - (maxView - 1)].SetIsVisible(false);
        }
    }
Пример #18
0
 public GameTile GetTile(GameTile tile)
 {
     return(grid[tile.GetX()][tile.GetY()]);
 }