Пример #1
0
    IEnumerator UpdateSnake()
    {
        while (true)
        {
            // handle multi key presses
            if (InputHelper.GetStandardMoveMultiInputKeys())
            {
                Debug.Log("We are pressing multiple keys for direction");
                yield return(null);

                continue;
            }

            if (InputHelper.GetStandardMoveUpDirection())
            {
                mState = Direction.UP;
            }

            if (InputHelper.GetStandardMoveLeftDirection())
            {
                mState = Direction.LEFT;
            }

            if (InputHelper.GetStandardMoveDownDirection())
            {
                mState = Direction.DOWN;
            }

            if (InputHelper.GetStandardMoveRightDirection())
            {
                mState = Direction.RIGHT;
            }

            yield return(StartCoroutine(MoveSnake(mState)));

            // here we check for snake collision (it can only collide with itself)
            if (SnakeCollidedWithSelf() == true)
            {
                break;
            }

            yield return(new WaitForSeconds(moveDelay));
        }

        if (instance.SnakeCollidedWithSelf())
        {
            yield return(StartCoroutine(ScreenHelper.FlashDeathScreen(6, 0.1f, new Color(1, 0, 0, 0.5f))));

            SnakeGame.Instance.UpdateLives(-1);

            if (SnakeGame.Instance.gameLives == 0)
            {
                Application.LoadLevel("Snake");
            }
            else
            {
                Initialize();
                Start();
            }
        }
    }
Пример #2
0
    // ---------------------------------------------------------------------------------------------------
    // UpdateSnake()
    // ---------------------------------------------------------------------------------------------------
    // Our main player loop, this is what runs the logic for the snake, movement, food, adding segments
    // ---------------------------------------------------------------------------------------------------
    IEnumerator UpdateSnake()
    {
        while (true)
        {
            // handle multi key presses
            if (InputHelper.GetStandardMoveMultiInputKeys())
            {
                Debug.Log("We are pressing multiple keys for direction");
                yield return(null);

                continue;
            }

            // are we moving up
            if (InputHelper.GetStandardMoveUpDirection())
            {
                yield return(StartCoroutine(MoveSnake(Direction.UP)));
                //mState = Direction.UP;
                //Debug.Log ("We are moving UP");
            }

            // are we moving left
            if (InputHelper.GetStandardMoveLeftDirection())
            {
                yield return(StartCoroutine(MoveSnake(Direction.LEFT)));
                //mState = Direction.LEFT;
                //Debug.Log ("We are moving LEFT");
            }

            // are we moving down
            if (InputHelper.GetStandardMoveDownDirection())
            {
                yield return(StartCoroutine(MoveSnake(Direction.DOWN)));
                //mState = Direction.DOWN;
                //Debug.Log ("We are moving DOWN");
            }

            if (InputHelper.GetStandardMoveRightDirection())
            {
                yield return(StartCoroutine(MoveSnake(Direction.RIGHT)));
                //mState = Direction.RIGHT;
                //Debug.Log ("We are moving RIGHT");
            }

            // run the snake movement constant based on Direction
            // yield return StartCoroutine(MoveSnake(mState));

            // here we check for snake collision (it can only collide with itself)
            if (SnakeCollidedWithSelf() == true)
            {
                break;
            }

            yield return(new WaitForSeconds(moveDelay));
        }

        // play our death sound
        GetComponent <AudioSource>().clip = death;
        GetComponent <AudioSource>().Play();

        // we are hit
        yield return(StartCoroutine(ScreenHelper.FlashDeathScreen(6, 0.1f, new Color(1, 0, 0, 0.5f))));

        // we reduce number of lives
        SnakeGame.Instance.UpdateLives(-1);

        // check for playable lives left
        if (SnakeGame.Instance.gameLives == 0)
        {
            // reload the level, resetting the game
            Application.LoadLevel("Uni2DSnake");
        }
        else
        {
            // here we handle resetting the snake after a collision
            Initialize();

            // we have to call Start manually because this object is already Instantiated
            Start();
        }
    }
Пример #3
0
    public IEnumerator MoveSnake(Direction moveDirection)
    {
        // define a temp List of Rects to our current snakes List of Rects
        List <Rect> tempRects   = new List <Rect>();
        Rect        segmentRect = Food.Instance.foodPos;

        // initialize
        for (int i = 0; i < snakePos.Count; i++)
        {
            tempRects.Add(snakePos[i]);
        }

        switch (moveDirection)
        {
        case Direction.UP:
            if (snakePos[0].y > 94)
            {
                // we can move up
                snakePos[0] = new Rect(snakePos[0].x, snakePos[0].y - 20, snakePos[0].width, snakePos[0].height);

                // now update the rest of our body
                UpdateMovePosition(tempRects);

                // check for food
                if (CheckForFood() == true)
                {
                    // check for valid build segment position and add a segment

                    // create a temporary check position (this one is below the last segment in snakePos[])
                    segmentRect = CheckForValidDownPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

                        // give control back to our calling method
                        yield break;
                    }

                    // create a temporary check position (this one is to the left the last segment in snakePos[])
                    segmentRect = CheckForValidLeftPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

                        // give control back to our calling method
                        yield break;
                    }

                    // create a temporary check position (this one is to the right the last segment in snakePos[])
                    segmentRect = CheckForValidRightPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);
                    }

                    // no need to check Up, because we are pressing the Up key, we do not want a segment above us
                }
            }
            else
            {
                SnakeGame.Instance.UpdateLives(-1);
                snakeIcon.RemoveAt(0);
                if (SnakeGame.Instance.gameLives != 0)
                {
                    mState = Direction.LEFT;
                }
                else
                {
                    yield return(StartCoroutine(ScreenHelper.FlashDeathScreen(6, 0.1f, new Color(1, 0, 0, 0.5f))));

                    if (SnakeGame.Instance.gameLives == 0)
                    {
                        Application.LoadLevel("Snake");
                    }
                }
            }
            break;

        case Direction.LEFT:
            if (snakePos[0].x > 22)
            {
                // we can move left
                snakePos[0] = new Rect(snakePos[0].x - 20, snakePos[0].y, snakePos[0].width, snakePos[0].height);

                // now update the rest of our body
                UpdateMovePosition(tempRects);

                // check for food
                if (CheckForFood() == true)
                {
                    // check for valid build segment position and add a segment

                    // create a temporary check position (this one is to the right the last segment in snakePos[])
                    segmentRect = CheckForValidRightPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

                        // give control back to our calling method
                        yield break;
                    }

                    // create a temporary check position (this one is above the last segment in snakePos[])
                    segmentRect = CheckForValidUpPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

                        // give control back to our calling method
                        yield break;
                    }

                    // create a temporary check position (this one is below the last segment in snakePos[])
                    segmentRect = CheckForValidDownPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

                        // give control back to our calling method
                        yield break;
                    }

                    // no need to check Left, because we are pressing the Left key, we do not want a segment ahead of us
                }
            }
            else
            {
                SnakeGame.Instance.UpdateLives(-1);
                snakeIcon.RemoveAt(0);
                if (SnakeGame.Instance.gameLives != 0)
                {
                    mState = Direction.DOWN;
                }
                else
                {
                    yield return(StartCoroutine(ScreenHelper.FlashDeathScreen(6, 0.1f, new Color(1, 0, 0, 0.5f))));

                    if (SnakeGame.Instance.gameLives == 0)
                    {
                        Application.LoadLevel("Snake");
                    }
                }
            }
            break;

        case Direction.DOWN:
            if (snakePos[0].y < 670)
            {
                // we can move down
                snakePos[0] = new Rect(snakePos[0].x, snakePos[0].y + 20, snakePos[0].width, snakePos[0].height);

                // now update the rest of our body
                UpdateMovePosition(tempRects);

                // check for food
                if (CheckForFood() == true)
                {
                    // check for valid build segment position and add a segment

                    // create a temporary check position (this one is above the last segment in snakePos[])
                    segmentRect = CheckForValidUpPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

                        // give control back to our calling method
                        yield break;
                    }

                    // create a temporary check position (this one is to the left the last segment in snakePos[])
                    segmentRect = CheckForValidLeftPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

                        // give control back to our calling method
                        yield break;
                    }

                    // create a temporary check position (this one is to the right the last segment in snakePos[])
                    segmentRect = CheckForValidRightPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);
                    }

                    // no need to check Down, because we are pressing the Down key, we do not want a segment below us
                }
            }
            else
            {
                SnakeGame.Instance.UpdateLives(-1);
                snakeIcon.RemoveAt(0);

                if (SnakeGame.Instance.gameLives != 0)
                {
                    mState = Direction.RIGHT;
                }
                else
                {
                    yield return(StartCoroutine(ScreenHelper.FlashDeathScreen(6, 0.1f, new Color(1, 0, 0, 0.5f))));

                    if (SnakeGame.Instance.gameLives == 0)
                    {
                        Application.LoadLevel("Snake");
                    }
                }
            }
            break;

        case Direction.RIGHT:
            if (snakePos[0].x < 982)
            {
                // we can move right
                snakePos[0] = new Rect(snakePos[0].x + 20, snakePos[0].y, snakePos[0].width, snakePos[0].height);

                // now update the rest of our body
                UpdateMovePosition(tempRects);

                // check for food
                if (CheckForFood() == true)
                {
                    // check for valid build segment position and add a segment

                    // create a temporary check position (this one is left of the last segment in snakePos[])
                    segmentRect = CheckForValidLeftPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

                        // give control back to our calling method
                        yield break;
                    }

                    // create a temporary check position (this one is to the left the last segment in snakePos[])
                    segmentRect = CheckForValidUpPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);

                        // give control back to our calling method
                        yield break;
                    }

                    // create a temporary check position (this one is below the last segment in snakePos[])
                    segmentRect = CheckForValidDownPosition();
                    if (segmentRect.x != 0)
                    {
                        // we build another segment passing the Rect as an argument
                        BuildSnakeSegment(segmentRect);

                        // increment our snake length
                        snakeLength++;

                        // decrement our moveDelay
                        moveDelay = Mathf.Max(0.05f, moveDelay - 0.01f);
                    }

                    // no need to check Right, because we are pressing the Right key, we do not want a segment ahead of us
                }
            }
            else
            {
                SnakeGame.Instance.UpdateLives(-1);
                snakeIcon.RemoveAt(0);

                if (SnakeGame.Instance.gameLives != 0)
                {
                    mState = Direction.UP;
                }
                else
                {
                    yield return(StartCoroutine(ScreenHelper.FlashDeathScreen(6, 0.1f, new Color(1, 0, 0, 0.5f))));

                    if (SnakeGame.Instance.gameLives == 0)
                    {
                        Application.LoadLevel("Snake");
                    }
                }
            }
            break;
        }

        yield return(null);
    }