Update() public method

public Update ( ) : void
return void
        /// <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();
            }

            // move the teddybears
            bear0.Update(gameTime);
            bear1.Update(gameTime);

            // playing the explosion
            explosion.Update(gameTime);

            // detect the collision and play the explosion
            if (bear0.Active && bear1.Active && bear0.DrawRectangle.Intersects(bear1.DrawRectangle))
            {
                bear0.Active = false;
                bear1.Active = false;

                // get the collision point of two teddybears
                Rectangle collisionRectangle = Rectangle.Intersect(bear0.DrawRectangle, bear1.DrawRectangle);
                explosion.Play(collisionRectangle.Center.X, collisionRectangle.Center.Y);
            }

            base.Update(gameTime);
        }
Example #2
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();
            }

            MouseState mouse = Mouse.GetState();

            if (teddyBear.DrawRectangle.Contains(mouse.Position) && mouse.LeftButton == ButtonState.Pressed)
            {
                teddyBear.Active = false;
                Point location = teddyBear.DrawRectangle.Center;
                explosion.Play(location.X, location.Y);

                Vector2 velocity = new Vector2((float)rand.NextDouble(), (float)rand.NextDouble());
                velocity *= 0.2f;
                teddyBear = new TeddyBear(Content, WindowWidth, WindowHeight, "teddybear", rand.Next(WindowWidth), rand.Next(WindowHeight), velocity);
            }

            teddyBear.Update(gameTime);
            explosion.Update(gameTime);

            base.Update(gameTime);
        }
Example #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)
        {
            // For Mobile devices, this logic will close the Game when the Back button is pressed
            // Exit() is obsolete on iOS
                        #if !__IOS__ && !__TVOS__
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
                        #endif

            // TODO: Add your update logic here
            teddyBear.Update(gameTime);
            explosion.Update(gameTime);

            MouseState state = Mouse.GetState();

            if (state.X >= teddyBear.DrawRectangle.Left && state.X <= teddyBear.DrawRectangle.Right &&
                state.Y >= teddyBear.DrawRectangle.Top && state.Y <= teddyBear.DrawRectangle.Bottom &&
                state.LeftButton == ButtonState.Pressed)
            {
                teddyBear.Active = false;
                explosion.Play(teddyBear.DrawRectangle.X, teddyBear.DrawRectangle.Y);
            }

            base.Update(gameTime);
        }
Example #4
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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }
            if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
            {
                // Create a new teddy bear object
                int x_velocity = rand.Next(1, 2);
                int y_velocity = rand.Next(1, 2);
                int x_rand     = rand.Next(0, WINDOW_WIDTH);
                int y_rand     = rand.Next(0, WINDOW_HEIGHT);

                teddyBear.Active = true;

                teddyBear = new TeddyBear(Content, WINDOW_WIDTH, WINDOW_HEIGHT, "teddybear", x_rand, y_rand, new Vector2(x_velocity, y_velocity));
            }

            if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
            {
                explosion.Play((teddyBear.DrawRectangle.X + teddyBear.DrawRectangle.Width / 2), (teddyBear.DrawRectangle.Y + teddyBear.DrawRectangle.Height / 2));
                teddyBear.Active = false;
            }


            teddyBear.Update();
            explosion.Update(gameTime);

            base.Update(gameTime);
        }
        /// <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();
            }

            // update objects
            teddy.Update(gameTime);
            explosion.Update(gameTime);


            // explode teddy with click
            if (Mouse.GetState().LeftButton == ButtonState.Pressed &&
                teddy.DrawRectangle.Contains(Mouse.GetState().X, Mouse.GetState().Y))
            {
                teddy.Active = false;
                explosion.Play(teddy.DrawRectangle.Center.X, teddy.DrawRectangle.Center.Y);
            }

            // create new teddy with rick click after explode it
            if (teddy.Active == false && Mouse.GetState().RightButton == ButtonState.Pressed)
            {
                Vector2 velocity = new Vector2((float)rand.NextDouble(), (float)rand.NextDouble()) / 5;
                teddy = new TeddyBear(Content, WindowWidth, WindowHeight, "teddybear", WindowWidth / 2, WindowHeight / 2, velocity);
            }

            base.Update(gameTime);
        }
        /// <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)
        {
            // For Mobile devices, this logic will close the Game when the Back button is pressed
            // Exit() is obsolete on iOS
                        #if !__IOS__ && !__TVOS__
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
                        #endif

            // TODO: Add your update logic here
            teddyBear0.Update(gameTime);
            teddyBear1.Update(gameTime);

            if (teddyBear0.Active &&
                teddyBear0.DrawRectangle.Intersects(teddyBear1.DrawRectangle))
            {
                teddyBear0.Active = false;
                teddyBear1.Active = false;
                explosion.Play(teddyBear0.DrawRectangle.Right,
                               teddyBear0.DrawRectangle.Center.Y);
            }

            explosion.Update(gameTime);

            base.Update(gameTime);
        }
        /// <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
            bear0.Update(gameTime);
            bear0.Update(gameTime);

            //check for collision
            if (bear0.Active &&
                bear1.Active &&
                bear0.DrawRectangle.Intersects(bear1.DrawRectangle))
            {
                bear0.Active = false;
                bear1.Active = false;


                Rectangle collisionRectangle = Rectangle.Intersect(bear0.DrawRectangle, bear1.DrawRectangle);
                explosion.Play(collisionRectangle.Center.X, collisionRectangle.Center.Y);
            }
            //playing explosion
            explosion.Update(gameTime);

            base.Update(gameTime);
        }
Example #8
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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            teddyBear.Update();
            // X = x - w/2;
            int teddyBearLocationX = teddyBear.DrawRectangle.X + teddyBear.DrawRectangle.Width;
            int teddyBearLocationY = teddyBear.DrawRectangle.Y + teddyBear.DrawRectangle.Height;



            if ((Mouse.GetState().X > teddyBear.DrawRectangle.X) &&
                (Mouse.GetState().X < teddyBearLocationX) &&
                (Mouse.GetState().Y > teddyBear.DrawRectangle.Y) &&
                (Mouse.GetState().Y < teddyBearLocationY) &&
                (Mouse.GetState().LeftButton == ButtonState.Pressed))
            {
                teddyBear.Active = false;
                explosion.Play(teddyBearLocationX, teddyBearLocationY);
            }

            explosion.Update(gameTime);

            base.Update(gameTime);
        }
Example #9
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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // TODO: Add your update logic here
            // update game objects
            teddy0.Update();
            teddy1.Update();
            explode0.Update(gameTime);

            // check for collision
            //if (teddy0.Active && teddy1.Active && teddy0.DrawRectangle.X == teddy1.DrawRectangle.X)
            if (teddy0.Active && teddy1.Active && teddy0.DrawRectangle.Intersects(teddy1.DrawRectangle))
            {
                // deactivate bears
                teddy0.Active = teddy1.Active = false;

                // play explosion at approximately the point of collision
                //explode0.Play(teddy0.DrawRectangle.X, teddy0.DrawRectangle.Y);
                Rectangle collisionRectangle = Rectangle.Intersect(teddy0.DrawRectangle, teddy1.DrawRectangle);
                explode0.Play(collisionRectangle.Center.X, collisionRectangle.Center.Y);
            }
            base.Update(gameTime);
        }
Example #10
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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // Updating  the two teddy bears
            teddyBear0.Update();
            teddyBear1.Update();

            // Explode the teddy bears when they collide
            Rectangle teddyBearRectangle0 = teddyBear0.DrawRectangle;
            Rectangle teddyBearRectangle1 = teddyBear1.DrawRectangle;

            if ((teddyBearRectangle0.X / 2 == teddyBearRectangle1.X / 2) &&
                (teddyBearRectangle0.Y / 2 == teddyBearRectangle1.Y / 2))
            {
                // If there is a collision, teddy bears active is false
                teddyBear0.Active = false;
                teddyBear1.Active = false;
                // Play explosion after collision
                explosion.Play(teddyBearRectangle0.Location.X + 5, teddyBearRectangle0.Location.Y + 5);
            }
            explosion.Update(gameTime);
            base.Update(gameTime);
        }
Example #11
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
            MouseState mouse = Mouse.GetState();

            //if(bear.DrawRectangle.Contains(mouse.Position) && mouse.LeftButton == ButtonState.Released &&
            //    previousLeftButtonState == ButtonState.Pressed)
            //{
            //    bear.Active = false;
            //    explosion.Play(bear.DrawRectangle.X, bear.DrawRectangle.Y);
            //}

            if (bear.DrawRectangle.Contains(mouse.Position) && mouse.RightButton == ButtonState.Released && previousRightButtonState == ButtonState.Pressed)
            {
                bear.Active = false;
                explosion.Play(bear.DrawRectangle.X, bear.DrawRectangle.Y);
                SpawnBear();
            }

            bear.Update(gameTime);
            explosion.Update(gameTime);

            base.Update(gameTime);
            previousLeftButtonState  = mouse.LeftButton;
            previousRightButtonState = mouse.RightButton;
        }
Example #12
0
        public void Update(GameTime gameTime)
        {
            ExplosionTest.Clear();
            if (0 == ExplosionDict.Count)
            {
                return;
            }

            keys.Clear();
            foreach (var key in ExplosionDict.Keys)
            {
                Explosion explosion = ExplosionDict[key];
                if (null == explosion || !explosion.IsExploding)
                {
                    continue;
                }

                // Update explosion but check to see if finished.
                explosion.Update(gameTime);
                if (!explosion.IsExploding)
                {
                    ExplosionTest.Add((Byte)explosion.EnemyID);
                    keys.Add(explosion.ID);
                }
            }

            // https://stackoverflow.com/questions/9892790/deleting-while-iterating-over-a-dictionary
            foreach (var key in keys)
            {
                ExplosionDict.Remove(key);
            }
        }
Example #13
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
            bear0.Update(gameTime);
            bear1.Update(gameTime);

            //if bears collide and bears are active
            if (bear0.Active &&
                bear1.Active &&
                bear0.DrawRectangle.Intersects(bear1.DrawRectangle))
            {
                //make bears inactive
                bear0.Active = false;
                bear1.Active = false;

                //find intersection of the two rectangles of the bears
                Rectangle intersection = Rectangle.Intersect(bear0.DrawRectangle, bear1.DrawRectangle);
                explosion.Play(intersection.X, intersection.Y);
            }
            else
            {
                bear0.Active = true;
                bear1.Active = true;
            }

            explosion.Update(gameTime);

            base.Update(gameTime);
        }
Example #14
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            currentLifeTimer += gameTime.ElapsedGameTime;
            if (currentLifeTimer >= TimeSpan.FromSeconds(MissileData.BulletLifeTime))
            {
                Die();
            }

            if (RigidBody.LinearVelocity.X < 0)
            {
                RigidBody.LinearAcceleration = new Vector2(500, RigidBody.LinearAcceleration.Y);
            }
            else
            {
                RigidBody.LinearVelocity     = new Vector2(0, RigidBody.LinearVelocity.Y);
                RigidBody.LinearAcceleration = new Vector2(0, RigidBody.LinearAcceleration.Y);
            }

            if (Target != null && Target.Alive)
            {
                float angle = Trigonometry.GetAngleOfLineBetweenObjectAndTarget(this, Target.WorldPosition);
                if (Math.Abs(angle - WorldRotation) > 0.1f)
                {
                    RigidBody.AngularVelocity = 15 * Trigonometry.GetRotateDirectionForShortestRotation(this, Target.WorldPosition);
                }
                else
                {
                    // Bad that we are assuming it is not parented to anything, but I think that is a valid assumption
                    LocalRotation = angle;
                    RigidBody.FullAngularStop();
                }
            }
            else
            {
                RigidBody.FullAngularStop();
            }

            EngineBlaze.Update(gameTime);

            if (Explosion.Animation.IsPlaying)
            {
                Explosion.Update(gameTime);
            }

            if (Explosion.Animation.Finished)
            {
                Alive = false;
            }
        }
Example #15
0
 /// <summary>
 /// Updates the explosions.
 /// </summary>
 /// <param name="gameTime">The GameTime.</param>
 private void UpdateExplosion(GameTime gameTime)
 {
     for (int i = 0; i < Explosions.Count; i++)
     {
         Explosion explosion = Explosions[i];
         if (explosion.RemainingLifeTime <= 0)
         {
             Explosions.Remove(explosion);
             i++;
         }
         else
         {
             explosion.Update(gameTime);
         }
     }
 }
Example #16
0
        private void HandleCollisions(GameTime gameTime)
        {
            // see if a player shot hit an enemy
            for (int i = 0; i < _playerShots.Count; i++)
            {
                PlayerShot playerShot = _playerShots[i];
                // check the shot and see if it it collided with an enemy
                if (playerShot != null && _enemyGroup.HandlePlayerShotCollision(_playerShots[i]))
                {
                    // remove the shot, add the score
                    _playerShots.RemoveAt(i);
                    _score += 100;
                    _explosion.Play();
                }
            }

            // see if an enemy shot hit the player
            if (_player != null && _enemyGroup.HandleEnemyShotCollision(_player))
            {
                // blow up the player
                _playerExplosion = new Explosion();
                Vector2 center = _player.Position + (_player.Size / 2.0f);
                _playerExplosion.Position = center - (_playerExplosion.Size / 2.0f);
                _player = null;
                _explosion.Play();
            }

            // if the player explosion animation is running, update it
            if (_playerExplosion != null)
            {
                // if this is the last frame
                if (_playerExplosion.Update(gameTime))
                {
                    // remove it
                    _playerExplosion = null;

                    // reset the board
                    _enemyGroup.Reset();
                    _playerShots.Clear();

                    _player          = new Player();
                    _player.Position = PlayerStartPosition;
                }
            }
        }
        /// <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
            teddy.Update(gameTime);
            explosion.Update(gameTime);
            MouseState mouse = Mouse.GetState();

            int x = mouse.Position.X;
            int y = mouse.Position.Y;

            if (teddy.DrawRectangle.Contains(x, y) && mouse.LeftButton == ButtonState.Released &&
                previousLeftButtonState == ButtonState.Pressed)
            {
                teddy.Active = false;
                explosion.Play(teddy.DrawRectangle.X, teddy.DrawRectangle.Y);
            }

            if (previousMiddleButtonState == ButtonState.Pressed)
            {
                teddy = new TeddyBear(Content, WindowWidth, WindowHeight, @"graphics/teddybear",
                                      300, 200, new Vector2((float)rdn.NextDouble() * (float)0.5, (float)rdn.NextDouble() * (float)0.5));
            }

            if (previousRightButtonState == ButtonState.Pressed)
            {
                explosion.Play(teddy.DrawRectangle.X, teddy.DrawRectangle.Y);
                teddy = new TeddyBear(Content, WindowWidth, WindowHeight, @"graphics/teddybear",
                                      300, 200, new Vector2((float)rdn.NextDouble() * (float)0.5, (float)rdn.NextDouble() * (float)0.5));
            }


            previousLeftButtonState   = mouse.LeftButton;
            previousMiddleButtonState = mouse.MiddleButton;
            previousRightButtonState  = mouse.RightButton;

            base.Update(gameTime);
        }
        /// <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();
            }

            teddy0.Update(gameTime);
            teddy1.Update(gameTime);

            if (teddy0.Active && teddy1.Active &&
                teddy0.DrawRectangle.Intersects(teddy1.DrawRectangle))
            {
                teddy0.Active = false;
                teddy1.Active = false;
                explosion.Play(teddy0.DrawRectangle.X, teddy0.DrawRectangle.Y);
            }
            explosion.Update(gameTime);
            base.Update(gameTime);
        }
Example #19
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();
            }

            MouseState mouseState    = Mouse.GetState();
            Rectangle  bearRectangle = bear.DrawRectangle;

            bool isMouseOverBear = (mouseState.X >= bearRectangle.Left && mouseState.X <= bearRectangle.Right &&
                                    mouseState.Y >= bearRectangle.Top && mouseState.Y <= bearRectangle.Bottom);

            if (isMouseOverBear && mouseState.LeftButton == ButtonState.Pressed && bear.Active)
            {
                bear.Active = false;
                explosion.Play(bearRectangle.Center.X, bearRectangle.Center.Y);
            }

            if (mouseState.MiddleButton == ButtonState.Released && mmbPreviousState == ButtonState.Pressed)
            {
                bear = GetRandomTeddyBear();
            }

            if (mouseState.RightButton == ButtonState.Released && rmbPreviousState == ButtonState.Pressed && bear.Active)
            {
                bear.Active = false;
                explosion.Play(bearRectangle.Center.X, bearRectangle.Center.Y);
                bear = GetRandomTeddyBear();
            }

            rmbPreviousState = mouseState.RightButton;
            mmbPreviousState = mouseState.MiddleButton;

            bear.Update(gameTime);
            explosion.Update(gameTime);

            base.Update(gameTime);
        }
Example #20
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)
        {
            timeElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            MouseState newMouseState = Mouse.GetState();

            cursorPos = new Vector2(oldMouseState.X, oldMouseState.Y);

            if (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
            {
                Vector2 vec = new Vector2(newMouseState.X, newMouseState.Y);
                explosion.Click(vec);
                //timeElapsed = 0;
            }
            oldMouseState = newMouseState;

            //timeElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (timeElapsed <= (float)smokeSystem.maxParticleLife / (float)smokeSystem.maxParticleCount)
            {
                explosion.Update(timeElapsed * 1000);
                timeElapsed = 0;
            }

            //explosion.Update((float)gameTime.ElapsedGameTime.TotalMilliseconds);

            ballSimulation.updateBallCollision();
            // TODO: Add your update logic here

            base.Update(gameTime);
        }
Example #21
0
        public override void Update(double elapsedTime)
        {
            if (this.IsPaused)
            {
                return;
            }

            if (this.Position.Y <= 0)
            {
                this.State = StageState.Stop;
            }

            if (!this.IsStopedMove)
            {
                this.UpdateY(elapsedTime);
            }

            if (this.v2dNextUserPosition.Y >= this.Position.Y && this.v2dNextUserPosition.Y <= (this.Position.Y + 500))
            {
                ssUser.Position.Y = this.v2dNextUserPosition.Y;
            }
            else
            {
                ssUser.Position.Y += this.Velocity.Y * elapsedTime;
            }

            if (this.v2dNextUserPosition.X >= this.Position.X && this.v2dNextUserPosition.X <= (this.Position.X + 800))
            {
                ssUser.Position.X = this.v2dNextUserPosition.X;
            }


            #region createBullet
            ssUser.TimeAcummulator.Update(elapsedTime);
            if (createBullet)
            {
                if (ssUser.TimeAcummulator.IsOverflow)
                {
                    Vector2D posB;
                    posB.X = ssUser.Position.X + ssUser.AddPosBullet;
                    posB.Y = ssUser.Position.Y;
                    ssUser.BulletSettings.AddBullets(lstUserBullets, posB, this, true);
                    createBullet = false;
                }
            }
            #endregion

            #region enemy.Update
            for (int ie = 0; ie < lstEnemies.Count; ie++)
            {
                EnemySpaceShip enemy = lstEnemies[ie];
                switch (enemy.Update(ssUser, this, elapsedTime))
                {
                case CombatStatus.MeDie:
                case CombatStatus.BothDie:
                    lstEnemies.RemoveAt(ie);
                    ie--;
                    if (enemy.HasItem)
                    {
                        this.Items.Add(enemy.Item);
                    }
                    enemy.Dispose();
                    break;
                }
            }
            #endregion

            #region lstUserBullets
            for (int i = 0; i < lstUserBullets.Count; ++i)
            {
                Bullet bullet = lstUserBullets[i];
                bullet.Update(elapsedTime);
                if (bullet.Position.Y <= this.Position.Y)
                {
                    lstUserBullets.RemoveAt(i);
                    bullet.Dispose();
                    --i;
                    continue;
                }
                for (int j = 0; j < lstEnemies.Count; ++j)
                {
                    EnemySpaceShip enemy    = lstEnemies[j];
                    CombatStatus   cs       = bullet.VerifyCombat(enemy);
                    bool           breakFor = false;
                    switch (cs)
                    {
                    case CombatStatus.BothDie:
                        #region CombatStatus.BothDie
                        this.lstExplosions.Add(new Explosion(bullet.Position, ExplosionType.One));
                        lstUserBullets.RemoveAt(i);
                        bullet.Dispose();
                        --i;
                        lstEnemies.RemoveAt(j);
                        --j;
                        if (enemy.HasItem)
                        {
                            lstItem.Add(enemy.Item);
                        }
                        enemy.Dispose();
                        breakFor = true;
                        break;

                        #endregion
                    case CombatStatus.MeDie:
                        #region CombatStatus.MeDie
                        this.lstExplosions.Add(new Explosion(bullet.Position, ExplosionType.One));
                        lstUserBullets.RemoveAt(i);
                        bullet.Dispose();
                        --i;
                        breakFor = true;
                        break;

                        #endregion
                    case CombatStatus.CombatantDie:
                        #region CombatStatus.CombatantDie
                        this.lstExplosions.Add(new Explosion(bullet.Position, ExplosionType.One));
                        lstEnemies.RemoveAt(j);
                        --j;
                        if (enemy.HasItem)
                        {
                            lstItem.Add(enemy.Item);
                        }
                        enemy.Dispose();
                        break;
                        #endregion
                    }
                    if (breakFor)
                    {
                        break;
                    }
                }
            }
            #endregion

            #region lstEnemiesBullets
            for (int i = 0; i < lstEnemiesBullets.Count; ++i)
            {
                Bullet enBullet = lstEnemiesBullets[i];
                enBullet.Update(elapsedTime);
                if (enBullet.Position.Y >= (this.Position.Y + 816))
                {
                    lstEnemiesBullets.RemoveAt(i);
                    enBullet.Dispose();
                    --i;
                    continue;
                }
                if (enBullet.VerifyCombat(ssUser) != CombatStatus.None)
                {
                    this.lstExplosions.Add(new Explosion(enBullet.Position, ExplosionType.Two));
                    lstEnemiesBullets.RemoveAt(i);
                    enBullet.Dispose();
                    --i;
                    break;
                }
            }

            #endregion

            #region lstExplosions
            for (int i = 0; i < lstExplosions.Count; ++i)
            {
                Explosion exp = lstExplosions[i];
                exp.Update(elapsedTime);
                if (exp.Destroy())
                {
                    lstExplosions.RemoveAt(i);
                    exp.Dispose();
                    --i;
                }
            }
            #endregion

            #region lstItem
            for (int i = 0; i < lstItem.Count; ++i)
            {
                Item ib = lstItem[i];
                if (ib.Update(elapsedTime, ssUser))
                {
                    lstItem.RemoveAt(i);
                    ib.Dispose();
                    --i;
                }
            }
            #endregion
        }
Example #22
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();
            }

            // get current mouse state and add mine if necessary
            MouseState mouse = Mouse.GetState();

            if (!mineAdded && mouse.LeftButton == ButtonState.Pressed)
            {
                mines.Add(new Mine(mineSprite, mouse.X, mouse.Y));

                // no more mines until mouse is released
                mineAdded = true;
            }

            // reset flag to allow adding more mines
            if (mineAdded && mouse.LeftButton == ButtonState.Released)
            {
                mineAdded = false;
            }

            // spawn a teddy bear if time is up
            if (millisUntilNewTeddy <= 0)
            {
                // time is up -> spawn new teddy
                teddys.Add(new TeddyBear(teddySprite, generateNewRandomTeddyVelocity(), WindowWidth, WindowHeight));

                // get new millis for teddy bear spawning
                millisUntilNewTeddy = generateNewRandomMillisForTeddy();
            }

            // decrease timer
            millisUntilNewTeddy -= gameTime.ElapsedGameTime.Milliseconds;

            // update teddys
            for (int i = teddys.Count - 1; i >= 0; i--)
            {
                TeddyBear teddy = teddys[i];

                // if teddy is inactive -> remove teddy
                if (!teddy.Active)
                {
                    teddys.Remove(teddy);
                    continue;
                }

                // update the teddy
                teddy.Update(gameTime);

                // check collision
                for (int j = mines.Count - 1; j >= 0; j--)
                {
                    Mine mine = mines[j];

                    // only check active mines
                    if (mine.Active)
                    {
                        if (teddy.CollisionRectangle.Intersects(mine.CollisionRectangle))
                        {
                            // add explosion on mine center
                            Point center = mine.CollisionRectangle.Center;
                            explosions.Add(new Explosion(explosionSprite, center.X, center.Y));

                            // deactivate teddy and mine
                            teddy.Active = false;
                            mine.Active  = false;
                        }
                    }
                    else
                    {
                        // remove inactive mine
                        mines.Remove(mine);
                    }
                }
            }

            // update explosions
            for (int i = explosions.Count - 1; i >= 0; i--)
            {
                Explosion explosion = explosions[i];

                if (explosion.Playing)
                {
                    // update active explosion
                    explosion.Update(gameTime);
                }
                else
                {
                    // update inactive explosion
                    explosions.Remove(explosion);
                }
            }


            base.Update(gameTime);
        }
        /// <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();
            }

            // get mouse state
            MouseState mouse = Mouse.GetState();

            // click left button on the teddy bear to make it explode
            if (mouse.LeftButton == ButtonState.Pressed &&
                leftButton == ButtonState.Released &&
                teddybear.Active &&
                teddybear.DrawRectangle.Contains(mouse.X, mouse.Y))
            {
                leftButton = ButtonState.Pressed;

                explosion.Play(teddybear.DrawRectangle.Center.X, teddybear.DrawRectangle.Center.Y);

                teddybear.Active = false;
            }

            // track the previous state
            if (mouse.LeftButton == ButtonState.Released)
            {
                leftButton = ButtonState.Released;
            }

            // click right button to replace the current teddy bear
            if (mouse.RightButton == ButtonState.Pressed && rightButton == ButtonState.Released)
            {
                rightButton = ButtonState.Pressed;

                teddybear = new TeddyBear(Content, WindowWidth, WindowHeight, @"graphics\teddybear", random.Next(0, WindowWidth), random.Next(0, WindowHeight), new Vector2((float)random.NextDouble(), (float)random.NextDouble()));
            }

            // track the previous state
            if (mouse.RightButton == ButtonState.Released)
            {
                rightButton = ButtonState.Released;
            }

            // click middle button to explode the curren teddy bear and replace to new one
            if (mouse.MiddleButton == ButtonState.Pressed && middleButton == ButtonState.Released)
            {
                middleButton = ButtonState.Pressed;

                if (teddybear.Active)
                {
                    explosion.Play(teddybear.DrawRectangle.Center.X, teddybear.DrawRectangle.Center.Y);
                }

                teddybear.Active = false;

                teddybear = new TeddyBear(Content, WindowWidth, WindowHeight, @"graphics\teddybear", random.Next(0, WindowWidth), random.Next(0, WindowHeight), new Vector2((float)random.NextDouble(), (float)random.NextDouble()));
            }

            // track the previous state
            if (mouse.MiddleButton == ButtonState.Released)
            {
                middleButton = ButtonState.Released;
            }

            teddybear.Update(gameTime);
            explosion.Update(gameTime);

            base.Update(gameTime);
        }
Example #24
0
        public override void Update(GameTime gameTime)
        {
            if (_explosion != null)
            {
                _explosion.Update(gameTime);

                if (_explosion.Dispose)
                {
                    _explosion = null;
                }
            }

            //handle particle Updates
            foreach (TextParticle particle in _textParticles)
            {
                particle.Update(gameTime);

                if (particle.Dispose)
                {
                    _disposeParticles.AddLast(particle);
                }
            }

            foreach (TextParticle particle in _disposeParticles)
            {
                _textParticles.Remove(particle);
            }
            _disposeParticles.Clear();


            //handle person update
            if (!IsEnabled)
            {
                return;
            }

            if (!IsInElevator)
            {
                //doesn't know where to go
                if (IsExploring)
                {
                    if (RoomPosition.X < RoomHeading.X)
                    {
                        _direction = TravelDirection.Right;
                    }
                    else
                    {
                        _direction = TravelDirection.Left;
                    }
                    _isTraveling = true;

                    if (_isTraveling)
                    {
                        Travel(gameTime);
                        CheckRoom(RoomHeading);
                    }
                }
                else //knows where he wants to go
                {
                    if (IsWaitingForElevator)
                    {
                        StressLevel  -= 1 * (float)gameTime.ElapsedGameTime.TotalSeconds;
                        _currentTime += gameTime.ElapsedGameTime;

                        if (_currentTime > _refreshStressTime)
                        {
                            _hp += 1;
                            _textParticles.Add(new TextParticle(_game, "+" + "1", Color.Yellow, this));
                            _currentTime = TimeSpan.Zero;
                        }
                    }
                    else if (!_isTraveling && RoomPosition != RoomHeading)
                    {
                        if (RoomPosition.Y == RoomHeading.Y)
                        {
                            if (RoomPosition.X < RoomHeading.X)
                            {
                                _direction = TravelDirection.Right;
                            }
                            else
                            {
                                _direction = TravelDirection.Left;
                            }
                            _isTraveling = true;
                        }
                        else
                        {
                            if (RoomPosition.X < _building.ShaftPosition)
                            {
                                _direction = TravelDirection.Right;
                            }
                            else
                            {
                                _direction = TravelDirection.Left;
                            }
                            _isTraveling = true;
                        }
                    }

                    if (_isTraveling)
                    {
                        _currentTime = TimeSpan.Zero;
                        Travel(gameTime);

                        if (IsOnRightFloor) //check for correct Room
                        {
                            CheckRoom(RoomHeading);
                        }
                        else //check for elevator
                        {
                            CheckRoom(new Point(_building.ShaftPosition, RoomHeading.Y));
                        }
                    }
                }
            }

            RoomPosition = _building.GetRoomFromPosition(Position);
            base.Update(gameTime);
        }
Example #25
0
        public void Update()
        {
            if (IsAlive)
            {
                if (!isGrounded)
                {
                    velocity.Y += Game.Gravity;
                    //velocity.Y += Game.Gravity * Game.DeltaTime;

                    if (velocity.X > 0)
                    {
                        velocity.X -= Game.FrictionX;
                        //velocity.X -= Game.FrictionX * Game.DeltaTime;
                        if (velocity.X <= 0)
                        {
                            velocity.X = 0;
                        }
                    }
                    else if (velocity.X < 0)
                    {
                        velocity.X += Game.FrictionX;
                        //velocity.X += Game.FrictionX * Game.DeltaTime;
                        if (velocity.X >= 0)
                        {
                            velocity.X = 0;
                        }
                    }
                }

                //prova senza delta time float deltaY = velocity.Y;
                //float deltaY = velocity.Y * Game.DeltaTime;
                //float deltaX = velocity.X * Game.DeltaTime;
                float deltaY = velocity.Y * Game.DeltaTime;
                float deltaX = velocity.X * Game.DeltaTime;

                position.Y += deltaY;
                position.X += deltaX;

                TranslateSprites(deltaX, deltaY);

                if (position.X - ray < 0 || position.X + ray > Game.Win.width || position.Y - height / 2 < 0)
                {
                    Die();
                    return;
                }

                if (AsteroidsManager.Collides(position, ray))
                {
                    Die();
                    return;
                }

                if (GroundManager.Collides(position, height / 2, ref isGrounded))
                {
                    if (!IsGrounded)
                    {
                        Die();
                        return;
                    }
                    else if (isGrounded && velocity.Y != 0)
                    {
                        if (velocity.GetLength() >= resistance)
                        {
                            Die();
                            Console.WriteLine("si");
                            return;
                        }
                        else
                        {
                            velocity.Y = velocity.X = 0f;
                        }
                    }
                }
            }
            else if (isVisible)
            {
                if (!explosionDie.Update())
                {
                    isVisible = false;
                }
            }
        }
Example #26
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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // TODO: Add your update logic here
            bear0.Update();
            bear1.Update();
            explosion.Update(gameTime);

            //get mouse state
            MouseState mouse = Mouse.GetState();

            //explode bears if mouse is over it and left mouse button is pressed
            if (bear0.Active &&
                mouse.LeftButton == ButtonState.Pressed &&
                previousMouseLeftPressed == ButtonState.Released &&
                bear0.DrawRectangle.Contains(mouse.X, mouse.Y))
            {
                bear0.Active = false;
                explosion.Play(bear0.DrawRectangle.Center.X, bear0.DrawRectangle.Center.Y);
            }

            else if (bear1.Active &&
                     mouse.LeftButton == ButtonState.Pressed &&
                     previousMouseLeftPressed == ButtonState.Released &&
                     bear1.DrawRectangle.Contains(mouse.X, mouse.Y))
            {
                bear1.Active = false;
                explosion.Play(bear1.DrawRectangle.Center.X, bear1.DrawRectangle.Center.Y);
            }
            //updating current mouse left button state
            previousMouseLeftPressed = mouse.LeftButton;

            //explode bears if they collide with each other
            if (bear0.Active &&
                bear1.Active &&
                bear1.DrawRectangle.Intersects(bear0.DrawRectangle))
            {
                bear0.Active = false;
                bear1.Active = false;

                //play explosion
                Rectangle collisionRectangle = Rectangle.Intersect(bear0.DrawRectangle, bear1.DrawRectangle);

                explosion.Play(collisionRectangle.Center.X, collisionRectangle.Center.Y);
            }

            //respawn bears if inactive and right mouse button is pressed
            if (bear0.Active == false &&
                mouse.RightButton == ButtonState.Pressed &&
                previousMouseRightPressed == ButtonState.Released)
            {
                int     randX         = rand.Next(800);
                int     randY         = rand.Next(600);
                Vector2 velocitySpawn = new Vector2(rand.Next(10), rand.Next(10));
                bear0 = new TeddyBear(Content, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, "teddybear0", randX, randY, velocitySpawn);
            }
            if (bear1.Active == false &&
                mouse.RightButton == ButtonState.Pressed &&
                previousMouseRightPressed == ButtonState.Released)
            {
                int     randX         = rand.Next(800);
                int     randY         = rand.Next(600);
                Vector2 velocitySpawn = new Vector2(rand.Next(10), rand.Next(10));
                bear1 = new TeddyBear(Content, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, "teddybear1", randX, randY, velocitySpawn);
            }

            //updating current mouse left button state
            previousMouseRightPressed = mouse.RightButton;

            base.Update(gameTime);
        }
Example #27
0
        private void HandleCollisions(GameTime gameTime)
        {
            // see if a player shot hit an enemy
            for (int i = 0; i < _playerShots.Count; i++)
            {
                PlayerShot playerShot = _playerShots[i];
                // check the shot and see if it it collided with an enemy
                if (playerShot != null && _enemyGroup.HandlePlayerShotCollision(_playerShots[i]))
                {
                    // remove the shot, add the score
                    _playerShots.RemoveAt(i);
                    _score += 100;
                    AudioManager.PlayCue(AudioManager.Cue.Explosion);
                }
            }

            // see if an enemy shot hit the player
            if (_player != null && _enemyGroup.HandleEnemyShotCollision(_player))
            {
                // blow up the player
                _playerExplosion = new Explosion();
                Vector2 center = _player.Position + (_player.Size / 2.0f);
                _playerExplosion.Position = center - (_playerExplosion.Size / 2.0f);
                _player = null;
                AudioManager.PlayCue(AudioManager.Cue.Explosion);
            }

            // see if an enemy hit the player directly
            if (_player != null && _enemyGroup.HandleEnemyPlayerCollision(_player))
            {
                // blow up the player
                _playerExplosion = new Explosion();
                Vector2 center = _player.Position + (_player.Size / 2.0f);
                _playerExplosion.Position = center - (_playerExplosion.Size / 2.0f);
                _player = null;
                AudioManager.PlayCue(AudioManager.Cue.Explosion);
                _loseGame = true;
            }

            // if the player explosion animation is running, update it
            if (_playerExplosion != null)
            {
                // if this is the last frame
                if (_playerExplosion.Update(gameTime) && !_loseGame)
                {
                    // remove it
                    _playerExplosion = null;

                    // we lose if we have no lives left
                    if (_lives == 0)
                    {
                        _loseGame = true;
                    }
                    else
                    {
                        // subract 1 life and reset the board
                        _lives--;
                        _enemyGroup.Reset();
                        _playerShots.Clear();
                        _player          = new Player();
                        _player.Position = new Vector2(AlienAttackGame.ScreenWidth / 2 - _player.Width / 2, AlienAttackGame.ScreenHeight - 100);
                    }
                }
            }
        }