Beispiel #1
0
    public void Init()
    {
        GameObject go = Instantiate(Astar);

        go.transform.parent = this.transform;
        AStarGrid           = go.GetComponent <AStarGrid>();
    }
 public void SetGrid(AStarGrid <PathNode> grid)
 {
     m_grid           = grid;
     m_grid.showDebug = true;
     m_grid.ShowDebug();
     m_grid.OnGridObjectChanged += Grid_OnGridValueChanged;
 }
 // set up the singleton and grid to be used for the generation
 void Awake()
 {
     Instance     = this;
     nodeDiamater = NodeRadius * 2;
     gridSizeX    = Mathf.RoundToInt(GridWorldSize.x / nodeDiamater);
     gridSizeY    = Mathf.RoundToInt(GridWorldSize.y / nodeDiamater);
 }
    private void CreateHallways()
    {
        List <Rect> roomRects = GetRoomRects();
        AStarGrid   grid      = new AStarGrid(roomRects, positionMeta);

        debugData.Grid = grid.Grid;
        HallwayMeshGenerator meshGenerator = new HallwayMeshGenerator(grid, hallwayTiling);

        foreach (HallwayMeta hw in hallwayMeta)
        {
            HallwayAStar  routing = new HallwayAStar(hw.StartDoor, hw.EndDoor, grid);
            List <Square> path    = routing.BuildPath();
            debugData.AddPath(path);
            AddLevelMetadataPath(path);
            meshGenerator.AddPath(path);
        }
        Mesh       mesh     = meshGenerator.GenerateMesh(true);
        GameObject hallways = new GameObject("Hallways");

        hallways.isStatic = setIsStatic;
        hallways.tag      = "ChunkInstance";
        hallways.layer    = LayerMask.NameToLayer("LevelGeometry");
        MeshFilter meshFilter = hallways.AddComponent <MeshFilter> ();

        meshFilter.sharedMesh = mesh;
        MeshRenderer meshRenderer = hallways.AddComponent <MeshRenderer> ();

        meshRenderer.sharedMaterials = hallwayMaterials;
        hallways.AddComponent <MeshCollider> ();
        FillHallways(grid, hallways);
    }
Beispiel #5
0
 public PathFinding2D(int width, int height)
 {
     m_instance = this;
     m_grid     = new AStarGrid <PathNode>(width, height, 1f, Vector3.zero,
                                           (AStarGrid <PathNode> grid, int x, int y) => new PathNode(grid, x, y));
     m_grid.showDebug = true;
 }
Beispiel #6
0
    void SetupLevel()
    {
        bool laddarSpawned = false; // bool will control if a ladder has already been spawned
        bool playerSpawned = false;

        cellularAutomata = GameObject.Find("MeshGenerator").GetComponent <CellularAutomata>();                                      // finds the cellular automata script in the scene
        aStarGrid        = GameObject.Find("A*").GetComponent <AStarGrid>();                                                        // finds the AStarGrid

        for (int x = 0; x < cellularAutomata.gridWidth; x++)                                                                        // loops through the grids x
        {
            for (int y = 0; y < cellularAutomata.gridHeight; y++)                                                                   // loops through the grids y
            {
                if (x != 0 && x != cellularAutomata.grid.GetLength(0) - 2 && y != 0 && y != cellularAutomata.grid.GetLength(1) - 2) // if we are within the bounds the array
                {
                    if (cellularAutomata.GetAmountOfNeighbours(x, y, 2) == 0 && aStarGrid.grid[x, y].walkable)                      // using the overloaded methoed of get neighbours to have a larger search area to stop things from spawning on walls and checks if the tile is walkable in the AStarGrid
                    {                                                                                                               //
                        if (GetRandomNumber() <= probabilityToSpawnEnemy)                                                           // generate a random number and if it is within probabilty
                        {
                            Instantiate(enemy, aStarGrid.grid[x, y].worldPosition, Quaternion.identity);                            // spawn an enemy
                        }
                        else if (!laddarSpawned)
                        {
                            laddarSpawned = Instantiate(ladder, aStarGrid.grid[x, y].worldPosition, Quaternion.identity); // if no spawn a ladder and set the ladderspawned varaible to true
                        }
                        else if (!playerSpawned && (x == (cellularAutomata.grid.GetLength(0) / 2) || y == (cellularAutomata.grid.GetLength(1) / 2)))
                        {
                            playerSpawned = Instantiate(player, aStarGrid.grid[x, y].worldPosition, Quaternion.identity);
                        }
                    }
                }
            }
        }
    }
Beispiel #7
0
    // this finds the NEAREST NODES to the start and goal location
    // this will likely need to be modified later to make a more robust node-finder that
    // will not assign units 1-st node as a node on another height level (rounding error)
    private AStarNode[] findNearestNodes(AStarGrid grid, Vector2 s, Vector2 g)
    {
        float nearestS = 9999f, nearestG = 9999f;
        float sMag, gMag;

        AStarNode startNode = new AStarNode(0, 0, 1);
        AStarNode goalNode  = new AStarNode(0, 0, 1);

        foreach (AStarNode an in grid.nodes)
        {
            sMag = (s - new Vector2(an.x, an.y)).sqrMagnitude;
            gMag = (g - new Vector2(an.x, an.y)).sqrMagnitude;

            if (sMag < nearestS)
            {
                startNode = an;
                nearestS  = sMag;
            }
            if (gMag < nearestG)
            {
                goalNode = an;
                nearestG = gMag;
            }
        }

        return(new AStarNode[] { startNode, goalNode });
    }
Beispiel #8
0
    void AddAroundGrid(AStarGrid curgrid, AStarGrid endgrid)
    {
        for (int i = 0; i < aroundpos.GetLength(0); i++)
        {
            var x = curgrid.X + aroundpos[i, 0];
            var y = curgrid.Y + aroundpos[i, 1];

            if (IsInList(x, y, maps) && !IsInList(x, y, closelist) && !IsInList(x, y, barrierlist))
            {
                if (IsInList(x, y, openlist))
                {
                    var grid = GetGridWithList(x, y, openlist);
                    grid.data.G = aroundpos[i, 2];
                }
                else
                {
                    int gvalue = aroundpos[i, 2];
                    int hvalue = GetHValue(x, y, endgrid);
                    var grid   = new AStarGrid(x, y, gvalue, hvalue);
                    if (CanMoveGrid(grid))
                    {
                        grid.parent = curgrid;
                        openlist.Add(grid);
                    }
                }
            }
        }
    }
Beispiel #9
0
    public AStarGrid CreatStart(int x, int y)
    {
        AStarGrid grid = new AStarGrid(x, y, 0, 0);

        openlist.Add(grid);
        return(grid);
    }
Beispiel #10
0
    private PathNode m_previousPathNode;    // Node came from

    public PathNode(AStarGrid <PathNode> grid, int x, int y)
    {
        m_grid     = grid;
        m_x        = x;
        m_y        = y;
        isWalkable = true;
    }
Beispiel #11
0
    public AStarSearch(AStarGrid grid, Vector2 startLoc, Vector2 goalLoc)
    {
        vStart = startLoc;
        vGoal  = goalLoc;
        aGrid  = grid;

        isDone = false;
    }
Beispiel #12
0
    int GetHValue(int x, int y, AStarGrid endgrid)
    {
        int xcount = Mathf.Abs(endgrid.X - x);
        int ycount = Mathf.Abs(endgrid.Y - y);
        int Hvalue = (xcount + ycount) * 10;

        return(Hvalue);
    }
Beispiel #13
0
    // Start is called before the first frame update
    #endregion

    void Start()
    {
        vie        = 3;
        pathfinder = GameObject.Find("PathFindAstar").GetComponent <AStarPathfinding>();
        anim       = this.GetComponent <Animator>();
        astargrid  = GameObject.Find("GridAStar").GetComponent <AStarGrid>();
        Player     = GameObject.Find("Heros").transform;
        startTime  = Time.time;
    }
Beispiel #14
0
        /// <summary>
        /// Called by unity.
        /// </summary>
        public void Start()
        {
            // This example assumes that there is a valid AStar grid component in the scene.
            // The included TileManager script can be used in this case.
            AStarGrid grid = AStarGridManager.DefaultGrid;

            // Issue a pathfinding request
            grid.findPath(new Index(0, 0), new Index(1, 1), onPathFound);
        }
Beispiel #15
0
 private IEnumerator searchForGrid()
 {
     while (grid == null)
     {
         grid = FindObjectOfType <AStarGrid>();
         yield return(null);
     }
     StartCoroutine(mainLoop());
 }
        // Methods
        public override void OnEnable()
        {
            grid = target as AStarGrid;

            // Construct the UI elements
            createUI();

            base.OnEnable();
        }
Beispiel #17
0
 //  CHANGE the discomfort field - initial functionality will focus on obstructors, like buildings
 public void modifyDiscomfortField(int globalX, int globalY, float[,] gm)
 {
     // overwrite our Map_Data_Package
     theCCDynamicFieldManager.theMapData.overwriteDiscomfortData(globalX, globalY, gm);
     // since the absolute discomfort grid g denotes unpassable regions
     // we now have to regenerate our AStarGrid
     theAStarGrid = new AStarGrid(theCCDynamicFieldManager.theMapData.getCompleteHeightMap(),
                                  theCCDynamicFieldManager.theMapData.getCompleteDiscomfortMap(), nodeDimensions);
 }
Beispiel #18
0
    private AStarGrid grid;                              //The grid used by the AStar algorithm, used to retrieve the segment positions

    public HallwayHelper(AStarGrid grid, LevelGeneratorPreset preset, GameObject hallwayObject)
    {
        this.preset            = preset;
        this.chunkInstantiator = ChunkInstantiator.Instance;
        //this.preset = preset;
        this.grid             = grid;
        this.hallwayObject    = hallwayObject;
        this.hallwayTemplates = new List <HallwayTemplateMeta> ();
        BuildMetadata();
    }
Beispiel #19
0
 public HallwayAStar(DoorDefinition start, DoorDefinition end, AStarGrid grid)
 {
     this.openList   = new List <Square> ();
     this.closedList = new List <Square> ();
     this.finalPath  = new List <Square> ();
     this.startDoor  = start;
     Square.Grid     = grid;
     this.endDoor    = end;
     this.grid       = grid;
 }
Beispiel #20
0
    public AStarSearch(AStarNode start, AStarNode end, AStarGrid grid, eHeuristicType heuristic = eHeuristicType.ManHattan)
    {
        mStartNode = start;
        mEndNode   = end;
        mNodeGrid  = grid;
        mHeuristic = heuristic;

        mOpenSet   = new List <AStarNode>();
        mClosedSet = new List <AStarNode>();
    }
 private MatchResult IsMatchingMaskAt(AStarGrid grid, GridPosition pos)
 {
     foreach (List <MaskSegment> rotatedMask in relativePositions)
     {
         if (IsMatchingMaskAt(grid, rotatedMask, pos))
         {
             return(new MatchResult(true, new int[] { pos.i, pos.j }, rotatedMask [0].RotatedBy));
         }
     }
     return(new MatchResult(false, new int[] { 0, 0 }, 0));
 }
Beispiel #22
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        AStarGrid navMesh = (AStarGrid)target;

        if (GUILayout.Button("Bake navigation mesh"))
        {
            navMesh.Bake();
        }
    }
    //Marks the positions of the mask relative to the given position in the grid
    //This will hinder other masks from matching at this position, thus instantiating several hallways
    //at the same position
    public void MarkPositionAsUsed(AStarGrid grid, MatchResult match)
    {
        List <MaskSegment> _mask = FindMaskByRotation(match.Rotation);

        foreach (MaskSegment relative in _mask)
        {
            int x = relative.Offset [0] + match.Position [0];
            int y = relative.Offset [1] + match.Position [1];
            grid.Grid [x, y].UsedByHallwayTemplate = true;
        }
    }
Beispiel #24
0
 bool CanMoveGrid(AStarGrid grid)
 {
     if (grid.data.G == 14 && barrierlist.Count > 0)
     {
         var minbarrier = GetHMinBarrier(grid.X, grid.Y);
         if ((minbarrier.X + 1 == grid.X || minbarrier.X - 1 == grid.X) || (minbarrier.Y + 1 == grid.Y || minbarrier.Y - 1 == grid.Y))
         {
             return(false);
         }
     }
     return(true);
 }
Beispiel #25
0
 /// <summary>
 /// 设置地图
 /// </summary>
 void InitMap()
 {
     maps.Clear();
     for (int i = 0; i < 8; i++)
     {
         for (int j = 0; j < 6; j++)
         {
             var AStarGrid = new AStarGrid(i, j);
             maps.Add(AStarGrid);
         }
     }
 }
 public static MaskState GetState(int x, int y, AStarGrid _grid)
 {
     if (_grid.Grid [x, y].IsPartOfPath)
     {
         return(MaskState.FILL);
     }
     else if (_grid.Grid [x, y].ShouldBeEmpty)
     {
         return(MaskState.EMPTY);
     }
     return(MaskState.UNUSED);
 }
Beispiel #27
0
    private void Awake()
    {
        if (g == null)
        {
            g = this;
        }

        nodeDiameter = nodeRadius * 2;
        gridSize.x   = Mathf.RoundToInt(gridDimensions.x / nodeDiameter);
        gridSize.y   = Mathf.RoundToInt(gridDimensions.y / nodeDiameter);

        GenerateGrid();
    }
 void Start()
 {
     //Get the referende to the grid attached to the gridObject.
     if (gridObject == null)
     {
         Debug.LogError("Pathfinding grid object not initialised!");
         return;
     }
     grid = gridObject.GetComponent <AStarGrid> ();
     previousTargetPosition  = transform.position;
     timeLeftUntilPathUpdate = 0;
     timeKeeper = Time.time;
 }
    public void OnEnable()
    {
        grid    = new AStarGrid(extends);
        squares = new List <Square> ();
        //Debug.Log ("onenable");

        if (mapping == null)
        {
            Debug.Log("init mapping");
            InitMapping();
        }
        InitGridFromMapping();
        DrawGeometry();
    }
 private bool IsMatchingMaskAt(AStarGrid grid, List <MaskSegment> _mask, GridPosition pos)
 {
     foreach (MaskSegment segment in _mask)
     {
         int[]        absolutePosition = new int[] { segment.Offset [0] + pos.i, segment.Offset [1] + pos.j };
         GridPosition testPosition     = grid.Grid [absolutePosition [0], absolutePosition [1]];
         bool         matchesSegment   = IsMatchingSegment(segment.State, testPosition) && !testPosition.UsedByHallwayTemplate;
         if (!matchesSegment)
         {
             return(false);
         }
     }
     return(true);
 }
 void Awake() {
     grid = GetComponent<AStarGrid>();
     pathManager = GetComponent<PathManager>();
 }