public void Shooting( Bullet bullet, GameTime gameTime, bool isPlayerTurnedRight, int leftBound, int rightBound) { this.mouseState = Mouse.GetState(); if (this.isShooting) { if (this.isBulletDirectionRight) { bullet.XPosition += this.bulletVelocity; } else { bullet.XPosition -= this.bulletVelocity; } if ((bullet.XPosition > rightBound) || (bullet.XPosition < leftBound)) { this.isShooting = false; } } else { if (this.mouseState.LeftButton == ButtonState.Pressed) { this.isShooting = true; if (isPlayerTurnedRight) { this.isBulletDirectionRight = true; } else { this.isBulletDirectionRight = false; } } } this.bullet = bullet; }
/// <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) { this.keyboardState = Keyboard.GetState(); // Allows the game to exit if (this.keyboardState.IsKeyDown(Keys.Escape)) { this.Exit(); } this.player.Update(); this.playerAbilities.Update(); // Move left this.player.MoveLeft(gameTime, this.controllersSettings.MoveLeftKey); if (this.player.IsMovingLeft) { // Load new texture depending on the direction of player Texture2D newPlayerTexture = this.Content.Load<Texture2D>("Player/PlayerTurnedLeft"); this.player.ChangeTexture = newPlayerTexture; MoveBakgroundRight(gameTime); MoveObjectRight(this.initializeFirstLevel.BackgroundObjects, gameTime, BACKGROUND_SLIDING_VELOCITY); } // Move right this.player.MoveRight(gameTime, this.controllersSettings.MoveRightKey); if (this.player.IsMovingRight) { // Load new texture depending on the direction of player Texture2D newPlayerTexture = this.Content.Load<Texture2D>("Player/PlayerTurnedRight"); this.player.ChangeTexture = newPlayerTexture; MoveBakgroundLeft(gameTime); MoveObjectsLeft(this.initializeFirstLevel.BackgroundObjects, gameTime, BACKGROUND_SLIDING_VELOCITY); } // Shooting if ((this.player.IsTurnedRight) && (!this.isPlayerShooting)) { Texture2D bulletTexture = Content.Load<Texture2D>("Bullets/bullet_right"); Vector2 bulletPosition = new Vector2(this.player.XPosition + this.player.Width - 87, this.player.YPosition + MUZZLE_OF_GUN_POSITION); this.bullet = new Bullet(bulletTexture, bulletPosition); } else if (!this.isPlayerShooting) { Texture2D bulletTexture = Content.Load<Texture2D>("Bullets/bullet_left"); Vector2 bulletPosition = new Vector2(this.player.XPosition, this.player.YPosition + MUZZLE_OF_GUN_POSITION); this.bullet = new Bullet(bulletTexture, bulletPosition); } if (this.bullet != null) { this.playerAbilities.Shooting(bullet, gameTime, this.player.IsTurnedRight, 0, this.windowWidth); this.isPlayerShooting = this.playerAbilities.IsShooting; } // Jumping this.isPlayerJumping = IsPlayerJumping(gameTime, this.controllersSettings.JumpKey); //this.playerAbilities.Jumping(gameTime, this.controllersSettings.JumpKey); //if (this.playerAbilities.IsJumping) if (this.isPlayerJumping) { this.jumpingVelocity = JUMPING_VELOCITY; this.isGravityOn = true; this.isPlayerOnGround = false; this.timer = (float)gameTime.ElapsedGameTime.TotalSeconds; // if (this.player.BoundingBox.Bottom > this.floor.BoundingBox.Top) // { // this.player.Position = new Vector2( // this.windowCenter - (this.player.Width / 2), // this.floor.BoundingBox.Top - this.player.Height); // this.playerAbilities.IsJumping = false; // } } CollisionDetectionPerPixel(gameTime); if ((float)gameTime.ElapsedGameTime.TotalSeconds > this.timer + 1) { if (!this.isPlayerJumping) { if (this.hasBottomCollision) { this.isPlayerOnGround = true; this.isGravityOn = false; //this.isPlayerJumping = false; this.oldJumpingVelocity = this.jumpingVelocity; } } } if (!this.hasBottomCollision) { this.isPlayerOnGround = false; this.isGravityOn = true; } if((this.isGravityOn && !isPlayerOnGround) || this.isPlayerJumping) { this.player.YPosition += this.jumpingVelocity; this.jumpingVelocity++; if (this.jumpingVelocity == 0) { this.isPlayerJumping = false; } if (this.player.YPosition + this.player.Height > this.floor.YPosition) { this.isGravityOn = false; this.isPlayerJumping = false; this.isPlayerOnGround = true; } } base.Update(gameTime); }