Exemplo n.º 1
0
    // Need to factor in the total length of ppath so that
    // 1. there is a cap to path length
    // 2. the movement is uniform speed no matter the path length
    public void moveToNextPointOnPath()
    {
        // check for point to move to;
        if (nextPointIndex < 0)
        {
            time = attackDelay;
            attackArea.SetActive(false);
            staminaBar.moving = false;
            staminaBar.recover();
            return;
        }

        // get next point
        Vector3 nextPoint;

        try
        {
            nextPoint = path[nextPointIndex];
        }
        catch (System.ArgumentOutOfRangeException e)
        {
            time = attackDelay;
            attackArea.SetActive(false);
            staminaBar.recover();
            staminaBar.moving = false;
            isMoving          = false;
            return;
        }

        // move
        if (staminaBar.move())
        {
            time -= Time.deltaTime;
            staminaBar.moving = true;
            isMoving          = true;
            // start attacking if attack delay has passed.
            if (time <= 0.0)
            {
                attackArea.SetActive(true);
            }

            var maxDistanceDelta = Time.deltaTime * speed;

            // thingToMove.transform.LookAt(nextPoint);
            thingToMove.transform.position = Vector3.MoveTowards(thingToMove.transform.position, nextPoint, maxDistanceDelta);

            //Debug.Log(Vector3.Distance(thingToMove.transform.position, nextPoint) + " " + maxDistanceDelta);
            if (Vector3.Distance(thingToMove.transform.position, nextPoint) < maxDistanceDelta)
            {
                if (nextPointIndex > 0)
                {
                    path.RemoveAt(nextPointIndex - 1);
                    nextPointIndex--;
                }
                nextPointIndex++;
            }
        }
        //TODO: Do somehting if no stamina left
        else
        {
            // remove remainder of path
            resetPath();
        }
    }