Ejemplo n.º 1
0
    void Update_DoMovement(float deltaTime)
    {
        if (currTile == destTile)
        {
            path_AStar = null;
            return;
        }
        if (nextTile == null || nextTile == currTile)
        {
            //get the next tile from pathfinding
            if (path_AStar == null || path_AStar.Length() == 0)
            {
                path_AStar = new Path_AStar(WorldController.Instance.World, currTile, destTile);
                if (path_AStar.Length() == 0)
                {
                    Debug.LogError("Path_AStar returned no path to destination");
                    AbandonJob();
                    path_AStar = null;
                    return;
                }
                nextTile = path_AStar.GetNextTile();
            }
            nextTile = path_AStar.GetNextTile();  //removes it from the path list
            if (nextTile == currTile)
            {
                Debug.Log("Update_DoMovement::NextTile is curr tile ?");
            }
        }

        float        totalDisToTravel = Vector3Int.Distance(currTile, nextTile); //total distance from A to B
        float        movementCost     = 1;
        ENTERABILITY enterability     = ENTERABILITY.Yes;

        if (WorldController.Instance.World.foundationGameMap.ContainsKey(nextTile) == true)
        {
            movementCost = WorldController.Instance.World.foundationGameMap[nextTile].movementCost;
            try{
                enterability = WorldController.Instance.World.foundationGameMap[nextTile].IsEnterable(WorldController.Instance.World.foundationGameMap[nextTile]);
            }
            catch { enterability = ENTERABILITY.Never; }
            if (movementCost > 0 && enterability == ENTERABILITY.Never)
            {
                enterability = ENTERABILITY.Yes;
            }
        }
        if (enterability == ENTERABILITY.Never)
        {
            Debug.LogError("Character " + name + " was trying to enter an unwalkable tile");
            nextTile   = currTile;
            path_AStar = null;
            return;
        }
        else if (enterability == ENTERABILITY.Soon)
        {
            //Have to wait to enter the tile, this is likely a door
            //No bailing on the movement/path, but we do return now and don't actually process the movement;
            return;
        }
        float distanceThisFrame   = speed / movementCost * deltaTime;     //how much distance can character tavel this update
        float percantageThisFrame = distanceThisFrame / totalDisToTravel; //how much is that in percentage

        movementPercentage += percantageThisFrame;                        //increase percentage moved
        if (movementPercentage >= 1)
        {
            //Destination reached
            currTile           = nextTile;
            movementPercentage = 0;
        }
        if (cbCharacterMoved != null)
        {
            cbCharacterMoved(this);
        }
    }
Ejemplo n.º 2
0
    void Update_Movement(float deltaTime)
    {
        if (_currentTile == _destinationTile)
        {
            _pathAStar = null;
            return;
        }

        if (_nextTile == null || _nextTile == _currentTile)
        {
            if (_pathAStar == null || _pathAStar.Length() == 0)
            {
                _pathAStar = new Path_AStar(WorldController.Instance.World, _currentTile, _destinationTile);
                if (_pathAStar.Length() == 0)
                {
                    Debug.LogError("Path_AStart returned no path to destination!");
                    AbandonJob();
                    return;
                }

                _nextTile = _pathAStar.GetNextTile();
            }

            _nextTile = _pathAStar.GetNextTile();
            if (_nextTile == _currentTile)
            {
                Debug.LogError("Update_Movement -- nextTile is currentTile?");
            }
        }

        //float distanceToTravel = Mathf.Sqrt(Mathf.Pow(_currentTile.X - _nextTile.X, 2) + Mathf.Pow(_currentTile.Y - _nextTile.Y, 2));

        /*if(_nextTile.MovementCost == 0)
         * {
         *  Debug.LogError("A character was trying to enter an unwalkable tile!");
         *  _nextTile = null;
         *  _pathAStar = null;
         *  return;
         * }*/

        switch (_nextTile.CheckEnterableState())
        {
        case EnterableState.No:
            Debug.LogError("A character was trying to enter an unwalkable tile!");
            _nextTile  = null;
            _pathAStar = null;
            return;

        case EnterableState.Soon:
            return;

        case EnterableState.Yes:
            break;

        default:
            break;
        }

        float distanceToTravel = Vector2.Distance(new Vector2(_currentTile.X, _currentTile.Y), new Vector2(_nextTile.X, _nextTile.Y));

        float distanceThisFrame = (_speed / _nextTile.MovementCost) * deltaTime;

        float percentageThisFrame = distanceToTravel <= 0 ? 1 : distanceThisFrame / distanceToTravel;

        _movementPercentage += percentageThisFrame;
        if (_movementPercentage >= 1)
        {
            _currentTile        = _nextTile;
            _movementPercentage = 0f;
        }
    }