Esempio n. 1
0
        private void FireCannon(object sender, EventArgs args)
        {
            // SFX: play fire cannon sound
            SoundEffectInstance cannonShotInstance = cannonShot.CreateInstance();

            cannonShotInstance.Apply3D(listener, breakEmitter);
            cannonShotInstance.Play();
            cannonShotInstance.Apply3D(listener, breakEmitter);

            // create cannonball
            Cannonball cb = new Cannonball(ball, cannon.Position, cannon.Forward, cannon.PowerBar.CurrentFillAmount);

            cb.Texture       = generator.makeMarbleTexture();
            cb.ExplodeEvent += Explosion;
            cannonballs.Add(cb);

            // decrease number of cannon fire's left
            score -= 10;
        }
Esempio n. 2
0
        private void FireCannon(object sender, EventArgs args)
        {
            // SFX: play fire cannon sound
            SoundEffectInstance cannonShotInstance = cannonShot.CreateInstance();
            cannonShotInstance.Apply3D(listener, breakEmitter);
            cannonShotInstance.Play();
            cannonShotInstance.Apply3D(listener, breakEmitter);

            // create cannonball
            Cannonball cb = new Cannonball(ball, cannon.Position, cannon.Forward, cannon.PowerBar.CurrentFillAmount);
            cb.Texture = generator.makeMarbleTexture();
            cb.ExplodeEvent += Explosion;
            cannonballs.Add(cb);

            // decrease number of cannon fire's left
            score -= 10;
        }
Esempio n. 3
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();
            }

            // TODO: Add your update logic here
            states[currentState].Update(gameTime);
            states[currentState].Update(gameTime, Matrix.Identity);

            #region Sound listeners and emitters
            listener.Position = cameras[curCamera].Position;
            listener.Up       = cameras[curCamera].Up;
            listener.Forward  = cameras[curCamera].Forward;

            clickEmitter.Position = cameras[curCamera].Position; //Sound business
            clickEmitter.Up       = cameras[curCamera].Up;
            clickEmitter.Forward  = cameras[curCamera].Forward;

            cannonEmitter.Position = cameras[2].Position; //Sound business
            cannonEmitter.Up       = cameras[2].Up;
            cannonEmitter.Forward  = cameras[2].Forward;
            #endregion

            // to go directly to game state
            if (InputManager.IsKeyReleased(Keys.D1))
            {
                currentState = GameState.Menu;
            }
            if (InputManager.IsKeyReleased(Keys.D2))
            {
                currentState = GameState.Play;
            }
            if (InputManager.IsKeyReleased(Keys.D3))
            {
                currentState = GameState.Pause;
            }
            if (InputManager.IsKeyReleased(Keys.D4))
            {
                currentState = GameState.End;
            }

            // M to Mute
            if (InputManager.IsKeyReleased(Keys.M))
            {
                mute = !mute;
                if (mute)
                {
                    player.Stop();
                }
                else
                {
                    player.PlayLooping();
                }
            }

            // P to Pause
            if (currentState.Equals(GameState.Play) || currentState.Equals(GameState.Pause))
            {
                if (InputManager.IsKeyReleased(Keys.P))
                {
                    if (currentState.Equals(GameState.Play))
                    {
                        currentState = GameState.Pause;
                    }
                    else
                    {
                        currentState = GameState.Play;
                    }
                }
            }

            // Tab to switch cameras
            if (InputManager.IsKeyPressed(Keys.Tab) && currentState.Equals(GameState.Play))
            {
                if (++curCamera > 2)
                {
                    curCamera = 0;
                }
            }

            if (currentState.Equals(GameState.Play))
            {
                #region First Person Camera Stuff
                float elapsedTime = (float)(gameTime.ElapsedGameTime.TotalSeconds);
                if (InputManager.IsKeyDown(Keys.Up) || InputManager.IsKeyDown(Keys.W))
                {
                    if (cameraAngle < MathHelper.PiOver2) // Vertical
                    {
                        cameraAngle           += elapsedTime * 2f;
                        cameras[2].RotatePitch = elapsedTime * 2f;
                    }
                }
                if (InputManager.IsKeyDown(Keys.Down) || InputManager.IsKeyDown(Keys.S))
                {
                    if (cameraAngle > 0.0f) // Horizontal
                    {
                        cameraAngle           -= elapsedTime * 2f;
                        cameras[2].RotatePitch = -elapsedTime * 2f;
                    }
                }
                if (InputManager.IsKeyDown(Keys.Left) || InputManager.IsKeyDown(Keys.A))
                {
                    cameras[2].RotateY = elapsedTime * 2f;
                }
                if (InputManager.IsKeyDown(Keys.Right) || InputManager.IsKeyDown(Keys.D))
                {
                    cameras[2].RotateY = -elapsedTime * 2f;
                }
                #endregion

                if (InputManager.IsKeyReleased(Keys.Space) && score <= 0)
                {
                    currentState = GameState.End; // game over
                }

                if (cubes.Count <= 0)
                {
                    foreach (Cannonball ball in cannonballs)
                    {
                        ball.Parent = null;
                    }
                    cannonballs.Clear();
                }

                // used a reverse for loop so the game doesn't crash when the cannonball explodes (ironic)
                for (int i = cannonballs.Count - 1; i > -1; i--)
                {
                    Cannonball b = cannonballs[i];
                    b.Update(gameTime);

                    // used another reverse for loop so the game doesn't crash when the block breaks (double irony)
                    for (int j = cubes.Count - 1; j > -1; j--)
                    {
                        Block c = cubes[j];
                        if (c.DidCollide(b))
                        {
                            // SFX: play collision sound based on block type (glass, wood, stone)
                            c.CurrentState = Block.State.Moved;
                            if (c.BlockType != Block.Type.Glass)
                            {
                                b.Explode();
                            }
                            breakEmitter.Position = c.Position;
                            breakEmitter.Up       = c.Up;
                            breakEmitter.Forward  = c.Forward;
                        }
                    }
                }
            }

            InputManager.Update(gameTime);
            base.Update(gameTime);
        }