コード例 #1
0
        public GameplayScreen()
        {
            TransitionOnTime  = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            time_passed       = 0.0f;
            total_time_passed = 0.0f;

            currentLevel = 1;

            InitializeLevels();

            // sounds
            m_player_wall = SoundMaster.GetSound("jump_thud");

            // Initialize Player.
            mPlayer     = new Player(GetLevel(currentLevel).LevelStart, Vector3.Zero);
            mPlayer.Yaw = GetLevel(currentLevel).LevelStartDirection;
            mPlayer.PlayerCamera.Orientation = Quaternion.CreateFromAxisAngle(Vector3.Up, GetLevel(currentLevel).LevelStartDirection);

            // Initialize input device states.
            keyboardState    = Keyboard.GetState();
            gamepadState     = GamePad.GetState(PlayerIndex.One);
            oldKeyboardState = keyboardState;
            oldGamepadState  = gamepadState;
        }
コード例 #2
0
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
            bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (coveredByOtherScreen)
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            else
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);

            if (IsActive)
            {
                // Get new input states.
                keyboardState = Keyboard.GetState();
                gamepadState  = GamePad.GetState(PlayerIndex.One);

                #region Controls
                #region Keyboard
                /* Space */

                // Jump.
                if (keyboardState.IsKeyDown(Keys.Space) && oldKeyboardState.IsKeyUp(Keys.Space))
                    mPlayer.Jump();

                /* WASD */

                // Move forward.
                if (keyboardState.IsKeyDown(Keys.W))
                    mPlayer.Move(0.0f, 1.0f);

                // Move backward.
                if (keyboardState.IsKeyDown(Keys.S))
                    mPlayer.Move(0.0f, -1.0f);

                // Move left.
                if (keyboardState.IsKeyDown(Keys.A))
                    mPlayer.Move(-1.0f, 0.0f);

                // Move right.
                if (keyboardState.IsKeyDown(Keys.D))
                    mPlayer.Move(1.0f, 0.0f);

                /* Arrow Keys */

                // Look up.
                if (keyboardState.IsKeyDown(Keys.Up))
                    mPlayer.AdjustPitch(1.0f);

                // Look down.
                if (keyboardState.IsKeyDown(Keys.Down))
                    mPlayer.AdjustPitch(-1.0f);

                // Look left.
                if (keyboardState.IsKeyDown(Keys.Left))
                    mPlayer.AdjustYaw(-1.0f);

                // Look right.
                if (keyboardState.IsKeyDown(Keys.Right))
                    mPlayer.AdjustYaw(1.0f);

                /* I */

                // Interact
                if(keyboardState.IsKeyDown(Keys.I) && oldKeyboardState.IsKeyUp(Keys.I))
                    GetLevel(currentLevel).Interact(mPlayer.Bound, gameTime);

                /* T */

                // Throw
                if(keyboardState.IsKeyDown(Keys.T) && oldKeyboardState.IsKeyUp(Keys.T))
                    mPlayer.Throw(gameTime);

                // Display Backpack
                if ((keyboardState.IsKeyDown(Keys.B) && oldKeyboardState.IsKeyUp(Keys.B)))
                    drawBackpack = true;
                else
                    drawBackpack = false;
                #endregion

                #region Controller
                if (gamepadState.IsConnected)
                {
                    /* Right Shoulder or A Button */

                    // Jump.
                    if (((gamepadState.Buttons.RightShoulder == ButtonState.Pressed) && (oldGamepadState.Buttons.RightShoulder == ButtonState.Released))
                        || ((gamepadState.Buttons.A == ButtonState.Pressed) && (oldGamepadState.Buttons.A == ButtonState.Released)))
                        mPlayer.Jump();

                    /* Y Button */

                    // Display Backpack
                    if ((gamepadState.Buttons.Y == ButtonState.Pressed))
                        drawBackpack = true;
                    else
                        drawBackpack = false;

                    /* B Button */

                    // Throw Projectile.
                    if ((gamepadState.Buttons.B == ButtonState.Pressed) && (oldGamepadState.Buttons.B == ButtonState.Released))
                        mPlayer.Throw(gameTime);

                    /* X Button */

                    // Interact.
                    if ((gamepadState.Buttons.X == ButtonState.Pressed) && (oldGamepadState.Buttons.X == ButtonState.Released))
                        GetLevel(currentLevel).Interact(mPlayer.Bound, gameTime);

                    /* Left Shoulder Button */

                    // Complete current Level.
                    if ((gamepadState.Buttons.LeftShoulder == ButtonState.Pressed) && (oldGamepadState.Buttons.LeftShoulder == ButtonState.Released))
                        GetLevel(currentLevel).IsCompleted = true;

                    /* Right Joystick */

                    // Rotate.
                    mPlayer.AdjustYaw(gamepadState.ThumbSticks.Right.X);
                    mPlayer.AdjustPitch(gamepadState.ThumbSticks.Right.Y);

                    /* Left Joystick */

                    // Move.
                    mPlayer.Move(gamepadState.ThumbSticks.Left.X, gamepadState.ThumbSticks.Left.Y);
                }
                #endregion
                #endregion

                #region Collision Detection
                // Update Player.
                mPlayer.Update(gameTime);

                /* Collision detection */

                BoundingBox BoundHit = new BoundingBox();
                if (GetLevel(currentLevel).HitWall(mPlayer.Bound, ref BoundHit))
                {
                    // Create contact point. Radius = 100 for player wall collisions
                    PointManagerRemake.add_Point(new Vector4(mPlayer.Position, 0.0f), gameTime.TotalGameTime.Milliseconds, 100.0f);

                    /* Compute collision normal. */

                    Vector3 vNorm = new Vector3();

                    if (mPlayer.Position.X <= BoundHit.Min.X)
                        vNorm = Vector3.Left;
                    if (mPlayer.Position.X > BoundHit.Max.X)
                        vNorm = Vector3.Right;
                    if (mPlayer.Position.Z <= BoundHit.Min.Z)
                        vNorm = Vector3.Forward;
                    if (mPlayer.Position.Z > BoundHit.Max.Z)
                        vNorm = Vector3.Backward;

                    // Get vector projection.
                    Vector3 vProj = Vector3.Dot(mPlayer.Velocity, vNorm) * vNorm;
                    vProj.Normalize();

                    // Store Player position.
                    Vector3 oldPos = mPlayer.Position;

                    // Revert Player position.
                    Vector3 newPos = mPlayer.Position + (2.0f) * vNorm;
                    newPos.Y = mPlayer.Position.Y;
                    mPlayer.Position = newPos;

                    // Update Player velocity.
                    mPlayer.Velocity = (-1.0f) * vProj * (new Vector3(mPlayer.Velocity.X, 0.0f, mPlayer.Velocity.Z).Length());

                    // Update Camera position.
                    mPlayer.PlayerCamera.Move(newPos - oldPos);

                    // play sound effect for player
                    m_player_wall.Play();
                }
                #endregion

                if (beginGame)
                {
                    ScreenManager.AddScreen(new FinishLevelScreen("begin", true), ControllingPlayer);
                    ScreenManager.AddScreen(new FinishLevelScreen("begin2", true), ControllingPlayer);
                    ScreenManager.AddScreen(new FinishLevelScreen("begin3", true), ControllingPlayer);
                    beginGame = false;
                }

                #region Level Complete
                // Level Completed.
                if (GetLevel(currentLevel).IsCompleted)
                {
                    ScreenManager.AddScreen(new FinishLevelScreen(currentLevel.ToString(), true), ControllingPlayer);
                    CleanUp();
                    currentLevel++;
                    PointManagerRemake.ClearAll();

                    if (finishGame)
                    {
                        this.ExitScreen();
                        ScreenManager.AddScreen(new BackgroundScreen(), null);
                        ScreenManager.AddScreen(new MainMenuScreen(), null);
                    }

                    if (currentLevel > NUMLEVELS)
                    {
                        currentLevel = NUMLEVELS;
                        finishGame = true;
                    }

                    mPlayer                          = new Player(Vector3.Zero, Vector3.Zero);
                    mPlayer.Position                 = GetLevel(currentLevel).LevelStart;
                    mPlayer.PlayerCamera.Position    = mPlayer.Position;
                    mPlayer.Yaw                      = GetLevel(currentLevel).LevelStartDirection;
                    mPlayer.PlayerCamera.Orientation = Quaternion.CreateFromAxisAngle(Vector3.Up, GetLevel(currentLevel).LevelStartDirection);

                    mPlayer.PlayerCamera.Update();
                }
                #endregion

                #region Game Over
                // Game Over.
                if (GetLevel(currentLevel).Update(mPlayer.Bound, gameTime))
                {
                    Exception ex = new Exception("add level to switch statement");

                    CleanUp();

                    ScreenManager.AddScreen(new FinishLevelScreen("Fail", true), ControllingPlayer);
                    switch (currentLevel)
                    {
                        case 1:
                            InitializeLevel_01();
                            break;
                        case 2:
                            InitializeLevel_02();
                            break;
                        case 3:
                            InitializeLevel_03();
                            break;
                        case 4:
                            InitializeLevel_04();
                            break;
                        case 5:
                            InitializeLevel_05();
                            break;
                        case 6:
                            InitializeLevel_06();
                            break;
                        case 7:
                            InitializeLevel_07();
                            break;
                        default:
                            throw ex;
                    }

                    mPlayer                          = new Player(Vector3.Zero, Vector3.Zero);
                    mPlayer.Position                 = GetLevel(currentLevel).LevelStart;
                    mPlayer.PlayerCamera.Position    = mPlayer.Position;
                    mPlayer.Yaw                      = GetLevel(currentLevel).LevelStartDirection;
                    mPlayer.PlayerCamera.Orientation = Quaternion.CreateFromAxisAngle(Vector3.Up, GetLevel(currentLevel).LevelStartDirection);

                    mPlayer.PlayerCamera.Update();
                }
                    //LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(), new MainMenuScreen());
                #endregion

                #region Point Manager
                // Update Point Manager.
                float temp = gameTime.TotalGameTime.Milliseconds;
                if ((temp - time_passed) > 0)
                {
                    total_time_passed += (temp - time_passed);
                    PointManagerRemake.updateTime(temp - time_passed);
                }
                else
                {
                    total_time_passed += temp;
                    PointManagerRemake.updateTime(temp);
                }
                #endregion

                // Set old input states.
                oldKeyboardState = keyboardState;
                oldGamepadState  = gamepadState;
            }
        }