private void CheckNeighbouringSections() //checking neighbours for BFS algorithm
    {
        if (!pathFindingInProgress)
        {
            return;
        }                                       //stop checking neighbours when path finding is done

        //for every neighbouring section
        foreach (Vector2Int section in neighbours)
        {
            Vector2Int neighbouringPosition = checkingFrom.GetPositionOfSectionInGrid() + section; //get the neighbour (section)

            if (maze.ContainsKey(neighbouringPosition))                                            //check if the neighbour exists in the grid then queue the new neighbour
            {
                GridSection neighbouringSection = maze[neighbouringPosition];                      //assign the neighbouring section

                if (neighbouringSection.alreadyChecked || queue.Contains(neighbouringSection))     //skip section if it has already been checked or is already in the queue (requires checking both for edge and end cases)
                {
                    //skip
                }
                else //otherwise
                {
                    queue.Enqueue(neighbouringSection); //queue the neighbour
                    neighbouringSection.checkedFrom = checkingFrom; //keep track of section the neighbour was checked from
                }
            }
        }
    }
Example #2
0
    void Update()
    {
        GroundGrid gridScript = floorGrid.GetComponent <GroundGrid>();

        if (gridScript.grid != null)
        {
            currentOver = SectionFromWorldPoint(transform.position, gridScript.grid, gridScript.gridSize);
            if (previousOver != currentOver)
            {
                if (previousOver != null)
                {
                    previousOver.overHead = false;
                }
                previousOver = currentOver;
            }
            currentOver.overHead = true;



            if (transform.position.y - currentOver.position.y < 0.5 && currentOver.occupied == false)
            {
                LockPosition(currentOver);
            }
            else if (transform.position.y - currentOver.position.y < 1 && currentOver.occupied == true)
            {
                buildingCollide(currentOver);
            }
        }
    }
Example #3
0
 /// <summary>
 /// Gets the HTML of the specified <paramref name="section" /> or falls back to <paramref name="fallbackPartial"/> if no section view is found.
 /// </summary>
 /// <param name="helper">The instance of <see cref="T:System.Web.Mvc.HtmlHelper" /> used for rendering the section.</param>
 /// <param name="section">The section to be rendered.</param>
 /// <param name="fallbackPartial">The fallback partial view to be used if no section partial is found by <see cref="GridSection.Name"/> for the rendering.</param>
 /// <returns>An instance of <see cref="T:System.Web.HtmlString" />.</returns>
 public static HtmlString RenderGridSectionOrFallback(this HtmlHelper helper, GridSection section, string fallbackPartial)
 {
     if (helper == null || section == null)
     {
         return(new HtmlString(""));
     }
     return(RenderGridSectionOrFallback(helper, section, section.Name, fallbackPartial));
 }
Example #4
0
 /// <summary>
 /// Gets the HTML of the specified <paramref name="section"/>.
 /// </summary>
 /// <param name="helper">The instance of <see cref="HtmlHelper"/> used for rendering the row.</param>
 /// <param name="section">The section to be rendered.</param>
 /// <param name="partial">The partial view to be used for the rendering.</param>
 /// <returns>An instance of <see cref="HtmlString"/>.</returns>
 public static HtmlString RenderGridSection(this HtmlHelper helper, GridSection section, string partial)
 {
     if (helper == null || section == null)
     {
         return(new HtmlString(""));
     }
     return(section.GetHtml(helper, partial));
 }
Example #5
0
    private int CalculateDistance(GridSection start, GridSection end)
    {
        int disX = Mathf.Abs((int)start.pos.x - (int)end.pos.x);
        int disY = Mathf.Abs((int)start.pos.y - (int)end.pos.y);
        int dis  = Mathf.Abs(disX - disY);

        return(DIAGONAL_COST * Mathf.Min(disX, disY) + STRAIGHT_COST * dis);
    }
Example #6
0
            public IGridSectionSyntax AddSection(int layoutColumnCount)
            {
                var gridSection = new GridSection(layoutColumnCount);

                _gridSections.Add(gridSection);

                return(new GridSectionSyntax(this, gridSection));
            }
    private void FindShortestPathToExit()                //Main BFS algorithm implementation (fragmented into smaller functions for easier editing and undertsanding)
    {
        queue.Enqueue(mazeStart);                        //start by adding the starting section to the queue

        while (queue.Count > 0 && pathFindingInProgress) //while the queue is not empty and finding algorithm has not been halted
        {
            checkingFrom = queue.Dequeue();              //dequeue the section the current one was reached/checked from
            CheckIfGoalFound();                          //check if the goal was reached
            CheckNeighbouringSections();                 //explore the neighbouring sections
            checkingFrom.alreadyChecked = true;          //mark the current section as checked/visisted
        }
    }
Example #8
0
    void LockPosition(GridSection sectionToLock)
    {
        sectionToLock.overHead = false;
        sectionToLock.occupied = true;
        GameObject building = Instantiate(groundBuilding, sectionToLock.position, transform.rotation);

        sectionToLock.currentOccupant = building;
        StationaryBlockBehaviour buildingScript = building.GetComponent <StationaryBlockBehaviour>();

        buildingScript.CurrentSection = sectionToLock;
        Destroy(gameObject);
    }
Example #9
0
            private IJsonArrayBuilder GetGridSectionRows(GridSection section, IJsonArraySyntax arraySyntax)
            {
                var builder = new FluentJsonBuilder();

                return(arraySyntax.WithItems(section.Rows.Select(gridRow => builder.CreateNew()
                                                                 .WithProperty("id", GetComponentId())
                                                                 .WithProperty("name", gridRow.LayoutName)
                                                                 .WithProperty("hasConfig", gridRow.HasConfig)
                                                                 .WithObject("config", GetDictionaryObject(gridRow.Config))
                                                                 .WithObject("styles", GetDictionaryObject(gridRow.Styles))
                                                                 .WithArray("areas", stx => GetGridRowColumns(gridRow, stx))
                                                                 .Build()).ToArray()));
            }
    // creates grid of gridsections based on gridsize
    GridSection[,] CreateGrid(int gridSize, Vector3 position)
    {
        GridSection[,] grid = new GridSection[gridSize, gridSize];
        Vector3 bottomLeft = new Vector3(position.x - gridSize / 2f, position.y - gridSize / 2f);

        for (int x = 0; x < gridSize; x++)
        {
            for (int y = 0; y < gridSize; y++)
            {
                grid[x, y] = new GridSection(new Vector3(bottomLeft.x + x + 0.5f, 0, bottomLeft.y + y + 0.5f));
            }
        }
        return(grid);
    }
Example #11
0
    private GridSection GetLowestFCostGridSection(List <GridSection> GridSectionList)
    {
        GridSection lowestCostGridSection = GridSectionList[0];

        for (int i = 1; i < GridSectionList.Count; i++)
        {
            if (GridSectionList[i].fCost < lowestCostGridSection.fCost)
            {
                lowestCostGridSection = GridSectionList[i];
            }
        }

        return(lowestCostGridSection);
    }
Example #12
0
    private List <GridSection> CalculatePath(GridSection endGridSection)
    {
        List <GridSection> path = new List <GridSection>();

        path.Add(endGridSection);
        GridSection currentGridSection = endGridSection;

        while (currentGridSection.lastSection != null)
        {
            path.Add(currentGridSection.lastSection);
            currentGridSection = currentGridSection.lastSection;
        }

        path.Reverse();
        return(path);
    }
Example #13
0
        /// <summary>
        /// Gets the HTML of the specified <paramref name="section" /> or falls back to <paramref name="fallbackPartial"/> if <paramref name="partial"/> isn't found.
        /// </summary>
        /// <param name="helper">The instance of <see cref="T:System.Web.Mvc.HtmlHelper" /> used for rendering the section.</param>
        /// <param name="section">The section to be rendered.</param>
        /// <param name="partial">The partial view to be used for the rendering.</param>
        /// <param name="fallbackPartial">The fallback partial view to be used if <paramref name="partial"/> isn't found.</param>
        /// <returns>An instance of <see cref="T:System.Web.HtmlString" />.</returns>
        public static HtmlString RenderGridSectionOrFallback(this HtmlHelper helper, GridSection section, string partial, string fallbackPartial)
        {
            if (helper == null || section == null)
            {
                return(new HtmlString(""));
            }

            // Determine the partial view
            if (GridUtils.IsValidPartialName(partial))
            {
                partial = "TypedGrid/Sections/" + partial;
            }

            // Do we have a partial view or should we use the fallback?
            return(helper.ViewExists(partial) ? helper.RenderGridSection(section, partial) : helper.RenderGridSection(section, fallbackPartial));
        }
    private void ProcessFoundPath()                              //with the path obtained from the main part of the BFS algorithm, process the list for the follower to use
    {
        path.Add(mazeExit);                                      //add the maze exit as it is not in the path due to the way the BFS is implemented here (same for the start later)

        GridSection sectionReachedFrom = mazeExit.checkedFrom;   //start the backtracking process

        while (sectionReachedFrom != mazeStart)                  //while the start has not been reached during the backtracking process
        {
            path.Add(sectionReachedFrom);                        //add successive waypoints
            sectionReachedFrom = sectionReachedFrom.checkedFrom; //moving backwards through the path from start to finish to record and construct the path in a list
        }

        path.Add(mazeStart); //add the start position as it is not recorded the way the start of the algorithm is implemnted

        path.Reverse();      //reverse the list from the backtracking process to get a list of sections from the maze start to the maze exit (in correct order)
    }
Example #15
0
 void buildingCollide(GridSection spotToCollide)
 {
     spotToCollide.occupied = false;
     spotToCollide.overHead = false;
     if (spotToCollide.currentOccupant.GetComponent <StationaryBlockBehaviour>().Good == true)
     {
         scoreManager.Score -= 1;
         scoreManager.Lives -= 1;
     }
     Instantiate(explosionEffect, transform.position, transform.rotation);
     Destroy(spotToCollide.currentOccupant);
     if (Collided != null)
     {
         Collided();
     }
     Destroy(gameObject);
 }
Example #16
0
    public void CreateGrid()                                                                                                  //---------------------------------------Creates terrain----------------------------------//
    {
        for (int x = 0; x < GridSize.x; x++)                                                                                  //------loops in x------//
        {
            for (int y = 0; y < GridSize.y; y++)                                                                              //-----loops in y------//
            {
                grid[x, y]         = new GridSection(new Vector2(x, y));                                                      //------------ creates the new girdSection ---------------//
                grid[x, y].segment = Instantiate(Segment, new Vector3(x * gridScale, 0, y * gridScale), Quaternion.identity); //----- spawn prefab -------//
                grid[x, y].segment.transform.localScale = new Vector3(gridScale, 1, gridScale);

                if (x == 0 || y == 0 || x == GridSize.x - 1 || y == GridSize.y - 1)                  //----if on border
                {
                    grid[x, y].segment.transform.localScale = new Vector3(gridScale, 25, gridScale); //------------------------- create wall border -----------//
                    grid[x, y].wall = true;
                }
                else
                {
                    float pixelValue = perlinNoise.GetPixel(x, y).r * 10;   //----------get pixel from perlin noise --------//

                    if (pixelValue == 0)
                    {
                        pixelValue = 1;
                    }

                    if (pixelValue <= noiseCutoff)  //---if above 5 = wall if below = floor ----//
                    {
                        pixelValue = 1;
                    }
                    else
                    {
                        grid[x, y].wall = true;
                    }


                    grid[x, y].segment.transform.localScale = new Vector3(gridScale, pixelValue, gridScale); //------scales on y axis by pixels value to create walls -----//

                    if (grid[x, y].wall)
                    {
                        grid[x, y].segment.transform.GetChild(0).GetComponent <MeshRenderer>().material = wallMat;  //-----color of wall -------//
                    }
                }
            }
        }
    }
Example #17
0
    private void GridSetup()
    {
        for (int i = 0; i < GridSize; i++)
        {
            for (int j = 0; j < GridSize; j++)
            {
                //getting the modulus if the index to use for the grid colour
                int k = (i + j) % 2;

                GameObject G = Resources.Load("GridSect") as GameObject;

                G.name = ("Grid" + i + "//" + j);

                GridSection TempAccess = G.GetComponent <GridSection>();
                TempAccess.TileSection = k;

                GameObject.Instantiate(G, new Vector3(i * roomSize, 0, j * roomSize), Quaternion.identity);
            }
        }
    }
Example #18
0
    private List <GridSection> GetNeighbourGridSections(GridSection currentGridSection)
    {
        List <GridSection> neighbourList = new List <GridSection>();

        if ((int)currentGridSection.pos.x - 1 > 0)                                                     //Gets X Minus Neigbours
        {
            neighbourList.Add(grid[(int)currentGridSection.pos.x - 1, (int)currentGridSection.pos.y]); //Left

            if ((int)currentGridSection.pos.y - 1 > 0)
            {
                neighbourList.Add(grid[(int)currentGridSection.pos.x - 1, (int)currentGridSection.pos.y - 1]); //Left Down
            }
            if ((int)currentGridSection.pos.y + 1 > 0)
            {
                neighbourList.Add(grid[(int)currentGridSection.pos.x - 1, (int)currentGridSection.pos.y + 1]); //Left Up
            }
        }
        if ((int)currentGridSection.pos.x + 1 < grid.GetLength(0))                                     //Gets X Plus Neighbours
        {
            neighbourList.Add(grid[(int)currentGridSection.pos.x + 1, (int)currentGridSection.pos.y]); //Right

            if ((int)currentGridSection.pos.y - 1 > 0)
            {
                neighbourList.Add(grid[(int)currentGridSection.pos.x + 1, (int)currentGridSection.pos.y - 1]); //Right Down
            }
            if ((int)currentGridSection.pos.y + 1 > 0)
            {
                neighbourList.Add(grid[(int)currentGridSection.pos.x + 1, (int)currentGridSection.pos.y + 1]); //Right Up
            }
        }

        if ((int)currentGridSection.pos.y - 1 > 0)                                                     //Gets Y Above
        {
            neighbourList.Add(grid[(int)currentGridSection.pos.x, (int)currentGridSection.pos.y - 1]); //Down
        }
        if ((int)currentGridSection.pos.y + 1 < grid.GetLength(1))                                     //Gets Y Below
        {
            neighbourList.Add(grid[(int)currentGridSection.pos.x, (int)currentGridSection.pos.y + 1]); //UP
        }
        return(neighbourList);
    }
Example #19
0
    // Start is called before the first frame update
    public void Start()
    {
        outerGridSizeX = innerGridSizeX + (innerGridDistance * 2);
        outerGridSizeY = innerGridSizeY + (innerGridDistance * 2);
        gridMap        = new GridSection[outerGridSizeX, outerGridSizeY];
        for (int x = 0; x < outerGridSizeX; x++)
        {
            for (int y = 0; y < outerGridSizeY; y++)
            {
                gridMap[x, y] = new GridSection();
            }
        }

        previousHall = new int[3] {
            innerGridDistance, innerGridDistance - 1, 0
        };
        createHall(innerGridDistance, innerGridDistance, 0, 0);

        townsfolkControllerScript.SummonTownsfolk();
        potionControllerScript.Start();
        potionControllerScript.SpawnPotions();
    }
 void DrawIndicators()
 {
     for (int x = 0; x < gridSize; x++)
     {
         for (int y = 0; y < gridSize; y++)
         {
             GridSection currentSquare = grid[x, y];
             if (currentSquare.overHead == true && currentSquare.occupied != true && currentSquare.currentIndicator == null)
             {
                 currentSquare.currentIndicator = Instantiate(markerSquare, grid[x, y].position + (Vector3.up * 1.2f), Quaternion.Euler(90, 0, 0));
             }
             if (currentSquare.overHead == true && currentSquare.occupied == true && currentSquare.currentIndicator == null)
             {
                 currentSquare.currentIndicator = Instantiate(floorOccupiedMarker, grid[x, y].position + (Vector3.up * 1.2f), Quaternion.Euler(90, 0, 0));
             }
             if (currentSquare.overHead == false)
             {
                 Destroy(currentSquare.currentIndicator);
             }
         }
     }
 }
 /// <inheritdoc />
 public virtual GridRow CreateGridRow(JObject json, GridSection section)
 {
     return(new(json, section, this));
 }
Example #22
0
    public List <GridSection> FindPath(int startX, int startY, int endX, int endY)
    {
        GridSection startGridSection = grid[startX, startY];
        GridSection endGridSection   = grid[endX, endY];

        openList = new List <GridSection> {
            startGridSection
        };
        closedList = new List <GridSection>();

        for (int x = 0; x < grid.GetLength(0); x++)
        {
            for (int y = 0; y < grid.GetLength(1); y++)
            {
                GridSection GridSection = grid[x, y];
                GridSection.gCost = int.MaxValue;
                GridSection.CalculateFCost();
                GridSection.lastSection = null;
            }
        }

        startGridSection.gCost = 0;
        startGridSection.hCost = CalculateDistance(startGridSection, endGridSection);
        startGridSection.CalculateFCost();

        //-------------------------------------------------------------------//

        while (openList.Count > 0)
        {
            GridSection currentGridSection = GetLowestFCostGridSection(openList);

            if (currentGridSection == endGridSection)  //
            {
                return(CalculatePath(endGridSection)); //
            }

            openList.Remove(currentGridSection);
            closedList.Add(currentGridSection);

            foreach (GridSection neighbourGridSection in GetNeighbourGridSections(currentGridSection)) //
            {
                if (closedList.Contains(neighbourGridSection))
                {
                    continue;
                }

                if (neighbourGridSection.wall)
                {
                    closedList.Add(neighbourGridSection);
                    continue;
                }

                int tempGCost = currentGridSection.gCost + CalculateDistance(currentGridSection, neighbourGridSection); //

                if (tempGCost < neighbourGridSection.gCost)                                                             //
                {
                    neighbourGridSection.lastSection = currentGridSection;                                              //
                    neighbourGridSection.gCost       = tempGCost;                                                       //
                    neighbourGridSection.hCost       = CalculateDistance(neighbourGridSection, endGridSection);         //
                    neighbourGridSection.CalculateFCost();                                                              //

                    if (!openList.Contains(neighbourGridSection))                                                       //
                    {
                        openList.Add(neighbourGridSection);
                    }
                }
            }
        }
        //
        return(null);
    }
Example #23
0
 public GridSectionSyntax(IGridEditorLayoutSyntax gridEditorLayoutSyntax, GridSection section)
 {
     _gridEditorLayoutSyntax = gridEditorLayoutSyntax;
     _gridSection            = section;
 }
Example #24
0
 /// <summary>
 /// Initializes a new instance based on the specified <paramref name="section"/>.
 /// </summary>
 /// <param name="section">The section the new instance should be based on.</param>
 public SpaGridSection(GridSection section)
 {
     Grid = section.Grid;
     Rows = new List <SpaGridRow>();
 }
Example #25
0
 /// <summary>
 /// Initializes a new instance based on the specified <paramref name="section"/>.
 /// </summary>
 /// <param name="section">The section the new instance should be based on.</param>
 /// <param name="rows">The rows that should be added to the section.</param>
 public SpaGridSection(GridSection section, IEnumerable <SpaGridRow> rows)
 {
     Grid = section.Grid;
     Rows = rows.ToList();
 }
Example #26
0
    GridSection gridSection;                   //establish the section of the grid this script belongs to

    private void Awake()
    {
        gridSection = GetComponent <GridSection>(); //get the grid section component on this object before executing anything elsd
    }