Exemplo n.º 1
0
    /// <summary>
    /// Moves each ghost based on the chosen IntersectionTiles
    /// </summary>
    public void Move()
    {
        if (_targetIntersectionTile != _currentIntersectionTile && _targetIntersectionTile != null && !IsInGhostHouse)
        {
            if (TargetWasOvershot())
            {
                _currentIntersectionTile = _targetIntersectionTile;
                transform.localPosition  = _currentIntersectionTile.transform.localPosition;

                if (_currentIntersectionTile.IsPortal)
                {
                    transform.localPosition  = _currentIntersectionTile.OppositePortal.transform.localPosition;
                    _currentIntersectionTile = _currentIntersectionTile.OppositePortal;
                }

                if (_currentIntersectionTile.IsGhostHouseEntrance && CurrentGhostMode == GhostMode.Eaten)
                {
                    ChangeGhostMode(_previousGhostMode);
                }

                _targetIntersectionTile   = ChooseNextTile();
                _previousIntersectionTile = _currentIntersectionTile;
                _currentIntersectionTile  = null;
            }
            else
            {
                transform.localPosition += Helpers.GetVectorDirection(_currentMoveDirection) * NormalSpeed * Time.deltaTime;
            }
        }
    }
Exemplo n.º 2
0
    private void Awake()
    {
        _spriteAnimator = GetComponentInChildren <Animator>();

        _player           = GameObject.FindGameObjectWithTag("Player");
        _playerController = _player.GetComponent <PlayerController>();

        // sets the first movement of each ghost to scatter, using the out of bounds tiles
        CurrentGhostMode = GhostMode.Scatter;
        GetScatterTile();

        var nextTile = Helpers.GetIntersectionTile(transform.localPosition);

        if (nextTile != null)
        {
            _currentIntersectionTile = nextTile;
        }

        // only blinky starts outside the ghost house, and moves left first
        // the other 3 ghosts need to leave the ghost house first
        if (IsInGhostHouse)
        {
            _currentMoveDirection   = MoveDirection.Up;
            _targetIntersectionTile = _currentIntersectionTile.UpNeighbor;
        }
        else
        {
            _currentMoveDirection   = MoveDirection.Left;
            _targetIntersectionTile = ChooseNextTile();
        }

        _previousIntersectionTile = _currentIntersectionTile;
        _previousMoveSpeed        = NormalSpeed;
    }
Exemplo n.º 3
0
    /// <summary>
    /// Chooses which tile the Ghost needs to travel based on less distance overall
    /// </summary>
    /// <returns>The next IntersectionTile for the Ghost to travel</returns>
    private IntersectionTile ChooseNextTile()
    {
        IntersectionTile targetNode = null;
        var leastDistance           = 999999f;

        // gets the tile based on the TargetTile function
        var targetTile = GetGhostTargetTile();

        // searches for all possible tiles to travel and chooses the one that makes the overall travel shorter
        for (var i = 0; i < _currentIntersectionTile.Neighbors.Length; i++)
        {
            if (_currentIntersectionTile.NeighborDirections[i] != Vector3.zero &&
                _currentIntersectionTile.NeighborDirections[i] != Helpers.GetVectorDirection(_currentMoveDirection) * -1)
            {
                var distance = Helpers.GetDistanceBetweenVectors
                                   (_currentIntersectionTile.Neighbors[i].transform.localPosition, targetTile);

                if (distance < leastDistance)
                {
                    leastDistance         = distance;
                    targetNode            = _currentIntersectionTile.Neighbors[i];
                    _currentMoveDirection = Helpers.GetDirectionFromVector(_currentIntersectionTile.NeighborDirections[i]);
                }
            }
        }

        return(targetNode);
    }
Exemplo n.º 4
0
    /// <summary>
    /// Moves the player accordingly to the target tiles
    /// </summary>
    public void Move()
    {
        if (_targetIntersectionTile != _currentIntersectionTile && _targetIntersectionTile != null)
        {
            if (_nextMoveDirection == Helpers.GetOppositeDirection(_currentMoveDirection))
            {
                _currentMoveDirection = Helpers.GetOppositeDirection(_currentMoveDirection);
                var tempTile = _targetIntersectionTile;
                _targetIntersectionTile   = _previousIntersectionTile;
                _previousIntersectionTile = tempTile;
            }

            if (TargetWasOvershot())
            {
                _currentIntersectionTile = _targetIntersectionTile;
                transform.localPosition  = _currentIntersectionTile.transform.localPosition;

                if (_currentIntersectionTile.IsPortal)
                {
                    transform.localPosition  = _currentIntersectionTile.OppositePortal.transform.localPosition;
                    _currentIntersectionTile = _currentIntersectionTile.OppositePortal;
                }

                var nextTile = MoveToNeighbor(_nextMoveDirection);

                if (nextTile != null)
                {
                    _currentMoveDirection = _nextMoveDirection;
                }
                if (nextTile == null)
                {
                    nextTile = MoveToNeighbor(_currentMoveDirection);
                }
                if (nextTile != null)
                {
                    _targetIntersectionTile   = nextTile;
                    _previousIntersectionTile = _currentIntersectionTile;
                    _currentIntersectionTile  = null;
                }
                else
                {
                    _currentMoveDirection = MoveDirection.Stale;
                    _playerAnimator.speed = 0f;
                }
            }
            else
            {
                transform.localPosition += Helpers.GetVectorDirection(_currentMoveDirection) * Speed * Time.deltaTime;
                _playerAnimator.speed    = 1f;
            }
        }
    }
Exemplo n.º 5
0
    private void Awake()
    {
        _playerAnimator = GetComponentInChildren <Animator>();

        var nextTile = Helpers.GetIntersectionTile(transform.localPosition);

        if (nextTile != null)
        {
            _currentIntersectionTile = nextTile;
        }

        CurrentMoveOrientation = MoveDirection.Left;
        ChangePosition(MoveDirection.Left);
    }
Exemplo n.º 6
0
    /// <summary>
    /// Changes player position based on the next MoveDirection
    /// </summary>
    /// <param name="direction">The next MoveDirection to travel</param>
    private void ChangePosition(MoveDirection direction)
    {
        if (direction != _currentMoveDirection)
        {
            _nextMoveDirection = direction;
        }

        if (_currentIntersectionTile != null)
        {
            var nextTile = MoveToNeighbor(direction);
            if (nextTile != null)
            {
                _currentMoveDirection     = direction;
                _targetIntersectionTile   = nextTile;
                _previousIntersectionTile = _currentIntersectionTile;
                _currentIntersectionTile  = null;
            }
        }
    }