Exemple #1
0
    public void RecalculateNeighbours()
    {
        position = GetComponent <Transform>().position;
        List <RaycastHit2D> neighbours = new List <RaycastHit2D>()
        {
            BlockAtRelativePos(-2, 4), BlockAtRelativePos(-1, 4), BlockAtRelativePos(0, 4), BlockAtRelativePos(1, 4), BlockAtRelativePos(2, 4),
            BlockAtRelativePos(-2, 3), BlockAtRelativePos(-1, 3), BlockAtRelativePos(0, 3), BlockAtRelativePos(1, 3), BlockAtRelativePos(2, 3),
            BlockAtRelativePos(-2, 2), BlockAtRelativePos(-1, 2), BlockAtRelativePos(0, 2), BlockAtRelativePos(1, 2), BlockAtRelativePos(2, 2),
            BlockAtRelativePos(-2, 1), BlockAtRelativePos(-1, 1), BlockAtRelativePos(0, 1), BlockAtRelativePos(1, 1), BlockAtRelativePos(2, 1),
            BlockAtRelativePos(-2, 0), BlockAtRelativePos(-1, 0), BlockAtRelativePos(1, 0), BlockAtRelativePos(2, 0),
            BlockAtRelativePos(-2, -1), BlockAtRelativePos(-1, -1), BlockAtRelativePos(0, -1), BlockAtRelativePos(1, -1), BlockAtRelativePos(2, -1),
            BlockAtRelativePos(-2, -2), BlockAtRelativePos(-1, -2), BlockAtRelativePos(0, -2), BlockAtRelativePos(1, -2), BlockAtRelativePos(2, -2),
            BlockAtRelativePos(-2, -3), BlockAtRelativePos(-1, -3), BlockAtRelativePos(0, -3), BlockAtRelativePos(1, -3), BlockAtRelativePos(2, -3),
            BlockAtRelativePos(-2, -4), BlockAtRelativePos(-1, -4), BlockAtRelativePos(0, -4), BlockAtRelativePos(1, -4), BlockAtRelativePos(2, -4),
        };

        foreach (RaycastHit2D block in neighbours)
        {
            if (!block)
            {
            }
            if (block)
            {
                MovementMeshNode node = block.collider.gameObject.GetComponent <MovementMeshNode>();
                if (node)
                {
                    node.RecalculateConnections();
                }
            }
        }
    }
Exemple #2
0
    private void AddConnectionIfValid(float x, float y, List <Vector2> requirements)
    {
        RaycastHit2D block = BlockAtRelativePos(x, y);

        if (!block)
        {
            return;
        }
        MovementMeshNode node = block.collider.gameObject.GetComponent <MovementMeshNode>();

        if (!node || !node.IsValid())
        {
            return;
        }
        foreach (Vector2 coords in requirements)
        {
            float xR = coords.x;
            float yR = coords.y;
            if (!IsFree(xR, yR))
            {
                return;
            }
        }
        AddToConnected(node);
        node.AddToConnected(this);
    }
Exemple #3
0
    private void UpdatePath()
    {
        if (!hasTarget)
        {
            return;
        }

        MovementMeshNode        x = globalMovementMesh.FindClosestNode(t.position);
        MovementMeshNode        y = globalMovementMesh.FindClosestNode(target != null ? target.position : fallback_target_pos);
        List <MovementMeshNode> l = globalMovementMesh.GetRoute(x, y);

        path = new List <Vector2>();
        if (l != null)
        {
            foreach (MovementMeshNode n in l)
            {
                path.Add(n.GetPosition());
            }
            currentWaypoint = 0;
        }
        else
        {
            character.Move(0);
            path = null;
        }
    }
Exemple #4
0
 public void AddToConnected(MovementMeshNode c)
 {
     foreach (MovementMeshNode m in connected)
     {
         if (m.GetPosition() == c.GetPosition())
         {
             return;
         }
     }
     connected.Add(c);
 }
Exemple #5
0
 public virtual void Die()
 {
     Destroy(gameObject);
     if (Game.Items.ContainsKey(itemID))
     {
         GameObject item = Instantiate(Resources.Load <GameObject>($"Items/{Game.Items[itemID]}_Item"));
         item.transform.position = transform.position;
         MovementMeshNode n = GetComponent <MovementMeshNode>();
         GlobalMovementMesh.Instance.RemoveNode(n);
         n.RecalculateNeighbours();
     }
 }
Exemple #6
0
    private List <MovementMeshNode> RebuildPath(Dictionary <MovementMeshNode, MovementMeshNode> cameFrom, MovementMeshNode last)
    {
        List <MovementMeshNode> path = new List <MovementMeshNode>()
        {
            last
        };
        MovementMeshNode current = last;

        while (cameFrom.ContainsKey(current))
        {
            current = cameFrom[current];
            path.Add(current);
        }
        path.Reverse();
        return(path);
    }
Exemple #7
0
    public MovementMeshNode FindClosestNode(Vector2 p)
    {
        MovementMeshNode result      = null;
        float            minDistance = -1;

        foreach (MovementMeshNode n in allNodes)
        {
            float distance = Vector2.Distance(n.GetPosition() + Vector2.up, p);
            if (minDistance == -1 || distance < minDistance)
            {
                minDistance = distance;
                result      = n;
            }
        }
        return(result);
    }
Exemple #8
0
    public List <MovementMeshNode> GetRoute(MovementMeshNode start, MovementMeshNode goal)
    {
        if (start == null || goal == null)
        {
            return(null);
        }
        SortedSet <MovementMeshNode> openSet = new SortedSet <MovementMeshNode>(new BestNextNode(goal))
        {
            start
        };
        Dictionary <MovementMeshNode, MovementMeshNode> cameFrom  = new Dictionary <MovementMeshNode, MovementMeshNode>();
        Dictionary <MovementMeshNode, float>            costSoFar = new Dictionary <MovementMeshNode, float>()
        {
            { start, 0 }
        };
        Dictionary <MovementMeshNode, float> expectedCost = new Dictionary <MovementMeshNode, float>()
        {
            { start, Heuristic(start, goal) }
        };

        while (openSet.Count > 0)
        {
            MovementMeshNode current = openSet.Max;
            if (current == goal)
            {
                return(RebuildPath(cameFrom, current));
            }
            _ = openSet.Remove(current);
            float next_score = costSoFar[current] + 1;
            foreach (MovementMeshNode n in current.GetConnected())
            {
                if (!costSoFar.ContainsKey(n) || (next_score < costSoFar[n]))
                {
                    cameFrom[n]     = current;
                    costSoFar[n]    = next_score;
                    expectedCost[n] = costSoFar[n] + Heuristic(n, goal);
                    if (!openSet.Contains(n))
                    {
                        _ = openSet.Add(n);
                    }
                }
            }
        }
        return(null);
    }
    public bool PlaceBlock(int itemID, Vector3 position)
    {
        if (GetBlockAtPosition(position))
        {
            return(false);
        }

        GameObject block = Resources.Load($"Blocks/{Game.Items[itemID]}") as GameObject;

        block.transform.position = position;

        GameObject blockInstance = Instantiate(block);

        blockInstance.transform.parent = transform;

        MovementMeshNode movementMeshNode = blockInstance.GetComponent <MovementMeshNode>();

        if (movementMeshNode)
        {
            movementMeshNode.RecalculateNeighbours();
        }

        return(true);
    }
Exemple #10
0
 private float Heuristic(MovementMeshNode s, MovementMeshNode f)
 {
     return(Vector2.Distance(s.GetPosition(), f.GetPosition()));
 }
Exemple #11
0
 public void RemoveNode(MovementMeshNode n)
 {
     _ = allNodes.Remove(n);
 }
Exemple #12
0
 public void AddNode(MovementMeshNode n)
 {
     allNodes.Add(n);
 }
Exemple #13
0
 public void RemoveFromConnected(MovementMeshNode c)
 {
     _ = connected.Remove(c);
 }