Exemple #1
0
    private void TurnOffSideWalls(ScriptableNode bridgeTo)
    {
        string bridgeSide = bridgeTo.Parent;

        bridgeTo.TurnOffWalls(WallDeactivator.DeactivateNeigbourWall(bridgeSide, currentNodeParent));
        currentNode.TurnOffWalls(WallDeactivator.DeactivateOwnWall(bridgeSide, currentNodeParent));
    }
 public void SetStart(ScriptableNode start)
 {
     Clear();
     start.Walkable = true;
     SetColor(GetChild(start), Color.green);
     Source = start;
 }
    public List <ScriptableNode> GetNeighbors(ScriptableNode node)
    {
        var neighbors = new List <ScriptableNode>();
        var dirs      = new List <Point>()
        {
            new Point(1, 0),
            new Point(1, 1),
            new Point(0, 1),
            new Point(-1, 1),
            new Point(-1, 0),
            new Point(-1, -1),
            new Point(0, -1),
            new Point(1, -1),
        };

        foreach (var dir in dirs)
        {
            var nay = Nodes.Find(n => n.U == node.U + dir.U && n.V == node.V + dir.V);
            if (nay != null)
            {
                neighbors.Add(nay);
            }
        }

        return(neighbors);
    }
    public void SetColor(ScriptableNode s, Color c)
    {
        GameObject go = GetChild(s);

        go.GetComponent <MeshRenderer>().material.color = c;
        go.GetComponent <NodeBehaviour>().Tween();
    }
Exemple #5
0
    private void SaveNodeToMazeModell(int x, int y, int i, ScriptableNode node)
    {
        switch (sides[i].name)
        {
        case "Top":
            mazeModellTop[x, y] = node;
            break;

        case "Front":
            mazeModellFront[x, y] = node;
            break;

        case "Back":
            mazeModellBack[x, y] = node;
            break;

        case "Bottom":
            mazeModellBottom[x, y] = node;
            break;

        case "Right":
            mazeModellRight[x, y] = node;
            break;

        case "Left":
            mazeModellLeft[x, y] = node;
            break;

        default:
            Debug.LogWarning("Couldn't add to mazemodells as side: " + sides[i].name + " doesn't exist");
            break;
        }
    }
Exemple #6
0
    public static string GetLowXSide(ScriptableNode node)
    {
        switch (node.Parent)
        {
        case "Top":
            return("Left");

        case "Front":
            return("Left");

        case "Back":
            return("Left");

        case "Bottom":
            return("Left");

        case "Right":
            return("Front");

        case "Left":
            return("Back");

        default:
            Debug.Log("outside the switch plz check");
            return(null);
        }
    }
Exemple #7
0
    private void TurnOffWalls(ScriptableNode bridgeTo)
    {
        Vector2Int dir = bridgeTo.ExploredFrom;

        bridgeTo.TurnOffWalls(-dir);
        currentNode.TurnOffWalls(dir);
    }
    public List <ScriptableNode> GetNeighbors(ScriptableNode node)
    {
        var neighbors = new List <ScriptableNode>();

        var dirs = new List <Point>()
        {
            new Point(1, 0),   //right
            new Point(1, 1),   //top right
            new Point(0, 1),   //top
            new Point(-1, 1),  //topleft
            new Point(-1, 0),  //left
            new Point(-1, -1), //botleft
            new Point(0, -1),  //bot
            new Point(1, -1),  //botright
        };

        foreach (var dir in dirs)
        {
            var nay = Nodes.Find(n => n.U == node.U + dir.U && n.V == node.V + dir.V);
            if (nay == null)
            {
                continue;
            }
            neighbors.Add(nay);
        }

        return(neighbors);
    }
Exemple #9
0
    // refactor this with either a default or falling cases

    public static Vector2Int ConvertToLowY(ScriptableNode node, Vector2Int cubeSize)
    {
        int x = cubeSize.x - 1;
        int y = cubeSize.y - 1;

        switch (node.Parent)
        {
        case "Top":
            return(new Vector2Int(node.Coordinates.x, 0));

        case "Front":
            return(new Vector2Int(node.Coordinates.x, 0));

        case "Back":
            return(new Vector2Int(node.Coordinates.x, 0));

        case "Bottom":
            return(new Vector2Int(node.Coordinates.x, 0));

        case "Right":
            return(new Vector2Int(node.Coordinates.y, node.Coordinates.x));

        case "Left":
            return(new Vector2Int(0, y - node.Coordinates.x));

        default:
            Debug.Log("outside the switch plz check");
            return(new Vector2Int(0, 0));;
        }
    }
Exemple #10
0
    private void InitializeNewNode(int i, int x, int y)
    {
        ScriptableNode newNode = (ScriptableNode)ScriptableObject.CreateInstance("ScriptableNode");

        newNode.Initialize(GetPos(x, y, i), new Vector2Int(x, y), gridSize, sides[i]);

        CreateEdgeWalls(x, y, newNode);
        SaveNodeToMazeModell(x, y, i, newNode);
    }
 public GameObject GetChild(ScriptableNode s)
 {
     if (Children[s.Id].GetComponent <NodeBehaviour>().Node != s)
     {
         Debug.LogError("node behaviour node mismatch");
         return(null);
     }
     return(Children[s.Id]);
 }
 public void Initialize(AstarNode n)
 {
     Parent    = null;
     Neighbors = new List <ScriptableNode>();
     Walkable  = true;
     G         = n.G;
     H         = n.H;
     U         = n.U;
     V         = n.V;
     Id        = n.Id;
 }
    public IEnumerator Astar(ScriptableNode start, ScriptableNode goal, List <ScriptableNode> cameFrom)
    {
        Timer    = 0;
        cameFrom = new List <ScriptableNode>();
        var closedSet = new List <ScriptableNode>();
        var openSet   = new List <ScriptableNode>();

        openSet.Add(start);
        SetColor(start, Color.cyan);
        start.H = heuristic_cost_estimate(start, goal);
        start.G = 0;
        start.F = start.G + start.H;
        while (openSet.Count > 0)
        {
            openSet.Sort((a, b) => a.F.CompareTo(b.F));
            var current = openSet[0]; //the node in openSet having the lowest fScore[] value
            if (current == goal)
            {
                cameFrom = reconstruct_path(goal);
                yield break;
            }

            openSet.Remove(current);
            closedSet.Add(current);
            SetColor(current, Color.grey);
            foreach (var neighbor in current.Neighbors)
            {
                //if in the closed list or not walkable ignore it
                if (closedSet.Contains(neighbor) || !neighbor.Walkable)
                {
                    continue; // Ignore the neighbor which is already evaluated.
                }
                // The distance from start to a neighbor
                int tentative_gScore = current.G + dist_between(current, neighbor);
                //if it's not in the open list then add it
                if (!openSet.Contains(neighbor)) // Discover a new node
                {
                    openSet.Add(neighbor);
                }
                else if (tentative_gScore >= neighbor.G)
                {
                    continue; // This is not a better path.
                }
                SetColor(neighbor, Color.cyan);
                neighbor.Parent = current; //This path is the best until now. Record it!
                neighbor.G      = tentative_gScore;
                neighbor.F      = neighbor.G + heuristic_cost_estimate(neighbor, goal);
            }

            Timer += Time.deltaTime;
            yield return(null);
        }
    }
 public void SetGoal(ScriptableNode goal)
 {
     if (!goal.Walkable || Source == null)
     {
         return;
     }
     Clear();
     SetColor(GetChild(goal), Color.green);
     StopAllCoroutines();
     Timer = 0;
     StartCoroutine(Astar(Source, goal, Path));
 }
Exemple #15
0
    private void HandleWallTurnOff(List <ScriptableNode> closeVisisted)
    {
        ScriptableNode bridgeTo = closeVisisted[Random.Range(0, closeVisisted.Count)];

        if (currentNodeParent == bridgeTo.Parent)
        {
            TurnOffWalls(bridgeTo);
        }
        else
        {
            TurnOffSideWalls(bridgeTo);
        }
    }
    public List <ScriptableNode> reconstruct_path(ScriptableNode s)
    {
        var iterator = s;
        var p        = new List <ScriptableNode>();

        while (iterator != Source)
        {
            p.Add(iterator);
            iterator = iterator.Parent;
        }
        p.Add(Source);
        p.ForEach(n => SetColor(GetChild(n), Color.yellow));
        return(p);
    }
Exemple #17
0
    private void AddToFrontier(Vector2Int direction)
    {
        ScriptableNode node = FindNode(direction);

        if (node == null)
        {
            return;
        }
        if (visitedNodes.Contains(node))
        {
            return;
        }

        frontier.Add(node);
    }
Exemple #18
0
    private ScriptableNode FindVisitedNode(Vector2Int direction)
    {
        ScriptableNode node = FindNode(direction);

        if (node == null)
        {
            return(null);
        }
        if (!visitedNodes.Contains(node))
        {
            return(null);
        }

        node.ExploredFrom = direction;
        return(node);
    }
Exemple #19
0
 private void CreateEdgeWalls(int x, int y, ScriptableNode node)
 {
     if (x == cubeSize.x - 1)
     {
         node.TurnOnThiccWall(Vector2Int.right);
     }
     if (x == 0)
     {
         node.TurnOnThiccWall(Vector2Int.zero);
     }
     if (y == cubeSize.y - 1)
     {
         node.TurnOnThiccWall(Vector2Int.up);
     }
     if (y == 0)
     {
         node.TurnOnThiccWall(Vector2Int.down);
     }
 }
Exemple #20
0
    private void ConnectNewNode()
    {
        List <ScriptableNode> closeVisisted = new List <ScriptableNode>();

        foreach (var direction in directions)
        {
            ScriptableNode nodeToAdd = FindVisitedNode(direction);
            if (nodeToAdd != null)
            {
                closeVisisted.Add(nodeToAdd);
            }
        }

        if (closeVisisted.Count < 1)
        {
            return;
        }

        HandleWallTurnOff(closeVisisted);
    }
    public void CreateNodes()
    {
        ClearNodes();
        Children = new List <GameObject>();
        Nodes    = new List <ScriptableNode>();
        var positions = Utilities.CreateGrid(Rows, Cols);

        foreach (var p in positions)
        {
            var node = ScriptableObject.CreateInstance <ScriptableNode>();
            node.Initialize(new AstarNode(p.U, p.V, Nodes.Count));
            node.name = string.Format("Node {0}", Nodes.Count.ToString());
            Nodes.Add(node);
        }

        Nodes.ForEach(n => n.Neighbors = GetNeighbors(n));
        Nodes.ForEach(n => n.Walkable  = true);
        Current = Nodes[0];
        Goal    = Nodes[Nodes.Count - 1];
        CreateChildren();
    }
Exemple #22
0
    // refactor this
    private void RemoveDuplicates()
    {
        foreach (var node in visitedNodes)
        {
            string nodeParent = node.Parent;
            currentNode = node;

            foreach (var direction in directions)
            {
                if (!node.GetWall(direction))
                {
                    continue;
                }

                string     parent = nodeParent;
                Vector2Int cords  = node.Coordinates + direction;

                //CheckParentAndCords(ref parent, ref cords);
                ScriptableNode connect = TryGetNode(cords, parent);

                if (connect == null)
                {
                    continue;
                }

/*                string connectParent = connect.Parent;
 *
 *              if (connectParent != nodeParent)
 *              {
 *                  connect.TurnOffWalls(WallDeactivator.DeactivateNeigbourWall(connectParent, nodeParent));
 *              }
 *              else
 *              {
 *                  connect.TurnOffWalls(-direction);
 *              }*/

                connect.TurnOffWalls(-direction);
            }
        }
    }
    public void CreateChildren()
    {
        foreach (var n in Nodes)
        {
            GameObject go = (prefab == null) ? GameObject.CreatePrimitive(PrimitiveType.Quad) : go = Instantiate(prefab);
            go.transform.SetParent(transform);
            go.transform.localPosition = new Vector3(n.U * Offset, n.V * Offset);
            go.transform.localScale   *= Scale;
            go.transform.localRotation = Quaternion.identity;
            go.name = string.Format("Node {0}", n.Id);
            go.GetComponent <MeshRenderer>().receiveShadows    = false;
            go.GetComponent <MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
            var nb = go.GetComponent <NodeBehaviour>();
            if (nb == null)
            {
                nb = go.AddComponent <NodeBehaviour>();
            }

            nb.Node          = n;
            nb.gridBehaviour = this;
            Source           = Nodes[0];
            Children.Add(go);
        }
    }
 public int heuristic_cost_estimate(ScriptableNode s, ScriptableNode g)
 {
     return(Utilities.ManhattanDistance(s.U, s.V, g.U, g.V));;
 }
Exemple #25
0
 private void GetNewFrontier()
 {
     currentNode       = frontier.ElementAt(Random.Range(0, frontier.Count));
     currentNodeParent = currentNode.Parent;
     visitedNodes.Add(currentNode);
 }
 public void SetStart(ScriptableNode s)
 {
     throw new NotImplementedException();
 }
    public int dist_between(ScriptableNode current, ScriptableNode neighbor)
    {
        int cost = (current.U == neighbor.U || current.V == neighbor.V) ? 10 : 14;

        return(cost);
    }