private bool handleEntityFloorCollisions(PhysicsEntity regEnt, IEnumerable<Floor> floors)
        {
            bool collisionDetected = false;

            foreach (Floor floor in floors)
            {
                Vector2 collision;

                // Do not cross the floor
                if (Hitbox.Collide(regEnt.Hitbox, floor.Rectangle, out collision))
                {
                    float absDepthX = Math.Abs(collision.X);
                    float absDepthY = Math.Abs(collision.Y);

                    // Resolve the collision along the shallow axis.
                    if (absDepthY <= absDepthX)
                    {
                        bool floorOnTop = floor.Rectangle.Y < regEnt.Hitbox.Dimensions.Y;

                        // If we crossed the top of a tile, we are on the ground.
                        if (floorOnTop == false)
                        {
                            // Hack for passable platforms
                            if (floor.IsPassable == false)
                            {
                                regEnt.IsOnGround = true;
                            }
                            else if (regEnt.Velocity.Y >= 0)
                            {
                                regEnt.IsOnGround = true;
                            }
                        }

                        if (floor.IsPassable == false || regEnt.IsOnGround)
                        {
                            regEnt.Location = new Vector2(regEnt.Location.X, regEnt.Location.Y + collision.Y);

                            if (absDepthY != 0)
                            {
                                regEnt.Velocity = new Vector2(regEnt.Velocity.X, 0f);
                            }
                        }
                    }
                    else
                    {
                        regEnt.Location = new Vector2(regEnt.Location.X + collision.X, regEnt.Location.Y);

                        bool passable = (floor.IsPassable == true);

                        if (!passable)
                        {
                            // If going in the direction of the collision
                            if (floor.Rectangle.X > regEnt.Hitbox.Dimensions.X)
                            {
                                regEnt.IsStuckRight = true;
                            }
                            else if (floor.Rectangle.X < regEnt.Hitbox.Dimensions.X)
                            {
                                regEnt.IsStuckLeft = true;
                            }

                            if (absDepthX != 0)
                            {
                                regEnt.Velocity = new Vector2(0f, regEnt.Velocity.Y);
                            }
                        }
                    }

                    regEnt.Hitbox.UpdateBounds();

                    if (regEnt.FloorCollisionDetected != null)
                    {
                        regEnt.FloorCollisionDetected(collision);
                    }

                    if (regEnt.IsAlive == false) break;
                    collisionDetected = true;
                }
            }

            return collisionDetected;
        }
Beispiel #2
0
        private bool handleEntityFloorCollisions(PhysicsEntity regEnt, IEnumerable <Floor> floors)
        {
            bool collisionDetected = false;

            foreach (Floor floor in floors)
            {
                Vector2 collision;

                // Do not cross the floor
                if (Hitbox.Collide(regEnt.Hitbox, floor.Rectangle, out collision))
                {
                    float absDepthX = Math.Abs(collision.X);
                    float absDepthY = Math.Abs(collision.Y);

                    // Resolve the collision along the shallow axis.
                    if (absDepthY <= absDepthX)
                    {
                        bool floorOnTop = floor.Rectangle.Y < regEnt.Hitbox.Dimensions.Y;

                        // If we crossed the top of a tile, we are on the ground.
                        if (floorOnTop == false)
                        {
                            // Hack for passable platforms
                            if (floor.IsPassable == false)
                            {
                                regEnt.IsOnGround = true;
                            }
                            else if (regEnt.Velocity.Y >= 0)
                            {
                                regEnt.IsOnGround = true;
                            }
                        }

                        if (floor.IsPassable == false || regEnt.IsOnGround)
                        {
                            regEnt.Location = new Vector2(regEnt.Location.X, regEnt.Location.Y + collision.Y);

                            if (absDepthY != 0)
                            {
                                regEnt.Velocity = new Vector2(regEnt.Velocity.X, 0f);
                            }
                        }
                    }
                    else
                    {
                        regEnt.Location = new Vector2(regEnt.Location.X + collision.X, regEnt.Location.Y);

                        bool passable = (floor.IsPassable == true);

                        if (!passable)
                        {
                            // If going in the direction of the collision
                            if (floor.Rectangle.X > regEnt.Hitbox.Dimensions.X)
                            {
                                regEnt.IsStuckRight = true;
                            }
                            else if (floor.Rectangle.X < regEnt.Hitbox.Dimensions.X)
                            {
                                regEnt.IsStuckLeft = true;
                            }

                            if (absDepthX != 0)
                            {
                                regEnt.Velocity = new Vector2(0f, regEnt.Velocity.Y);
                            }
                        }
                    }

                    regEnt.Hitbox.UpdateBounds();

                    if (regEnt.FloorCollisionDetected != null)
                    {
                        regEnt.FloorCollisionDetected(collision);
                    }

                    if (regEnt.IsAlive == false)
                    {
                        break;
                    }
                    collisionDetected = true;
                }
            }

            return(collisionDetected);
        }