This represents an object that exists in the game
Ejemplo n.º 1
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
            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);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Updates the collision matrix with the new position
        /// </summary>
        /// <param name="obj">Object we want to update</param>
        /// <param name="oldPosition">Position where object was before</param>
        private void UpdateCollisionMatrix(GameObject obj, Vector2 oldPosition)
        {
            var newPosition = GridSpace.GetGridCoord(obj.MPosition);
            if (oldPosition.Equals(newPosition)) return;

            _mCollisionMatrix[(int)oldPosition.Y][(int)oldPosition.X].Remove(obj);
            if(!_mCollisionMatrix[(int)newPosition.Y][(int)newPosition.X].Contains(obj))
                _mCollisionMatrix[(int)newPosition.Y][(int)newPosition.X].Add(obj);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads the level from the content manager
        /// </summary>
        /// <param name="content">Content Manager to load from</param>
        public void Load(ContentManager content, string assetName)
        {
            _mKootenay = content.Load<SpriteFont>("fonts/Kootenay");
            _mQuartz = content.Load<SpriteFont>("fonts/QuartzLarge");

            //            mDirections = new Texture2D[4];
            //            mDirections[3] = content.Load<Texture2D>("HUD/arrow_left");
            //            mDirections[2] = content.Load<Texture2D>("HUD/arrow_down");
            //            mDirections[1] = content.Load<Texture2D>("HUD/arrow_right");
            //            mDirections[0] = content.Load<Texture2D>("HUD/arrow_up");

            _mRailLeft = content.Load<Texture2D>("Images/NonHazards/Rails/RailLeft");
            _mRailHor = content.Load<Texture2D>("Images/NonHazards/Rails/RailHorizontal");
            _mRailRight = content.Load<Texture2D>("Images/NonHazards/Rails/RailRight");
            _mRailTop = content.Load<Texture2D>("Images/NonHazards/Rails/RailTop");
            _mRailBottom = content.Load<Texture2D>("Images/NonHazards/Rails/RailBottom");
            _mRailVert = content.Load<Texture2D>("Images/NonHazards/Rails/RailVertical");

            _mContent = content;

            MNumCollected = 0;
            MNumCollectable = 0;

            // Particle Engine
            var textures = new List<Texture2D>();
            textures.Add(content.Load<Texture2D>("Images/Particles/diamond"));
            textures.Add(content.Load<Texture2D>("Images/Particles/star"));
            _collectibleEngine = new ParticleEngine.ParticleEngine(textures, new Vector2(400, 240), 20);
            _collectibleEngine.ColorScheme = "Yellow";

            textures = new List<Texture2D>();
            textures.Add(content.Load<Texture2D>("Images/Particles/line"));
            textures.Add(content.Load<Texture2D>("Images/Particles/square"));
            _wallEngine = new ParticleEngine.ParticleEngine(textures, new Vector2(400, 240), 20);
            _wallEngine.ColorScheme = "Blue";

            _backGroundParticleCount = 500;
            _backgroundParticles = new Particle[_backGroundParticleCount];
            var random = new Random();
            var particle = content.Load<Texture2D>("Images/Particles/diamond");
            for (var i = 0; i < _backGroundParticleCount; i++)
            {
                var pos = new Vector2(random.Next(-(int)(_mBounds.Width + Size.X) / 2, 3 * (int)(_mBounds.Width + Size.X) / 2),
                    random.Next(-(int)(_mBounds.Height + Size.Y) / 2, 3 * (int)(_mBounds.Height + Size.Y) / 2));
                _backgroundParticles[i] = new Particle(particle, pos, random);
            }

            //lastCollided = new GameObject[2];
            //lastCollided[0] = lastCollided[1] = null;
            _lastCollided = null;

            _mCollectableLocations = new List<Vector2>();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Removes the object from the matrix(for collectables
        /// </summary>
        /// <param name="obj">Object we want to remove</param>
        private void RemoveFromMatrix(GameObject obj)
        {
            var position = GridSpace.GetGridCoord(obj.MPosition);

            _mCollisionMatrix[(int)position.Y][(int)position.X].Remove(obj);
        }