Esempio n. 1
0
    public void Update()
    {
        if (_bullet == null || !_bullet.gameObject.activeSelf)
        {
            return;
        }

        if (_canMove && Vector3.Distance(_bullet.position, _targetPosition) < 0.001f)
        {
            _canMove         = false;
            _targetPosition += Vector3.up;

            IntVector2 position = new IntVector2((int)_targetPosition.x, (int)_targetPosition.y);

            if (position.y >= _gridMaxHeight)
            {
                DestroyBullet();
                return;
            }

            if (_levelManager.IsCellOccupiedByMushroom(position))
            {
                DestroyBullet();
                _mushroomManager.DamageMushroomAtPosition(position, _damage);
                return;
            }

            if (_levelManager.IsCellOccupiedBySpider(position))
            {
                DestroyBullet();
                _spiderManager.DestroySpider(position);
                return;
            }

            if (_levelManager.IsCellOccupiedByCentipede(position))
            {
                DestroyBullet();
                _centipedeManager.DestroyBodyPart(position);
                return;
            }

            MainApp.Instance.StartCoroutine(Move(1f / _speed));
        }
    }
Esempio n. 2
0
    public void UpdatePosition(Action action)
    {
        if (_head == null)
        {
            Debug.LogWarning("head is null");
            return;
        }

        if (_canMove && Vector3.Distance(_head.transform.position, _targetPosition) < 0.001f)
        {
            _onHeadMove = action;
            _canMove    = false;

            Vector3 newPosition = _targetPosition + Vector3.right * (int)HorizontalDirection;

            IntVector2 position = new IntVector2((int)newPosition.x, (int)newPosition.y);

            if (position.x > _gridSize.x - 1)
            {
                if (position.y >= _gridSize.y - 1 || position.y <= 0)
                {
                    VerticalDirection = VerticalDirection == VerticalDirection.Up
                        ? VerticalDirection.Down
                        : VerticalDirection.Up;
                }

                newPosition.x       = _gridSize.x - 1;
                newPosition        += Vector3.up * (int)VerticalDirection;
                HorizontalDirection = HorizontalDirection == HorizontalDirection.Left ? HorizontalDirection.Right : HorizontalDirection.Left;
            }

            if (position.x < 0)
            {
                if (position.y >= _gridSize.y - 1)
                {
                    VerticalDirection = VerticalDirection.Down;
                }
                else if (position.y <= 0)
                {
                    VerticalDirection = VerticalDirection.Up;
                }

                newPosition.x       = 0;
                newPosition        += Vector3.up * (int)VerticalDirection;
                HorizontalDirection = HorizontalDirection == HorizontalDirection.Left ? HorizontalDirection.Right : HorizontalDirection.Left;
            }

            if (_levelManager.IsCellOccupiedByMushroom(new IntVector2((int)newPosition.x, (int)newPosition.y)) ||
                _levelManager.IsCellOccupiedByCentipede(new IntVector2((int)newPosition.x, (int)newPosition.y)))
            {
                newPosition.x = _targetPosition.x;
                Vector3 cellPos = newPosition + Vector3.up * (int)VerticalDirection;
                if (cellPos.y >= _gridSize.y - 1 || cellPos.y <= 0)
                {
                    VerticalDirection = VerticalDirection == VerticalDirection.Up
                        ? VerticalDirection.Down
                        : VerticalDirection.Up;
                    newPosition += Vector3.up * (int)VerticalDirection;
                }
                else
                {
                    newPosition = cellPos;
                }

                HorizontalDirection = HorizontalDirection == HorizontalDirection.Left ? HorizontalDirection.Right : HorizontalDirection.Left;
            }

            if (_levelManager.IsCellOccupiedByPlayer(new IntVector2((int)newPosition.x, (int)newPosition.y)))
            {
                ReducePlayerLife();
            }

            _targetPosition = newPosition;
            MainApp.Instance.StartCoroutine(MoveHead(1f / _speed));
        }
    }