Exemplo n.º 1
0
 public void changeSprite(Vector2 position, Vector2 targetPosition)
 {
     if (level <= 1)
     {
         walkCycle.changeSprite(position, targetPosition);
     }
     else if (level >= 2)
     {
         advancedWalk.changeSprite(position, targetPosition);
     }
 }
    private void FixedUpdate()
    {
        /* !! OUTDATED !! Since 21/12/-20
         *
         * First we get player's position and then calculate targetPosition with RaycastHit.
         * Then we compare whether is hits collider and then if collider is tagged with "Wall"
         *
         * if it hits and player hasn´t hit it over 100 times, will player be set back at last saved position.
         * if it hits and player has hit "wall" tagged collider over 100, will player be returned to starting position.
         *
         * This is to prevent from player getting stuck and need to boot the game.
         *
         * Then if targetPosition doesn´t hit the collider which is tagged as "Wall", we check if player has been Freezed.
         * If that´s not the case, player may move to the target point.
         * At this point our Walk class object is called to change the sprite for the right direction.
         *
         * Then lastly we have timer that saves new last savePosition every 10 ms.
         */

        if (!freeze)
        {
            Vector2 position       = getPosition();
            Vector2 targetPosition = position;
            targetPosition.x = position.x + speed * horizontal;
            targetPosition.y = position.y + speed * vertical;

            float x = count.countDiversition(targetPosition.x, position.x);
            float y = count.countDiversition(targetPosition.y, position.y);

            RaycastHit2D raycast = Physics2D.Raycast(position, targetPosition, 1);
            if (raycast.collider != null && raycast.collider.CompareTag("Wall"))
            {
                Vector3 nextPos = position;

                /* Recognise the wall. */

                if (x > y && firstXHit != true)
                {
                    /* X-wall */
                    firstXHit = true;

                    if (targetPosition.x > position.x)
                    {
                        xRight = true;
                    }
                    else
                    {
                        xLeft = true;
                    }
                }

                if (y > x && firstYHit != true)
                {
                    /* Y-wall. */
                    firstYHit = true;

                    if (targetPosition.y > position.y)
                    {
                        yUp = true;
                    }
                    else
                    {
                        yDown = true;
                    }
                }

                /* If wall is... Allow moving other than towards wall */

                if (xRight)
                {
                    if (firstYHit)
                    {
                        if (yUp)
                        {
                            if (targetPosition.y < nextPos.y)
                            {
                                nextPos.y = targetPosition.y;
                            }
                        }
                        else if (yDown)
                        {
                            if (targetPosition.y > nextPos.y)
                            {
                                nextPos.y = targetPosition.y;
                            }
                        }
                    }

                    if (targetPosition.x < nextPos.x)
                    {
                        nextPos.x = targetPosition.x;
                    }
                    walk.changeSprite(nextPos, position);
                    rigidbody2d.MovePosition(nextPos);
                }
                else if (xLeft)
                {
                    if (firstYHit)
                    {
                        if (yUp)
                        {
                            if (targetPosition.y < nextPos.y)
                            {
                                nextPos.y = targetPosition.y;
                            }
                        }
                        else if (yDown)
                        {
                            if (targetPosition.y > nextPos.y)
                            {
                                nextPos.y = targetPosition.y;
                            }
                        }
                    }

                    if (targetPosition.x > nextPos.x)
                    {
                        nextPos.x = targetPosition.x;
                    }
                    walk.changeSprite(nextPos, position);
                    rigidbody2d.MovePosition(nextPos);
                }
                else if (yUp)
                {
                    if (firstXHit)
                    {
                        if (xLeft)
                        {
                            if (targetPosition.x > nextPos.x)
                            {
                                nextPos.x = targetPosition.x;
                            }
                        }
                        else if (xRight)
                        {
                            if (targetPosition.x < nextPos.x)
                            {
                                nextPos.x = targetPosition.x;
                            }
                        }
                    }

                    if (targetPosition.y < nextPos.y)
                    {
                        nextPos.y = targetPosition.y;
                    }
                    walk.changeSprite(nextPos, position);
                    rigidbody2d.MovePosition(nextPos);
                }
                else if (yDown)
                {
                    if (firstXHit)
                    {
                        if (xLeft)
                        {
                            if (targetPosition.x > nextPos.x)
                            {
                                nextPos.x = targetPosition.x;
                            }
                        }
                        else if (xRight)
                        {
                            if (targetPosition.x < nextPos.x)
                            {
                                nextPos.x = targetPosition.x;
                            }
                        }
                    }

                    if (targetPosition.y > nextPos.y)
                    {
                        nextPos.y = targetPosition.y;
                    }
                    walk.changeSprite(nextPos, position);
                    rigidbody2d.MovePosition(nextPos);
                }
            }
            else
            {
                if (raycast.collider == null || (raycast.collider != null && !(raycast.collider.CompareTag("Wall"))))
                {
                    xRight    = false; xLeft = false;
                    yUp       = false; yDown = false;
                    firstYHit = false;
                    firstXHit = false;
                }

                Vector3 temp = position;

                if (x > y)
                {
                    temp.x = targetPosition.x;
                    rigidbody2d.MovePosition(temp);
                }
                else if (x < y)
                {
                    temp.y = targetPosition.y;
                    rigidbody2d.MovePosition(temp);
                }

                walk.changeSprite(targetPosition, position);
            }
        }
    }