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(); } } } }
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); }
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; } }
public void AddToConnected(MovementMeshNode c) { foreach (MovementMeshNode m in connected) { if (m.GetPosition() == c.GetPosition()) { return; } } connected.Add(c); }
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(); } }
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); }
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); }
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); }
private float Heuristic(MovementMeshNode s, MovementMeshNode f) { return(Vector2.Distance(s.GetPosition(), f.GetPosition())); }
public void RemoveNode(MovementMeshNode n) { _ = allNodes.Remove(n); }
public void AddNode(MovementMeshNode n) { allNodes.Add(n); }
public void RemoveFromConnected(MovementMeshNode c) { _ = connected.Remove(c); }