Exemplo n.º 1
0
    private List <GridCellProperties> getNeighbors(GridCellProperties node)
    {
        List <GridCellProperties> neighbors = new List <GridCellProperties>();

        //checks and adds top neighbor
        if (node.GridX >= 0 && node.GridX < xWidth && node.GridZ + 1 >= 0 && node.GridZ + 1 < zWidth)
        {
            neighbors.Add(childrenByPosition[node.GridX, node.GridZ + 1]);
        }

        //checks and adds bottom neighbor
        if (node.GridX >= 0 && node.GridX < xWidth && node.GridZ - 1 >= 0 && node.GridZ - 1 < zWidth)
        {
            neighbors.Add(childrenByPosition[node.GridX, node.GridZ - 1]);
        }

        //checks and adds right neighbor
        if (node.GridX + 1 >= 0 && node.GridX + 1 < xWidth && node.GridZ >= 0 && node.GridZ < zWidth)
        {
            neighbors.Add(childrenByPosition[node.GridX + 1, node.GridZ]);
        }

        //checks and adds left neighbor
        if (node.GridX - 1 >= 0 && node.GridX - 1 < xWidth && node.GridZ >= 0 && node.GridZ < zWidth)
        {
            neighbors.Add(childrenByPosition[node.GridX - 1, node.GridZ]);
        }

        return(neighbors);
    }
Exemplo n.º 2
0
    public void test()
    {
        GridCellProperties start  = childrenByPosition[0, 0];
        GridCellProperties target = childrenByPosition[xWidth - 1, zWidth - 1];

        findPath(start, target);

        path.ForEach(step => Debug.Log(step.GridX + " " + step.GridZ));
    }
Exemplo n.º 3
0
    private int GetDistance(GridCellProperties nodeA, GridCellProperties nodeB)
    {
        int dstX = Mathf.Abs(nodeA.GridX - nodeB.GridX);
        int dstY = Mathf.Abs(nodeA.GridZ - nodeB.GridZ);

        if (dstX > dstY)
        {
            return(14 * dstY + 10 * (dstX - dstY));
        }
        return(14 * dstX + 10 * (dstY - dstX));
    }
Exemplo n.º 4
0
    private void RetracePath(GridCellProperties startNode, GridCellProperties endNode)
    {
        List <GridCellProperties> newPath     = new List <GridCellProperties>();
        GridCellProperties        currentNode = endNode;

        while (currentNode != startNode)
        {
            newPath.Add(currentNode);
            currentNode = currentNode.parent;
        }
        newPath.Reverse();
        path = newPath;
    }
Exemplo n.º 5
0
    private List <GridCellProperties> findPath(GridCellProperties start, GridCellProperties target)
    {
        List <GridCellProperties>    openSet   = new List <GridCellProperties>();
        HashSet <GridCellProperties> closedSet = new HashSet <GridCellProperties>();

        openSet.Add(start);

        //calculates path for path finding
        while (openSet.Count > 0)
        {
            GridCellProperties node = openSet[0];
            openSet.Remove(node);
            closedSet.Add(node);

            //If target found, retrace path
            if (node == target)
            {
                RetracePath(start, target);
                return(path);
            }

            foreach (GridCellProperties neighbour in getNeighbors(node))
            {
                if (neighbour.flags.HasFlag(BlockStatus.Occupied) || !neighbour.flags.HasFlag(BlockStatus.Walkable) || closedSet.Contains(neighbour))
                {
                    continue;
                }

                int newCostToNeighbour = GetDistance(node, neighbour);
                if (newCostToNeighbour < neighbour.cost || !openSet.Contains(neighbour))
                {
                    neighbour.cost   = GetDistance(neighbour, target);
                    neighbour.parent = node;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
        return(null);
    }
Exemplo n.º 6
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        EditorGUI.BeginChangeCheck();
        GridCellProperties script = (GridCellProperties)target;

        script.flags = (BlockStatus)EditorGUILayout.EnumFlagsField(script.flags);
        EditorUtility.SetDirty(script);
        if (EditorGUI.EndChangeCheck())
        {
            /*manual assignment here, remember to check that the selected objects
             * are in fact of the appropriate type.*/
            foreach (Object obj in targets)
            {
                ((GridCellProperties)obj).flags = script.flags;
                EditorUtility.SetDirty(((GridCellProperties)obj));
            }
        }
    }
Exemplo n.º 7
0
    public void GenerateLevel()
    {
        if (!Application.isEditor)
        {
            Debug.LogWarning("You may not use this function outside of edit mode");
            return;
        }
#if UNITY_EDITOR
        for (int x = 0; x < xWidth; x++)
        {
            for (int z = 0; z < zWidth; z++)
            {
                GameObject ob = PrefabUtility.InstantiatePrefab((GameObject)Resources.Load("GridCell"), transform) as GameObject;
                ob.transform.position = Grid.CellToWorld(new Vector3Int(x, z, 0));
                GridCellProperties gCPRef = ob.GetComponent <GridCellProperties>();
                gCPRef.GridX = x;
                gCPRef.GridZ = z;
            }
        }
        CreateGridData();
#endif
    }