private static void StepTo(Node node) { CollisionLayers collisionMask = CollisionLayers.Walk; Node newNode = null; int dirStart; int dirEnd; if (node.directionIndex == -1) { dirStart = 0; dirEnd = 8; } else if (node.directionIndex % 2 == 0) { dirStart = ((node.directionIndex - 1) + 8) % 8; dirEnd = dirStart + 3; } else { dirStart = ((node.directionIndex - 2) + 8) % 8; dirEnd = dirStart + 5; } for (int i = dirStart; i < dirEnd; ++i) { int dir = i % 8; Vector2i pos = node.pos + directions[dir]; bool passable = CollisionMap.Passable(pos, collisionMask, size: size, ignore: self); if (passable) { if (newNode == null) { newNode = Node.Get(); } newNode.pos = pos; bool closed = closeNodes.Contains(newNode); if (!closed) { newNode.parent = node; newNode.gScore = node.gScore + 1; newNode.hScore = Vector2i.manhattanDistance(target, newNode.pos); newNode.score = newNode.gScore + newNode.hScore; newNode.directionIndex = dir; openNodes.Add(newNode); closeNodes.Add(newNode); newNode = null; } } } if (newNode != null) { newNode.Recycle(); } }
void OnEnable() { map = new Cell[width * height]; for (int i = 0; i < map.Length; ++i) { map[i].blocked = CollisionLayers.All; } origin = width * 5 + 5; instance = this; }
private static void Collapse(Node node) { while (node.parent != null && node.parent.parent != null) { if (CollisionMap.Raycast(node.pos, node.parent.parent.pos, size: size, ignore: self)) { break; } node.parent = node.parent.parent; } }
public CollisionMap(int width, int height) { instance = this; this.width = width; this.height = height; map = new Cell[width * height]; for (int i = 0; i < map.Length; ++i) { map[i].blocked = CollisionLayers.All; } origin = width * 5 + 5; }