MrGravity.Level.Update C# (CSharp) Method

Update() public method

Updates the level's progress
public Update ( GameTime gameTime, GameStates &gameState ) : void
gameTime Microsoft.Xna.Framework.GameTime The game time.
gameState GameStates State of the game.
return void
        public void Update(GameTime gameTime, ref GameStates gameState)
        {
            if (_mPlayer.MIsAlive)// only update while player is alive
            {
                if (!_bw.IsBusy)
                    _bw.RunWorkerAsync();

                if (_mDeathState == DeathStates.Playing)
                {
                    MTimer += (gameTime.ElapsedGameTime.TotalSeconds);

                    if (_mPlayerEnd != null)
                        _mPlayerEnd.UpdateFace(MTimer);

                    foreach (var gObject in _mObjects)
                    {
                        if (gObject.CollisionType == XmlKeys.Collectable)
                        {
                            if (_mCollectableAnimation == null)
                            {
                                _mCollectableAnimation = GetAnimation(gObject.MName);
                            }
                            if (!_mCollectableLocations.Contains(gObject.MPosition))
                            {
                                _mCollectableLocations.Add(gObject.MPosition);
                            }
                        }
                        if (gObject.CollisionType == XmlKeys.Hazardous)
                        {
                            if (gObject is ReverseTile)
                            {
                                if (_mReverseHazardAnimation == null)
                                {
                                    _mReverseHazardAnimation = GetAnimation("ReverseMovingHazard");
                                }
                            }
                            else if (gObject is MovingTile)
                            {
                                if (_mHazardAnimation == null)
                                    _mHazardAnimation = GetAnimation("MovingHazard");
                            }
                        }
                        if (gObject is PhysicsObject)
                        {
                            var pObject = (PhysicsObject)gObject;

                            pObject.FixForBounds((int)Size.X, (int)Size.Y, IsMainMenu);
                            var oldPos = GridSpace.GetGridCoord(pObject.MPosition);

                            if (pObject is Player)
                            {
                                ((Player)pObject).CurrentTime = (int)MTimer;
                            }
                            pObject.Update(gameTime);

                            // Update zoom based on players velocity
                            pObject.FixForBounds((int)Size.X, (int)Size.Y, IsMainMenu);
                            UpdateCollisionMatrix(pObject, oldPos);

                            // handle collision right after you move
                            HandleCollisions(pObject, ref gameState);

                            if (pObject is Player)
                                foreach (var trigger in _mTrigger)
                                    trigger.RunTrigger(_mObjects, (Player)pObject);
                        }
                        if (!_mHasRespawned) break;
                    }

                    //Update wall animations
                    for(var i = 0; i < _mActiveAnimations.Count; i++)
                    {
                        var current = _mActiveAnimations.ElementAt(i);
                        current.Value.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
                        if (current.Value.Frame == 0 && current.Value.PreviousFrame == current.Value.LastFrame - 1)
                            _mActiveAnimations.Remove(current.Key);
                    }

                    //Update collectable animations
                    if (_mCollectableAnimation != null)
                        _mCollectableAnimation.Update((float)gameTime.ElapsedGameTime.TotalSeconds);

                    //Update hazard animations
                    if (_mHazardAnimation != null)
                        _mHazardAnimation.Update((float)gameTime.ElapsedGameTime.TotalSeconds);

                    if (_mReverseHazardAnimation != null)
                        _mReverseHazardAnimation.Update((float)gameTime.ElapsedGameTime.TotalSeconds);

                    //Check to see if we collected anything
                    if (_mRemoveCollected.Count > 0)
                    {
                        MNumCollected = MNumCollectable - (MNumCollectable - _mCollected.Count());

                        //Safely remove the collected objects
                        foreach (var g in _mRemoveCollected)
                        {
                            RemoveFromMatrix(g);
                            _mObjects.Remove(g);
                            _mCollectableLocations.Remove(g.MPosition);
                        }

                        //Then clear the list
                        _mRemoveCollected.Clear();
                    }

                    //Update number of deaths occured
                    MDeaths = 5 - _mPlayer.MNumLives;

                    // Update the camera to keep the player at the center of the screen
                    // Also only update if the velocity if greater than 0.5f in either direction
                    if (!IsMainMenu && (Math.Abs(_mPlayer.ObjectVelocity.X) > 0.5f || Math.Abs(_mPlayer.ObjectVelocity.Y) > 0.5f))
                    {
                       MCam.Position = new Vector3(_mPlayer.Position.X - 275, _mPlayer.Position.Y - 175, 0);
                    }
                    else if(IsMainMenu)
                    {
                        MCam.Position = new Vector3(_mPlayer.SpawnPoint.X - 275, _mPlayer.SpawnPoint.Y - 100, 0);
                    }

                    //Pause
                    if (_mControls.IsStartPressed(false) || Guide.IsVisible)
                        if (gameState == GameStates.InGame)
                            gameState = GameStates.Pause;
                }

                else if (_mDeathState == DeathStates.Respawning)
                {
                    _mDeathState = DeathStates.Panning;
                    Respawn();
                }

                else//Pan back to player after death
                {
                    MCam.Position += _mDeathPanLength;
                    _mDeathPanUpdates++;

                    if (_mDeathPanUpdates == ScalingFactor)
                        _mDeathState = DeathStates.Playing;
                }
            }

            if (!_mPlayer.MIsAlive)
            {
                _mPlayer.StopRumble();
                if (_mControls.IsAPressed(false))// resets game after game over
                {
                    _mPlayer.MNumLives = 5;
                    _mPlayer.MIsAlive = true;
                    MNumCollected = 0;
                    MTimer = 0;

                    //Add the collected objects back to the object list
                    foreach (var collected in _mCollected)
                        _mObjects.Add(collected);

                    //Reset the collision matrix
                    PrepareCollisionMatrix();

                    //Clear the collection lists
                    _mCollected.Clear();
                    _mRemoveCollected.Clear();
                }
            }

            _collectibleEngine.Update(0);
            _wallEngine.Update(0);
        }