示例#1
0
        /// <summary>
        /// Public override method which is launched at each frame.
        /// </summary>
        public override void Update()
        {
            // Gets the position in Y of the indicator
            y = position.Pos.Y;

            /* Sees what key is pressed and depending of the keys pressed
             * the indicator can go up and down or initiate another scene*/
            foreach (ConsoleKey key in keyObserver.GetCurrentKeys())
            {
                if (key == ConsoleKey.UpArrow && option != 0)
                {
                    y -= 2;
                    option--;
                }
                else if (key == ConsoleKey.DownArrow && option != 3)
                {
                    y += 2;
                    option++;
                }
                else if (key == ConsoleKey.Enter)
                {
                    if (option == 0)
                    {
                        ParentScene.Terminate();
                        Level1 level1 = new Level1();
                        level1.Run();
                    }

                    if (option == 1)
                    {
                        ParentScene.Terminate();
                        Level2 level2 = new Level2();
                        level2.Run();
                    }
                    else if (option == 2)
                    {
                        ParentScene.Terminate();
                        Help helpMenu = new Help();
                        helpMenu.Run();
                    }
                    else
                    {
                        ParentScene.Terminate();
                    }
                }
            }

            // The position of the gameobject actualizes with the new values
            position.Pos = new Vector3(x, y, position.Pos.Z);
        }
示例#2
0
        /// <summary>
        /// Public override method which is launched at each frame.
        /// </summary>
        public override void Update()
        {
            bool ground;

            x = position.Pos.X;
            y = position.Pos.Y;
            bool colide = false;

            // Sets Gameover to true when the player reaches the end of the
            // level.
            if (ParentScene.xdim - 9 == position.Pos.X && !Gameover)
            {
                Gameover = true;
            }

            if (Gameover)
            {
                // Sets the correct keys to observe
                ParentGameObject.GetComponent <KeyObserver>().keysToObserve =
                    new ConsoleKey[] { ConsoleKey.Enter, ConsoleKey.Escape };
                ParentScene.inputHandler.quitKeys = new ConsoleKey[] {
                    ConsoleKey.Enter,
                    ConsoleKey.Escape,
                };
                ParentScene.inputHandler.RegisterObserver(
                    ParentGameObject
                    .GetComponent <KeyObserver>()
                    .keysToObserve, ParentGameObject
                    .GetComponent <KeyObserver>());

                // Runs if player falls of the map.
                if (ParentScene.ydim - 4 == position.Pos.Y)
                {
                    // Outputs the dead text
                    dead.GetComponent <RenderableStringComponent>()
                    .SwitchString(() => "Press Enter to restart");

                    // Waits for player input to go to the next level or
                    // back to the main menu.
                    foreach (ConsoleKey key in keyObserver.GetCurrentKeys())
                    {
                        if (key == ConsoleKey.Enter)
                        {
                            if (level == 1)
                            {
                                ParentScene.Terminate();
                                Level1 level1 = new Level1();
                                level1.Run();
                                break;
                            }
                            else
                            {
                                ParentScene.Terminate();
                                Level2 level2 = new Level2();
                                level2.Run();
                                break;
                            }
                        }

                        if (key == ConsoleKey.Escape)
                        {
                            ParentScene.Terminate();
                            Menu menu = new Menu();
                            menu.Run();
                            break;
                        }
                    }
                }

                // Runs if player reaches the end of the level.
                else if (ParentScene.xdim - 9 == position.Pos.X)
                {
                    // Outputs the ending level text
                    dead.GetComponent <RenderableStringComponent>()
                    .SwitchString(() =>
                                  $"Press Enter to go to the next level, your score is : {actualScore.Scoring}");

                    // Waits for player input to go to the next level or back
                    // to the main menu.
                    foreach (ConsoleKey key in keyObserver.GetCurrentKeys())
                    {
                        if (key == ConsoleKey.Enter)
                        {
                            if (level == 1)
                            {
                                ParentScene.Terminate();
                                Level2 level2 = new Level2();
                                level2.Run();
                                break;
                            }
                            else
                            {
                                ParentScene.Terminate();
                                Menu menu = new Menu();
                                menu.Run();
                                break;
                            }
                        }

                        if (key == ConsoleKey.Escape)
                        {
                            ParentScene.Terminate();
                            Menu menu = new Menu();
                            menu.Run();
                            break;
                        }
                    }
                }
            }
            else
            {
                // Checks if the player is on the ground
                ground = CheckGround();

                // If he is not jumping and not in the ground, the player
                // will then starts to fall
                if (!inAir && !ground)
                {
                    while (!ground)
                    {
                        Falling();

                        // Removes the keyObserver from the player.
                        ParentScene.inputHandler
                        .RemoveObserver(ParentGameObject
                                        .GetComponent <KeyObserver>());

                        doesNotHaveKeyObserver = true;
                        ground = true;
                    }
                }

                // If he is in the ground, then he can walk and jump as normal
                else if (!inAir)
                {
                    if (doesNotHaveKeyObserver)
                    {
                        // Adds the keyObserver to the player.
                        ParentScene.inputHandler
                        .AddObserver(ParentGameObject
                                     .GetComponent <KeyObserver>());
                        doesNotHaveKeyObserver = false;
                    }

                    // Controls the movement of the player.
                    foreach (ConsoleKey key in keyObserver.GetCurrentKeys())
                    {
                        switch (key)
                        {
                        case ConsoleKey.RightArrow:
                            lastkey = ConsoleKey.RightArrow;
                            foreach (Vector2 v in occupied)
                            {
                                if (position.Pos.X + 7 == v.X &&
                                    position.Pos.Y == v.Y)
                                {
                                    colide = true;
                                }
                            }

                            if (!colide)
                            {
                                x++;
                            }

                            // Map limits.
                            x            = Math.Clamp(x, 0, ParentScene.xdim - 9);
                            y            = Math.Clamp(y, 0, ParentScene.ydim - 3);
                            position.Pos = new Vector3(
                                x,
                                y,
                                position.Pos.Z);
                            TurnSprite(true);
                            break;

                        case ConsoleKey.UpArrow:
                            lastkey      = ConsoleKey.UpArrow;
                            position.Pos = new Vector3(
                                x,
                                y,
                                position.Pos.Z);
                            break;

                        case ConsoleKey.LeftArrow:
                            lastkey = ConsoleKey.LeftArrow;
                            foreach (Vector2 v in occupied)
                            {
                                if (position.Pos.X - 1 == v.X &&
                                    position.Pos.Y == v.Y)
                                {
                                    colide = true;
                                }
                            }

                            if (!colide)
                            {
                                x--;
                            }

                            // Map limits.
                            x            = Math.Clamp(x, 0, ParentScene.xdim - 9);
                            y            = Math.Clamp(y, 0, ParentScene.ydim - 3);
                            position.Pos = new Vector3(
                                x,
                                y,
                                position.Pos.Z);
                            TurnSprite(false);
                            break;

                        case ConsoleKey.Spacebar:
                            inAir        = true;
                            position.Pos = new Vector3(
                                x,
                                y,
                                position.Pos.Z);
                            break;

                        case ConsoleKey.Escape:
                            ParentScene.Terminate();
                            Menu menu = new Menu();
                            menu.Run();
                            break;
                        }
                    }

                    // Map limits.
                    x = Math.Clamp(x, 0, ParentScene.xdim - 9);
                    y = Math.Clamp(y, 0, ParentScene.ydim - 3);
                }

                // Player Jump.
                else if (inAir)
                {
                    actualScore.Scoring -= 5;
                    ParentScene.inputHandler.RemoveObserver(
                        ParentGameObject.GetComponent <KeyObserver>());
                    if (jumpFrames <= 2)
                    {
                        y -= 2;
                        if (lastkey == ConsoleKey.RightArrow)
                        {
                            x += 3;
                        }
                        else if (lastkey == ConsoleKey.LeftArrow)
                        {
                            x -= 3;
                        }
                        jumpFrames++;
                    }
                    else if (jumpFrames == 3)
                    {
                        y -= 2;
                        if (lastkey == ConsoleKey.RightArrow)
                        {
                            x += 3;
                        }
                        else if (lastkey == ConsoleKey.LeftArrow)
                        {
                            x -= 3;
                        }
                        jumpFrames = 0;
                        inAir      = false;
                    }

                    // Map limits.
                    x            = Math.Clamp(x, 0, ParentScene.xdim - 9);
                    y            = Math.Clamp(y, 0, ParentScene.ydim - 3);
                    position.Pos = new Vector3(x, y, position.Pos.Z);
                    Thread.Sleep(80);
                }

                if (coins != null)
                {
                    coinScore = CheckCoin();
                }
                if (coinScore)
                {
                    actualScore.Scoring += 1000;
                    coinScore            = false;
                }
            }
        }