// Breaks an asteroid apart when it is hit by a blast private void breakApart(Asteroid asteroid, Blast blast) { //crash sound breakEffect.Play(.5f, .5f, 1f); Vector3 explosionVector; if (asteroid.scale < .75) { score += 150; asteroids.Remove(asteroid); currentAsteroidCount--; } else { if (asteroid.scale == 2) { score += 50; } if (asteroid.scale == 1) { score += 100; } asteroid.scale /= 2; Asteroid copy1 = new Asteroid(); copy1.scale = asteroid.scale; copy1.position = asteroid.position; Asteroid copy2 = new Asteroid(); copy2.scale = asteroid.scale; copy2.position = asteroid.position; //find vector perpendicular to asteroid velocity and blaster velocity //using the cross product, then normalized explosionVector = Vector3.Normalize(Vector3.Cross(asteroid.velocity, blast.velocity)); //updates positions along the explosion vector copy1.position += asteroid.scale * explosionVector; copy2.position -= asteroid.scale * explosionVector; //updates velocities copy1.velocity += (40f / asteroid.scale) * explosionVector; copy2.velocity -= (40f / asteroid.scale) * explosionVector; asteroids.Remove(asteroid); asteroids.Add(copy1); asteroids.Add(copy2); currentAsteroidCount++; } }
//Creates a new blast at location of ship and figures out velocity for it, then adds it to the list private void shoot() { blast = new Blast(); blast.scale = 10f; blast.velocity = world.Up * 500f; blast.position = blast.velocity * .05f; blast.texture = blastTexture; blastList.Add(blast); laser.Play(.5f, .7f, 1f); }
// Checks if a blast has hit an asteroid private bool hitCheck(Model model1, Matrix world1, Blast blast) { for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++) { BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere; sphere1 = sphere1.Transform(world1); if (Vector3.Distance(sphere1.Center, blast.position) < sphere1.Radius) { blastList.Remove(blast); return(true); } } return(false); }
//draws a blast to the screen as a billboard private void DrawBlast(Blast blast, BasicEffect effect) { //mostly Dr. Wittman's Particle code Matrix billboard = Matrix.CreateBillboard(blast.position, new Vector3(0, 0, 150), Vector3.UnitY, null); GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; GraphicsDevice.BlendState = BlendState.Additive; GraphicsDevice.RasterizerState = RasterizerState.CullNone; GraphicsDevice.SetVertexBuffer(vertexBuffer); effect.World = Matrix.CreateScale(blast.scale) * billboard; effect.View = view; effect.Projection = projection; effect.Texture = blast.texture; effect.TextureEnabled = true; effect.LightingEnabled = false; effect.PreferPerPixelLighting = false; foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2); } }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { asteroidModel = Content.Load <Model>("Models/rock1"); asteroidTexture = Content.Load <Texture2D>("Textures/rock1"); asteroids = new List <Asteroid>(); for (int i = 0; i < startingAsteroidCount; ++i) //generating all initial asteroids { asteroid = new Asteroid(); asteroid.position = Vector3.Zero; while (Math.Abs(asteroid.position.X) < 75 || Math.Abs(asteroid.position.Y) < 75 || Math.Abs(asteroid.position.Z) < 75) { asteroid.position = choosePosition(-positionRange, positionRange); } asteroid.velocity = chooseVelocity(); asteroid.scale = 2; asteroids.Add(asteroid); } //Creates thruster thruster = new Blast(); thruster.scale = 0f; thruster.velocity = Vector3.Zero; //Creates new list of blasts blastList = new List <Blast>(); basicEffect = new BasicEffect(GraphicsDevice); //sets up vertexBuffer for a square that is later used for blasts VertexPositionColorTexture[] vertices = new VertexPositionColorTexture[6]; Vector2 upperLeft = new Vector2(0, 0); Vector2 lowerLeft = new Vector2(0, 1); Vector2 upperRight = new Vector2(1, 0); Vector2 lowerRight = new Vector2(1, 1); vertices[0] = new VertexPositionColorTexture(new Vector3(1, 1, 0), Color.White, upperRight); vertices[1] = new VertexPositionColorTexture(new Vector3(1, -1, 0), Color.White, lowerRight); vertices[2] = new VertexPositionColorTexture(new Vector3(-1, -1, 0), Color.White, lowerLeft); vertices[3] = new VertexPositionColorTexture(new Vector3(1, 1, 0), Color.White, upperRight); vertices[4] = new VertexPositionColorTexture(new Vector3(-1, -1, 0), Color.White, lowerLeft); vertices[5] = new VertexPositionColorTexture(new Vector3(-1, 1, 0), Color.White, upperLeft); vertexBuffer = new VertexBuffer(basicEffect.GraphicsDevice, typeof(VertexPositionColorTexture), vertices.Length, BufferUsage.WriteOnly); vertexBuffer.SetData <VertexPositionColorTexture>(vertices); //gameplay variables lives = 3; score = 0; level = 1; nextLevelScore = 500; gameover = false; isSpacePressed = false; base.Initialize(); }