예제 #1
0
    //moves this formation to the target hex if it is within movement range
    public IEnumerator MoveToHex(Vector3 targetLocation)
    {
        //lock animation
        isAnimating = true;

        //find a path using the pathgrid
        pathGrid.GetFinalPath(transform.position, targetLocation, movementRemaining, facing);
        //if a path exists, move to it
        if (pathGrid.finalPath.Count != 0)
        {
            //subtract the movement cost from remaining movement
            movementRemaining -= pathGrid.pathCost;
            //store the final node in case the coroutine needs to end early
            PathNode finalNode = pathGrid.finalPath[pathGrid.finalPath.Count - 1];

            //go through each point
            for (int i = 1; i < pathGrid.finalPath.Count; i++)
            {
                //if the animation is not being skipped
                if (!skipNextAnimation)
                {
                    //move to next point, set facing, and wait for the movement delay
                    transform.position = pathGrid.NodeToWorld(pathGrid.finalPath[i]);
                    facing             = pathGrid.finalPath[i].prevFacing;

                    //set the rotation based on the facing
                    transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, facing * -60);
                    yield return(new WaitForSeconds(movementDelay));
                }
                else
                {
                    //if the animation is being skipped go to the final node and break
                    transform.position = pathGrid.NodeToWorld(finalNode);
                    facing             = finalNode.prevFacing;
                    transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, facing * -60);
                    skipNextAnimation  = false;
                    break;
                }
            }
        }
        else
        {
            Debug.Log("No path exists to the target point or the point is out of range");
        }

        //if no moment remains grey out the formation
        if (movementRemaining <= 0)
        {
            gameObject.GetComponent <SpriteRenderer>().color = Color.grey;
        }

        //unlock animation
        isAnimating = false;
    }