Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        public void MoveTo(MapGraphNode targetNode)
        {
            nodeTo = targetNode;

            if (movementCoroutine != null)
            {
                StopCoroutine(movementCoroutine);
            }

            movementCoroutine = StartCoroutine(Move());
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
            }
        }
Exemplo n.º 7
0
        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);
                }
            }
        }
Exemplo n.º 8
0
 public EnemyReturnToBaseMovement(MapMovingEntity mapMovingEntity, MapGraphNode baseNode, Action movementFinishedAction) : base(mapMovingEntity)
 {
     this.movementFinishedAction = movementFinishedAction;
     this.baseNode = baseNode;
 }
Exemplo n.º 9
0
 public EnemyMovementAStar(MapMovingEntity mapMovingEntity, MapGraphNode targetNode) : base(mapMovingEntity)
 {
     this.targetNode = targetNode;
 }
Exemplo n.º 10
0
 public EnemyMovementTarget(MapMovingEntity mapMovingEntity, MapGraphNode targetNode, Action movementFinishedAction) : base(mapMovingEntity)
 {
     this.targetNode             = targetNode;
     this.movementFinishedAction = movementFinishedAction;
 }