Example #1
0
        /// <summary>
        /// Checks if the player hits the ground or a planet.
        /// </summary>
        /// <param name="p">The player.</param>
        private void CheckAirCollision(Player p)
        {
            if (CollidesWithBorders(p))
            {
                PlacePlayerInsideLevel(p);
                if (PhysicsEngine.CollidesWithBottomBorder(p, model.Terrain))
                {
                    p.IsStandingOn = GroundTypes.Ground;
                }
            }

            p.Position += p.TotalVelocity; //Check if next position collides with terrain
            bool collision = CollisionEngine.IntersectPixels(p.WorldTransform, (int)p.Size.X, (int)p.Size.Y, p.ColorData, model.Terrain);

            //bool collision = CollisionEngine.BoundingBoxTerrainCollision(p.BoundingBox, model.Terrain);
            if (collision) //check player vs terrain
            {
                Rectangle bottomHalf = PhysicsEngine.GetBottomHalf(p.BoundingBox);
                if (BunnyGame.DebugMode)
                {
                    model.View.DrawRectangle(bottomHalf, Color.Green);
                }
                bool bottomCollision = CollisionEngine.BoundingBoxTerrainCollision(bottomHalf, model.Terrain);
                p.Position -= p.TotalVelocity;
                if (bottomCollision)
                { //bottom half collides with terrain
                    PlacePlayerOnGround(p);
                }
                else
                { //top half collides with terrain
                    p.SelfVelocity = -p.SelfVelocity;
                    p.Velocity     = -p.Velocity;
                }
            }
            else
            { //Check player vs. planets
                Planet planet = CollidesWithPlanet(p);
                if (planet != null)
                {
                    PlacePlayerOnPlanet(p, planet);
                    collision = true;
                }
                if (!collision)
                {
                    p.Position -= p.TotalVelocity;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Places an entity in the level so its bounding box is fully inside the level and not colliding with the terrain.
        /// </summary>
        /// <param name="e">The entity to be placed.</param>
        public void PlaceEntity(Entity e)
        {
            int  timeOutCounter = 100;
            bool freespaceFound = true;

            e.Position = new Vector2(rand.Next(Terrain.Width), rand.Next(Terrain.Height));
            do
            {
                if (CollisionEngine.BoundingBoxTerrainCollision(e.BoundingBox, Terrain) || !Terrain.IsRectangleClear(e.BoundingBox))
                {
                    freespaceFound = false;
                }
                else
                {
                    foreach (Entity e2 in EntityList)
                    {
                        if (e.BoundingBox.Intersects(e2.BoundingBox))
                        {
                            freespaceFound = false;
                        }
                    }
                }
                timeOutCounter--;
                if (timeOutCounter == 0)
                {
                    DestroyCircle((int)e.Position.X, (int)e.Position.Y, (int)(MathHelper.Max(e.Size.X, e.Size.Y) / 2));
                    freespaceFound = true;
                }
                if (!freespaceFound)
                {
                    e.Position -= new Vector2(0, e.Size.Y / 2);
                    if (e.Position.Y - e.Origin.Y < 0)
                    {
                        e.Position = new Vector2(rand.Next(Terrain.Width), rand.Next(Terrain.Height));
                    }
                }
            } while (!freespaceFound);
        }