public void MoveToRandomNode() { MapGraphNode targetNode = null; Direction selectedDirection = (Direction)UnityEngine.Random.Range(1, 4); // Try to avoid coming back the same way if (selectedDirection == currentDirection.OppositeDirection()) { selectedDirection = selectedDirection.NextDirection(); } // Find a valid movement direction for (int i = 0; i < 4; i++) { targetNode = mapMovingEntity.nodeFrom.NodeDirection(selectedDirection); if (targetNode != null) { break; } selectedDirection = selectedDirection.NextDirection(); } if (targetNode == null) { Log.Error(this, "Cannot find a valid target node!"); return; } currentDirection = selectedDirection; mapMovingEntity.MoveTo(targetNode); }
public WeightedNode(MapGraphNode node, MapGraphNode targetNode, WeightedNode parentNode) { this.node = node; this.parentDistance = (node.transform.position - parentNode.node.transform.position).sqrMagnitude; this.targetDistance = (node.transform.position - targetNode.transform.position).sqrMagnitude; this.parentNode = parentNode; this.pathDistance = parentNode.pathDistance + parentDistance; }
public MapGraphNode NextNode(MapGraphNode targetNode) { List <WeightedNode> openNodes = new List <WeightedNode>(); List <WeightedNode> closedNodes = new List <WeightedNode>(); openNodes.Add(new WeightedNode(mapMovingEntity.nodeFrom, targetNode)); while (openNodes.First().node != targetNode) { openNodes.OrderBy(n => n.TotalCost); closedNodes.Add(openNodes.First()); openNodes.RemoveAt(0); for (int i = 0; i < 4; i++) { var neighbouringNode = closedNodes.Last().node.NodeDirection((Direction)i); if (neighbouringNode == null) { continue; } WeightedNode neighbourWeightedNode = closedNodes.Find(n => n.node == neighbouringNode); if (neighbourWeightedNode != null) { continue; } neighbourWeightedNode = openNodes.Find(n => n.node == neighbouringNode); if (neighbourWeightedNode == null) { openNodes.Add(new WeightedNode(neighbouringNode, targetNode, closedNodes.Last())); } else { float currentParentDistance = (neighbourWeightedNode.node.transform.position - closedNodes.Last().node.transform.position).sqrMagnitude; if (neighbourWeightedNode.pathDistance > closedNodes.Last().pathDistance + currentParentDistance) { neighbourWeightedNode.parentNode = closedNodes.Last(); neighbourWeightedNode.parentDistance = currentParentDistance; neighbourWeightedNode.pathDistance = closedNodes.Last().pathDistance + currentParentDistance; } } } } WeightedNode currentNode = openNodes.First(); while (currentNode.parentNode.parentNode != null) { currentNode = currentNode.parentNode; } return(currentNode.node); }
public void MoveTo(MapGraphNode targetNode) { nodeTo = targetNode; if (movementCoroutine != null) { StopCoroutine(movementCoroutine); } movementCoroutine = StartCoroutine(Move()); }
public void AutoAssignNeighbour(ref MapGraphNode neighbour, Func <MapGraphNode, MapGraphNode, bool> condition) { var nodes = GameObject.FindObjectsOfType <MapGraphNode>(); MapGraphNode bestNode = null; foreach (var node in nodes) { if (condition(bestNode, node)) { bestNode = node; } } neighbour = bestNode; EditorUtility.SetDirty(this); }
public void MoveDirection(Direction targetDirection) { if (targetDirection == Direction.None) { return; } if (targetDirection == movementDirection) { return; } MapGraphNode targetNode = null; if (IsMoving == false) { targetNode = mapMovingEntity.nodeFrom.NodeDirection(targetDirection); } else { if (movementDirection == targetDirection.OppositeDirection()) { targetNode = mapMovingEntity.nodeFrom; mapMovingEntity.nodeFrom = mapMovingEntity.nodeTo; } else { nextMovementDirection = targetDirection; return; } } if (targetNode != null) { movementDirection = targetDirection; nextMovementDirection = targetDirection; continuationDirection = targetDirection; mapMovingEntity.MoveTo(targetNode); animator.SetBool("IsMoving", true); RotateDirection(targetDirection); } }
IEnumerator Move() { while (true) { float moveDistance = Time.deltaTime * movementSpeed; float remainingDistance = Vector3.Distance(transform.position, nodeTo.transform.position); if (moveDistance >= remainingDistance) { transform.position = nodeTo.transform.position; nodeFrom = nodeTo; nodeTo = null; MovementFinished(); yield break; } else { transform.position += (nodeTo.transform.position - transform.position).normalized * moveDistance; yield return(null); } } }
public EnemyReturnToBaseMovement(MapMovingEntity mapMovingEntity, MapGraphNode baseNode, Action movementFinishedAction) : base(mapMovingEntity) { this.movementFinishedAction = movementFinishedAction; this.baseNode = baseNode; }
public EnemyMovementAStar(MapMovingEntity mapMovingEntity, MapGraphNode targetNode) : base(mapMovingEntity) { this.targetNode = targetNode; }
public EnemyMovementTarget(MapMovingEntity mapMovingEntity, MapGraphNode targetNode, Action movementFinishedAction) : base(mapMovingEntity) { this.targetNode = targetNode; this.movementFinishedAction = movementFinishedAction; }