Beispiel #1
0
        public void CreateSmoke(Vector2 pos)
        {
            SmokeParticle ex = new SmokeParticle(this, smokeParticleTexture, pos);

            gameObjects.Add(ex);
        }
Beispiel #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            switch (State)
            {
            case GameState.LEVEL:
                physicsWorld.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);

                #region Mouse Inputs
                if (Mouse.GetState().ScrollWheelValue < mouseWheelLoc)
                {
                    player.CycleNextWeapon();
                }
                else if (Mouse.GetState().ScrollWheelValue > mouseWheelLoc)
                {
                    player.CyclePreviousWeapon();
                }

                mouseWheelLoc = Mouse.GetState().ScrollWheelValue;
                #endregion

                #region Camera
                int Y_CAMERA_THRESHOLD = (ScreenHeight / 2) + 100;
                //X movement
                if (ConvertUnits.ToDisplayUnits(player.Position.X) > (-1 * (gameCamera.Position.X)) + ScreenWidth - X_CAMERA_THRESHOLD)
                {
                    gameCamera.Move(new Vector2(-1 * (ConvertUnits.ToDisplayUnits(player.Position.X) - (-1 * (gameCamera.Position.X) + ScreenWidth - X_CAMERA_THRESHOLD)), 0));
                }

                else if (ConvertUnits.ToDisplayUnits(player.Position.X) < (-1 * (gameCamera.Position.X)) + X_CAMERA_THRESHOLD)
                {
                    gameCamera.Move(new Vector2(-1 * (ConvertUnits.ToDisplayUnits(player.Position.X) - (-1 * (gameCamera.Position.X) + X_CAMERA_THRESHOLD)), 0));
                }

                //Y movement
                float playerY = ConvertUnits.ToDisplayUnits(player.Position.Y);

                if (playerY > gameCamera.Position.Y + 700 && gameCamera.Position.Y > 0)
                {
                    gameCamera.Move(new Vector2(0, -1 * (ConvertUnits.ToDisplayUnits(player.Position.Y) - (-1 * (gameCamera.Position.Y) + 400))));
                }
                else if (playerY < gameCamera.Position.Y + Y_CAMERA_THRESHOLD + 100)
                {
                    gameCamera.Move(new Vector2(0, -1 * (ConvertUnits.ToDisplayUnits(player.Position.Y) - (-1 * (gameCamera.Position.Y) + Y_CAMERA_THRESHOLD))));
                }

                CameraOffset = gameCamera.Position;
                #endregion

                #region Keyboard inputs

                if (player.LinearVelocity.Y != 0)
                {
                    player.State = PlayerState.JUMPING;
                }
                else if (Keyboard.GetState().IsKeyDown(Keys.D) || Keyboard.GetState().IsKeyDown(Keys.A))
                {
                    player.State = PlayerState.RUNNING;
                }
                else
                {
                    player.State = PlayerState.IDLE;
                }

                //TODO: fix ground colloision detection
                if (Keyboard.GetState().IsKeyDown(Keys.Space) && player.LinearVelocity.Y == 0)
                {
                    MediaPlayerHelper.MediaPlayerHelper.Instance.PlaySound(JumpEffect);
                    player.ApplyForce(new Vector2(0, -200.0f));
                }

                if (Keyboard.GetState().IsKeyDown(Keys.D) && player.LinearVelocity.X < 5.0f)
                {
                    player.ApplyForce(new Vector2(20.0f, 0));
                }
                else if (Keyboard.GetState().IsKeyDown(Keys.A) && player.LinearVelocity.X > -5.0f)
                {
                    player.ApplyForce(new Vector2(-20.0f, 0));
                }

                if (Keyboard.GetState().IsKeyDown(Keys.R))
                {
                    player.EquipedWeapon.Reload();
                }
                #endregion

                #region Update HUD
                hud.Health      = player.Health;
                hud.AmmoLoaded  = player.EquipedWeapon.LoadedAmmo;
                hud.AmmoReserve = player.EquipedWeapon.ReserveAmmo;
                hud.Reloading   = player.EquipedWeapon.Reloading;
                hud.selection   = player.EquipedWeaponSlot;
                #endregion

                #region world cleanup
                for (int i = gameObjects.Count - 1; i >= 0; i--)
                {
                    if (gameObjects[i] is ILivingThing)
                    {
                        ILivingThing deadObject = gameObjects[i] as ILivingThing;
                        if (deadObject.Health <= 0)
                        {
                            if (!(deadObject is Player))
                            {
                                Body deadBody = deadObject as Body;
                                CreateExplosion(deadBody.Position);
                            }
                            physicsWorld.RemoveBody(deadObject as Body);
                            gameObjects.RemoveAt(i);
                        }
                    }
                    else if (gameObjects[i] is Bullet)
                    {
                        Bullet bullet = gameObjects[i] as Bullet;
                        if (bullet.OffScreen())
                        {
                            physicsWorld.RemoveBody(bullet);
                            gameObjects.RemoveAt(i);
                        }
                    }
                    else if (gameObjects[i] is Explosion)
                    {
                        Explosion explosion = gameObjects[i] as Explosion;
                        if (!explosion.IsAlive)
                        {
                            gameObjects.RemoveAt(i);
                        }
                    }
                    else if (gameObjects[i] is SmokeParticle)
                    {
                        SmokeParticle smoke = gameObjects[i] as SmokeParticle;
                        if (!smoke.IsAlive)
                        {
                            gameObjects.RemoveAt(i);
                        }
                    }
                }
                if (player.Health <= 0)
                {
                    MediaPlayerHelper.MediaPlayerHelper.Instance.PlaySound(deathEffect);
                    respawnPlayer();
                }
                #endregion

                #region Misc Updates
                player.Update(gameTime);
                crosshair.Update(gameTime);

                for (int i = gameObjects.Count - 1; i >= 0; i--)
                {
                    gameObjects[i].Update(gameTime);
                }

                #endregion
                break;

            case GameState.MAIN_MENU:
                mainMenu.Update(gameTime);
                gameCamera.Position = Vector2.Zero;
                break;

            case GameState.LEVEL_COMPLETE:
                levelCompleteMenu.Update(gameTime);
                gameCamera.Position = Vector2.Zero;
                break;

            case GameState.OPTIONS_MENU:
                throw new NotImplementedException();
                break;
            }

            base.Update(gameTime);
        }