Example #1
0
    void UpdateDoMovement(float deltaTime)
    {
        if (currentTile == destinationTile)
        {
            pathAStar = null;
            return;
        }

        if (nextTile == null || nextTile == currentTile)
        {
            if (pathAStar == null || pathAStar.count() == 0)
            {
                pathAStar = new Path_AStar(World.worldInstance, currentTile, destinationTile);

                if (pathAStar.count() == 0)
                {
                    Debug.LogError("Path_AStar -- returned (0) no path to destinationTile");
                    AbbandonJob();
                    return;
                }

                pathAStar.Dequeue();
            }

            nextTile = pathAStar.Dequeue();

            /*if (nextTile == destinationTile) {
             *      nextTile = currentTile;
             *      destinationTile = currentTile;
             * }*/
        }

        float distanceToTravel = Mathf.Sqrt(Mathf.Pow(currentTile.X - nextTile.X, 2) + Mathf.Pow(currentTile.Y - nextTile.Y, 2));

        if (nextTile.isAccessible() == Accessiblity.NEVER)
        {
            Debug.LogError("Charcter tried to enter in unwalkable tile");
            nextTile  = null;
            pathAStar = null;
            return;
        }
        else if (nextTile.isAccessible() == Accessiblity.SOON)
        {
            return;
        }

        float distanceThisFrame   = speed / nextTile.movementCost * deltaTime;
        float percentageThisFrame = distanceThisFrame / distanceToTravel;

        movementPercentage += percentageThisFrame;

        if (movementPercentage >= 1)
        {
            currentTile        = nextTile;
            movementPercentage = 0;
        }
    }
Example #2
0
    void GetNewJob()
    {
        myJob = World.worldInstance.jobQueue.Dequeue();

        if (myJob == null)
        {
            destinationTile = currentTile;
            return;
        }

        destinationTile = myJob.tile;
        myJob.RegisterJobStoppedCallback(OnJobStopped);
        pathAStar = new Path_AStar(World.worldInstance, currentTile, destinationTile);

        if (pathAStar.count() == 0)
        {
            Debug.LogError("Path_AStar -- returned (0) no path to target job tile");
            AbbandonJob();
            destinationTile = currentTile;
        }
    }