MrGravity.Level.HandleCollisions C# (CSharp) Method

HandleCollisions() private method

Checks to see if given object is colliding with any other object and handles the collision
private HandleCollisions ( PhysicsObject physObj, GameStates &gameState ) : void
physObj MrGravity.Game_Objects.Physics_Objects.PhysicsObject object to see if anything is colliding with it
gameState GameStates
return void
        private void HandleCollisions(PhysicsObject physObj, ref GameStates gameState)
        {
            // keep track of all object colliding with physObj
            var collidingList = new List<GameObject>();

            var gridPos = GridSpace.GetGridCoord(physObj.MPosition);

            //Goes through the 9 possible positions for collision to see if this physics object is colliding with anything
            for (var i = -1; i < 2; i++)
            {
                if (gridPos.Y + i < 0 || gridPos.Y + i >= _mCollisionMatrix.Length) continue;//Bounds check
                for (var j = -1; j < 2; j++)
                {
                    if (gridPos.X + j < 0 || gridPos.X + j >= _mCollisionMatrix[(int)gridPos.Y + i].Length) continue;//Bounds check

                    foreach (var obj in _mCollisionMatrix[(int)gridPos.Y+i][(int)gridPos.X+j])
                    {
                        var collided = false;

                        if (physObj.IsSquare && obj.IsSquare)// both squares
                        {
                            collided = physObj.IsCollidingBoxAndBox(obj);
                        }
                        else if (!physObj.IsSquare && obj.IsSquare) // phys obj is circle
                        {
                            collided = physObj.IsCollidingCircleAndBox(obj);
                        }
                        else if (physObj.IsSquare && !obj.IsSquare) //obj is circle
                        {
                            collided = physObj.IsCollidingBoxAndCircle(obj);
                        }
                        else // both circles
                        {
                            collided = physObj.IsCollidingCircleandCircle(obj);
                        }

                        if (obj.Equals(physObj) || obj is PlayerEnd && !(physObj is Player))
                            continue;

                        if (collided && !(obj is PlayerEnd))
                        {
                            collidingList.Add(obj);
                        }

                        //If player reaches the end, set the timer to 0
                        if (collided && obj is PlayerEnd && physObj is Player)
                        {
                            _mPlayer.MCurrentTexture = PlayerFaces.FromString("Laugh");
                            _mPlayerEnd.MCurrentTexture = PlayerFaces.FromString("GirlLaugh3");

                            GameSound.StopOthersAndPlay(GameSound.LevelStageVictory);
                            _mPhysicsEnvironment.GravityDirection = GravityDirections.Down;
                            gameState = GameStates.Unlock;
                        }

                        //If player collided with a collectable object
                        if (collided && ((physObj is Player) && obj.CollisionType == XmlKeys.Collectable || (obj is Player) && physObj.CollisionType == XmlKeys.Collectable))
                        {
                            if (physObj.CollisionType == XmlKeys.Collectable)
                            {
                                _mCollected.Add(physObj);
                                _mRemoveCollected.Add(physObj);
                                _mCollectableLocations.Remove(physObj.MPosition);
                            }
                            else if (obj.CollisionType == XmlKeys.Collectable)
                            {
                                _mCollected.Add(obj);
                                _mRemoveCollected.Add(obj);
                                _mCollectableLocations.Remove(obj.MPosition);
                            }

                            GameSound.PlayerColCollectable.Play(GameSound.Volume * 0.8f, 0f, 0f);
                            _collectibleEngine.EmitterLocation = new Vector2(obj.MPosition.X + 32, obj.MPosition.Y + 32);
                            _collectibleEngine.Update(10);
                        }
                        //If player hits a hazard
                        else if (collided && ((physObj is Player) && obj.CollisionType == XmlKeys.Hazardous || (obj is Player) && physObj.CollisionType == XmlKeys.Hazardous))
                        {
                            // Particle Effects (don't work).
                            //Vector2 one = new Vector2(obj.mPosition.X + 32, obj.mPosition.Y + 32);
                            //Vector2 two = new Vector2(physObj.mPosition.X + 32, physObj.mPosition.Y + 32);
                            //Vector2 midpoint = new Vector2((one.X + two.X) / 2, (one.Y + two.Y) / 2);
                            //wallEngine.EmitterLocation = midpoint;
                            //wallEngine.Update(10);
                            GameSound.PlayerSoundDeath.Play(GameSound.Volume * 0.8f, 0.0f, 0.0f);

                            if (physObj is Player)
                            {
                                physObj.Kill();
                                _mPlayerEnd.MCurrentTexture = PlayerFaces.FromString("GirlSad");
                            }
                            else
                            {
                                ((Player)obj).Kill();
                                _mPlayerEnd.MCurrentTexture = PlayerFaces.FromString("GirlSad");
                            }

                            //Get difference of two positions
                            _mDeathPanLength = Vector3.Subtract(new Vector3(_mPlayer.SpawnPoint.X - 275, _mPlayer.SpawnPoint.Y - 100, 0), MCam.Position);
                            //Divide by scaling factor to get camera pan at each update.
                            _mDeathPanLength = Vector3.Divide(_mDeathPanLength, ScalingFactor);
                            //Set the update counter to zero
                            _mDeathPanUpdates = 0;

                            gameState = GameStates.Death;
                            _mDeathState = DeathStates.Respawning;

                            _mHasRespawned = false;

                            return;
                        }

                    }

                    //Start any animations on walls we are touching
                    if (physObj is Player)
                        foreach (var cObject in collidingList)
                        {
                            if (cObject is Wall)
                            {
                                var animation = ((Wall)cObject).NearestWallPosition(physObj.MPosition);
                                if (!_mActiveAnimations.ContainsKey(animation.Key))
                                    _mActiveAnimations.Add(animation.Key, GetAnimation(animation.Value));

                                // Particle Effects.
                                //if (cObject != lastCollided[0] && cObject != lastCollided[1])
                                if (cObject != _lastCollided)
                                {
                                    var one = new Vector2(_mPlayer.Position.X + 32, _mPlayer.Position.Y + 32);
                                    var two = new Vector2(animation.Key.X + 32, animation.Key.Y + 32);
                                    var midpoint = new Vector2((one.X + two.X) / 2, (one.Y + two.Y) / 2);
                                    _wallEngine.EmitterLocation = midpoint;
                                    _wallEngine.Update(10);

                                    // play wall collision sound
                                    GameSound.PlayerColWall.Play(GameSound.Volume * 0.8f, 0f, 0f);

                                    //lastCollided[1] = lastCollided[0];
                                    //lastCollided[0] = cObject;
                                    _lastCollided = cObject;

                                }
                            }

                            else if (cObject is MovingTile && !((MovingTile)cObject).BeingAnimated && cObject.CollisionType != XmlKeys.Hazardous)
                                ((MovingTile)cObject).StartAnimation(GetAnimation(cObject.MName));
                            else if (cObject is ReverseTile && !((ReverseTile)cObject).BeingAnimated && cObject.CollisionType != XmlKeys.Hazardous)
                                ((ReverseTile)cObject).StartAnimation(GetAnimation(cObject.MName));
                            else if (cObject is StaticObject && cObject.CollisionType != XmlKeys.Collectable)
                            {
                                if (!_mActiveAnimations.ContainsKey(cObject.MPosition))
                                    _mActiveAnimations.Add(cObject.MPosition, GetAnimation(cObject.MName));

                                // Particle Effects.
                                //if (cObject != lastCollided[0] && cObject != lastCollided[1])
                                if (cObject != _lastCollided)
                                {
                                    var one = new Vector2(_mPlayer.Position.X + 32, _mPlayer.Position.Y + 32);
                                    var two = new Vector2(cObject.MPosition.X + 32, cObject.MPosition.Y + 32);
                                    var midpoint = new Vector2((one.X + two.X) / 2, (one.Y + two.Y) / 2);
                                    _wallEngine.EmitterLocation = midpoint;
                                    _wallEngine.Update(10);

                                    // play wall collision sound
                                    GameSound.PlayerColWall.Play(GameSound.Volume * 0.8f, 0f, 0f);

                                    //lastCollided[1] = lastCollided[0];
                                    //lastCollided[0] = cObject;
                                    _lastCollided = cObject;

                                }
                            }
                        }

                    physObj.HandleCollisionList(collidingList);
                }
            }
        }