示例#1
0
        /// <summary>
        /// Checks to see if the given object is in bounds or not
        /// </summary>
        /// <param name="objectToCheck">Object we are checking for collisions with</param>
        public void CheckBounds(PhysicsObject objectToCheck)
        {
            Rectangle currentBoundingBox = objectToCheck.BoundingBox;

            Vector2 dPos = objectToCheck.ObjectVelocity;

            Color[] mapData = new Color[currentBoundingBox.Width * currentBoundingBox.Height];

            while (isTouchingBounds(new Rectangle(
                currentBoundingBox.X,currentBoundingBox.Y,
                currentBoundingBox.Width,currentBoundingBox.Height), dPos, mapData))
                    dPos = Vector2.Multiply(dPos, .75f);

            objectToCheck.ObjectVelocity = dPos;
        }
 /// <summary>
 /// Returns true if the physics objects are colliding with each other
 /// 
 /// TODO - Add pixel perfect collision
 /// </summary>
 /// <param name="otherObject">The other object to test against</param>
 /// <returns>True if they are colliding with each other; False otherwise</returns>
 public virtual bool IsColliding(PhysicsObject otherObject)
 {
     return !Equals(otherObject) && mBoundingBox.Intersects(otherObject.mBoundingBox);
 }
示例#3
0
        /// <summary>
        /// Checks to see if given object is colliding with any other object and handles the collision
        /// </summary>
        /// <param name="physObj">object to see if anything is colliding with it</param>
        private void HandleCollisions(PhysicsObject physObj, ref GameStates gameState)
        {
            // keep track of all object colliding with physObj
            List<GameObject> collidingList = new List<GameObject>();

            Vector2 gridPos = GridSpace.GetGridCoord(physObj.mPosition);

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

                    foreach (GameObject obj in mCollisionMatrix[(int)gridPos.Y+i][(int)gridPos.X+j])
                    {
                        bool 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.level_stageVictory);
                            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.playerCol_collectable.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.playerSound_death.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, SCALING_FACTOR);
                            //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 (GameObject cObject in collidingList)
                        {
                            if (cObject is Wall)
                            {
                                KeyValuePair<Vector2, string> 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)
                                {
                                    Vector2 one = new Vector2(mPlayer.Position.X + 32, mPlayer.Position.Y + 32);
                                    Vector2 two = new Vector2(animation.Key.X + 32, animation.Key.Y + 32);
                                    Vector2 midpoint = new Vector2((one.X + two.X) / 2, (one.Y + two.Y) / 2);
                                    wallEngine.EmitterLocation = midpoint;
                                    wallEngine.Update(10);

                                    // play wall collision sound
                                    GameSound.playerCol_wall.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)
                                {
                                    Vector2 one = new Vector2(mPlayer.Position.X + 32, mPlayer.Position.Y + 32);
                                    Vector2 two = new Vector2(cObject.mPosition.X + 32, cObject.mPosition.Y + 32);
                                    Vector2 midpoint = new Vector2((one.X + two.X) / 2, (one.Y + two.Y) / 2);
                                    wallEngine.EmitterLocation = midpoint;
                                    wallEngine.Update(10);

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

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

                                }
                            }
                        }

                    physObj.HandleCollisionList(collidingList);
                }
            }
        }