//Method for Updating the asteroids public void updateAsteroids(PlayerShip playerShip) { foreach (Asteroid asteroid in this.z_asteroidHolder) { //If the asteroid has gone off the screen, reset it back to the top if (asteroid.getPosition().Y > this.z_viewPort.Height+(asteroid.getSprite().Height*1.5) || asteroid.getHasBeenHit()) { asteroid.sethasBeenHit(false); //Then reset it and rerandomize it's variables //Not sure if reloading a new image for each asteroid is a good idea //Might cause game lag** asteroid.setSprite(z_content.Load<Texture2D> ("Images\\Asteroids\\Asteroid" + this.getRandomImage())); asteroid.setPosition(new Vector2(this.getRandomWidth(), this.getRandomHeight())); asteroid.setSpeed(this.getRandomSpeed()); //Because The rotation is perfectly centered, asteroids that move slow look really //Off balance. So instead I'm temporarily scaling rotation speed with the asteroids traveling speed //asteroid.setRotationSpeed(this.getRandomRotationSpeed()); asteroid.setRotationSpeed((asteroid.getSpeed() / 50)*this.getRandomRotationDirection()); asteroid.setIsAlive(true); } //If an asteroid has hit the player, then do something if (asteroid.getHitRec().Intersects(playerShip.getHitRec())) { asteroid.sethasBeenHit(true); playerShip.setHealth(0); } //Otherwise Update it's new position asteroid.AstroUpdate(); } }
//Update and Draw Methods -------------------------------------------------------------- //Main Update Method for Keyboard public void MissleManagerUpdateFriendlyKeyboard(KeyboardState currentKeyState, KeyboardState previousKeyState, PlayerShip playerShip, SpriteBatch spriteBatch) { //For the simple collision checking //this.z_EnemyShipList = enemyList; //The Alogrithm: //Determine if the player shot a missle //If so then add it the List //If the list is not empty, update each missle //While checking each missle, make sure it hasn't left the screen or collided with something //If so, remove it from the list if (currentKeyState.IsKeyDown(Keys.Space) && previousKeyState.IsKeyUp(Keys.Space) && playerShip.getIsAlive()) { //Play a fire sound this.z_fireSound1.Play(.2f, 0, 0); //Create and add a new Missle Object this.z_FriendlyMissles.Add(new PlayerMissle1(this.z_friendlyMissleSprite1, new Vector2(playerShip.getPosition().X + playerShip.getSprite().Width / 2 - this.z_friendlyMissleSprite1.Width / 2, playerShip.getPosition().Y), spriteBatch)); } //If List is empty, nothing to update, exit this update function if (this.z_FriendlyMissles.Count <= 0) return; this.UpdateFriendlyList(); }
//Main Update Method for GamePad public void MissleManagerUpdateFriendlyGamepad(GamePadState currentPadState, GamePadState previousPadState, PlayerShip playerShip, SpriteBatch spriteBatch) { //For the simple collision checking //this.z_EnemyShipList = enemyList; //Same Algorithm as before, but with a gamePad controller [Fire = right Trigger] if (currentPadState.Triggers.Right >= .5f && previousPadState.Triggers.Right == 0 && playerShip.getIsAlive()) { this.z_FriendlyMissles.Add(new PlayerMissle1(this.z_friendlyMissleSprite1, new Vector2(playerShip.getPosition().X + playerShip.getSprite().Width / 2 - this.z_friendlyMissleSprite1.Width / 2, playerShip.getPosition().Y), spriteBatch)); //Play a fire sound this.z_fireSound1.Play(.2f, 0, 0); } //If List is empty, nothing to update, exit this update function if (this.z_FriendlyMissles.Count <= 0) return; this.UpdateFriendlyList(); }
//Load Content Method ----------------------------------------------------------------------------------- protected override void LoadContent() { //Set the contentManger this.z_contentManager = new ContentManager(Services); // Create a new SpriteBatch, which can be used to draw textures. this.z_spriteBatch = new SpriteBatch(GraphicsDevice); //Set the viewPortRec this.z_viewportRec = new Rectangle(0, 0, z_graphics.GraphicsDevice.Viewport.Width, z_graphics.GraphicsDevice.Viewport.Height); //Load the background Images this.z_backgroundImage1 = new ScrollingBackground(Content.Load<Texture2D>("Textures\\spaceBackground")); this.z_backgroundImage2 = new ScrollingBackground(Content.Load<Texture2D>("Textures\\spaceBackground")); //Set the positions for the background Images this.z_backgroundImage1.setPosition(new Vector2(0f, 0f)); this.z_backgroundImage2.setPosition(new Vector2(0f, 0f - this.z_viewportRec.Height)); //Turn the background Images alive this.z_backgroundImage1.setIsAlive(true); this.z_backgroundImage2.setIsAlive(true); //Create the Player's ship image this.z_playerShip = new PlayerShip(Content.Load<Texture2D>("Images\\ship2")); //Set the starting position for player's ship this.z_playerShip.setPosition(new Vector2(this.z_viewportRec.Center.X, z_graphics.GraphicsDevice.Viewport.Height - 80)); //Set the player alive this.z_playerShip.setIsAlive(true); //Load the Music this.z_beautifulDarkness = Content.Load<Song>("Audio\\Beautiful_Darkness"); MediaPlayer.IsRepeating = true; //Load Fonts this.z_timerFont = Content.Load<SpriteFont>("Fonts\\TimerFont"); //Load Achivement Stuff this.z_achivementFail = new GameObject(Content.Load<Texture2D>("Images\\AchivementFailed")); this.z_achivementFail.setPosition(new Vector2((this.z_viewportRec.Width/2)-(this.z_achivementFail.getSprite().Width/2), this.z_viewportRec.Height-100)); this.z_achivementSound = Content.Load<SoundEffect>("Audio\\AchievementSound"); //Load the Settings for the asteroidManager this.z_asteroidManager = new AsteroidManager(AsteroidManager.AsteroidManagerState.Moderate, this.z_viewportRec, this.z_contentManager, this.z_spriteBatch); }