Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (MovementAxis == MovementAxis.None || isStoped)
        {
            return;
        }

        var movementBeforeStop = MovementDirection;
        var newPositionInfo    = MovementUtils.GetPosition(transform, MovementDirection, EnemySpeed, positionOnPreviousFrame);

        if (newPositionInfo.nextTile?.TileType == TileType.Wall)
        {
            isStoped = true;
            var animationSequence = MovementUtils.DoWallCollisionAnimation(MovementDirection, transform, wallCollisionAnimationSpeed);
            animationSequence[0].AppendCallback(() => isStoped = false);
            WallCollisionEvent(gameObject);
            ChooseNextDirection(movementBeforeStop);
        }
        else
        {
            transform.position = newPositionInfo.newPosition;
        }

        positionOnPreviousFrame = transform.position;
    }
    private void MovementWithChangeDirection()
    {
        var movementDirectionFromInput = SwipeDetector.GetDirectionFromSwipes();

        if (movementDirectionFromInput != Vector3.zero)
        {
            if (MovementDirection != Vector3.zero)
            {
                transform.position = new Vector3(Mathf.Round(transform.position.x), Mathf.Round(transform.position.y), Mathf.Round(transform.position.z));
            }
            MovementDirection = movementDirectionFromInput;
        }


        if (MovementDirection == Vector3.zero)
        {
            return;
        }

        if (animationSequence.Any())
        {
            animationSequence.ForEach(a => a.Kill());
            animationSequence.Clear();

            transform.localScale = Vector3.one;
            transform.position   = new Vector3(Mathf.Round(transform.position.x), Mathf.Round(transform.position.y), Mathf.Round(transform.position.z));
        }

        var newPositionInfo = MovementUtils.GetPosition(transform, MovementDirection, PlayerSpeed, positionOnPreviousFrame);

        transform.position = newPositionInfo.newPosition;
        if (newPositionInfo.nextTile?.TileType == TileType.Wall)
        {
            if (MovementUtils.CheckSpikesCollision(newPositionInfo.nextTile, MovementDirection))
            {
                //var camera = GamePlayPrefabsSettings.Prefabs.MainCamera;
                //camera.transform.SetParent(null);
                GameplaySettings.MainCamera.transform.SetParent(null);
                Destroy(transform.gameObject);
            }
            else
            {
                animationSequence = MovementUtils.DoWallCollisionAnimation(MovementDirection, transform, wallCollisionAnimationSpeed);
                animationSequence[0].AppendCallback(() => animationSequence.Clear());

                MovementDirection = Vector3.zero;
                WallCollisionEvent(gameObject);
            }
        }

        positionOnPreviousFrame = transform.position;
    }