private void ProcessKeyboard(Vector3 direction, GameTime gameTime, SoundEngine soundEngine) { KeyboardState keys = Keyboard.GetState(); if (keys.GetPressedKeys().Length > 0) { if (keys.IsKeyDown(Keys.W)) { Thrust(direction, gameTime); soundEngine.ShipEngine.Play(); } if (keys.IsKeyDown(Keys.D)) { Roll(gameTime, "right"); soundEngine.ShipEngine.Play(); } if (keys.IsKeyDown(Keys.A)) { Roll(gameTime, "left"); soundEngine.ShipEngine.Play(); } if (keys.IsKeyDown(Keys.Q)) { if (this.Shrink != null) { this.Shrink.Activated = true; this.ShrinkUsed = true; soundEngine.Shrink.Play(); } } } else { Stop(direction, gameTime); soundEngine.ShipEngine.Stop(); } }
private void CheckCollisions(CollisionEngine collisionEngine, SoundEngine soundEngine, List<Powerup> powerups, List<Asteroid> asteroids) { // Check if the ship hits the edge of the universe if (collisionEngine.CollidesWithEdge(this.Position, this.BoundingSphere)) { float edge = collisionEngine.EDGE_OF_UNIVERSE; Vector3 pos = this.Position; if (pos.X > edge) pos.X = edge; if (pos.X < -edge) pos.X = -edge; if (pos.Y > edge) pos.Y = edge; if (pos.Y < -edge) pos.Y = -edge; if (pos.Z > edge) pos.Z = edge; if (pos.Z < -edge) pos.Z = -edge; UpdatePosition(pos); } // The ship is destroyed if it hits an asteroid unless it has a shield, // in that case the asteroid is destroyed. foreach (Asteroid asteroid in asteroids) { if (collisionEngine.CollideTwoObjects(this.BoundingSphere, asteroid.BoundingSphere)) { if (this.Shield != null) { asteroid.Destroyed = true; this.Shield = null; } else { this.Destroyed = true; LoseLife(); } if (soundEngine.Explosion.State != SoundState.Playing) soundEngine.Explosion.Play(); } } // The ship gets a powerup if it runs over it foreach (Powerup powerup in powerups) { if (collisionEngine.CollideTwoObjects(this.BoundingSphere, powerup.BoundingSphere)) { if (powerup.Type == Powerup.PowerupType.Shield && this.Shield == null) this.Shield = powerup; if (powerup.Type == Powerup.PowerupType.Shrink && this.Shrink == null) this.Shrink = powerup; powerup.Collected = true; // The powerup is activated if it is a shield if (powerup.Type == Powerup.PowerupType.Shield) { powerup.Activated = true; soundEngine.Shield.Play(); } break; } } }
public void Update(Vector3 direction, CollisionEngine collisionEngine, SoundEngine soundEngine, ParticleEngine particleEngine,List<Asteroid> asteroids, List<Powerup> powerups, MouseState originalMouseState, GameTime gameTime, GraphicsDevice device) { if (this.Shrink != null) { if (this.Shrink.Activated) { this.Shrink.Update(gameTime); if (this.Shrink.Timer <= 0) { this.Shrink = null; this.BoundingSphere = new BoundingSphere( this.Position, this.BoundingSphere.Radius / 0.25f); soundEngine.Grow.Play(); } else { this.BoundingSphere = new BoundingSphere( this.Position, this.BoundingSphere.Radius * 0.25f); } } } CheckCollisions(collisionEngine, soundEngine, powerups, asteroids); if (this.Destroyed) { if (this.Lives == 0) return; else { particleEngine.AddParticle(gameTime, this.Position); UpdatePosition(new Vector3(0, 0, 0)); this.Destroyed = false; this.Speed = 0f; this.Velocity = Vector3.Zero; this.Rotation = Quaternion.Identity; } } ProcessKeyboard(direction, gameTime, soundEngine); ProcessMouse(originalMouseState, gameTime, device); }
private void CheckCollisions(CollisionEngine collisionEngine, SoundEngine soundEngine, ParticleEngine particleEngine, GameTime gameTime, List<Asteroid> asteroids) { // Destroy the torpedo if it hits the edge of the universe if (collisionEngine.CollidesWithEdge(this.Position, this.BoundingSphere)) { if (soundEngine.Explosion.State != SoundState.Playing) soundEngine.Explosion.Play(); this.Destroyed = true; particleEngine.AddParticle(gameTime, this.Position); } // Destroy the torpedo if it hits an asteroid foreach (Asteroid asteroid in asteroids) { if (collisionEngine.CollideTwoObjects(this.BoundingSphere, asteroid.BoundingSphere)) { particleEngine.AddParticle(gameTime, this.Position); this.Destroyed = true; break; } } }
public void Update(CollisionEngine collisionEngine, SoundEngine soundEngine, ParticleEngine particleEngine, GameTime gameTime, List<Asteroid> asteroids) { CheckCollisions(collisionEngine, soundEngine, particleEngine, gameTime, asteroids); float speed = VELOCITY_CONST / gameTime.ElapsedGameTime.Milliseconds; Vector3 velocity = speed * this.Direction; Vector3 newPos = this.Position + velocity; UpdatePosition(newPos); }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); device = graphics.GraphicsDevice; Mouse.SetPosition(device.Viewport.Width / 2, device.Viewport.Height / 2); originalMouseState = Mouse.GetState(); billboardEffect = this.Content.Load<Effect>("Shaders/billboard_effect"); camera = new Camera(device); lifeTexture = this.Content.Load<Texture2D>("Sprites/spaceship_sprite"); explosionParticleTexture = this.Content.Load<Texture2D>("Sprites/explosion_particle"); shieldSprite = this.Content.Load<Texture2D>("Sprites/power_up_shield"); shrinkSprite = this.Content.Load<Texture2D>("Sprites/power_up_shrink"); timeFont = Content.Load<SpriteFont>("Fonts/Courier New"); collisionEngine = new CollisionEngine(); particleEngine = new ParticleEngine(explosionParticleTexture, billboardEffect, device); soundEngine = new SoundEngine(this.Content); skybox = new Skybox(); skybox.LoadModel(this.Content, effect); spaceship = new Spaceship(); spaceship.LoadModelAndTexture(this.Content, effect); torpedoes = new List<Torpedo>(); powerups = new List<Powerup>(); LoadAsteroids(); }