Example #1
0
        /// <summary>
        /// 设置新路径
        /// </summary>
        public void SetPath(Queue <Coord> path)
        {
            //从path的副本中设置路径,防止外界的修改
            if (path != null)
            {
                this.PathQueue = new Queue <Coord>(path);
            }

            if (ExistPath)
            {
                TargetPos    = PathQueue.Peek();
                StartPos     = PathQueue.Peek();
                EndTargetPos = PathQueue.ElementAt(PathQueue.Count - 1);
                if (this.PathQueue.Count < 2)
                {
                    EndDrection = Direction.No;
                }
                else
                {
                    EndDrection = (EndTargetPos - this.PathQueue.ElementAt(this.PathQueue.Count - 2)).GetDirection;
                }
            }
            else
            {
                TargetPos    = Coord.Empty;
                StartPos     = Coord.Empty;
                EndTargetPos = Coord.Empty;
                EndDrection  = Direction.No;
            }
        }
 void Start()
 {
     if (PathCache == null)
     {
         PathCache = new Queue <Vector3>();
     }
     if (PathQueue.Count > 0)
     {
         TargetNode    = PathQueue.Dequeue();
         StartPosition = TargetNode;
     }
     else
     {
         Debug.Log("PathQueue is empty");
     }
 }
 void ReachedGoal(Action goalAction)
 {
     if (goalAction != null)
     {
         goalAction();
         Debug.Log("Performed Goal Action");
     }
     else
     {
         Debug.Log("Goal actions is empty");
     }
     foreach (Vector3 node in PathCache)
     {
         PathQueue.Enqueue(node);
     }
     this.transform.position = StartPosition;
     this.gameObject.SetActive(false);
 }
    // Update is called once per frame
    void Update()
    {
        Vector3 dir = TargetNode - transform.position;

        dir.Normalize();

        transform.Translate(dir * Time.deltaTime * CurrentCreep.MovementSpeed);

        if (Vector3.Distance(transform.position, TargetNode) < NodeMargin)
        {
            Debug.Log("Creep reached target node");
            if (PathQueue.Count > 0)
            {
                PathCache.Enqueue(TargetNode);
                TargetNode = PathQueue.Dequeue();
            }
            else
            {
                ReachedGoal(GoalAction);
            }
        }
    }