Beispiel #1
0
 public bool Intersects(FloatRect f)
 {
     if (right <= f.Left || left >= f.Right || top >= f.Bottom || bottom <= f.Top)
         return false;
     else
         return true;
 }
Beispiel #2
0
        public void UpdateCollision(ref Entity entity, InputManager inputManager, SoundEngine soundEngine)
        {
            FloatRect rect = new FloatRect(position.X, position.Y, layer.TileDimensions.X, layer.TileDimensions.Y);

            if (entity.OnTile && containsEntity)
            {
                if (!entity.SyncTilePosition)
                {
                    entity.Position += this.velocity;
                    entity.SyncTilePosition = true;
                }

                if (entity.Rect.Right < rect.Left || entity.Rect.Left > rect.Right || entity.Rect.Bottom != rect.Top)
                {
                    containsEntity = false;
                    entity.ActivateGravity = true;
                    entity.CanJump = false;
                }
            }

            if (entity.Rect.Intersects(rect) && state == State.Solid)
            {
                FloatRect preventity = new FloatRect(entity.PrevPosition.X, entity.PrevPosition.Y, entity.Animation.FrameWidth, entity.Animation.FrameHeight);

                FloatRect prevTile = new FloatRect(this.prevPosition.X, this.prevPosition.Y, layer.TileDimensions.X, layer.TileDimensions.Y);

                if (entity.Rect.Bottom >= rect.Top && preventity.Bottom <= prevTile.Top)
                {
                    //bottom collision
                    entity.Position = new Vector2(entity.Position.X, position.Y - entity.Animation.FrameHeight);
                    entity.ActivateGravity = false;
                    entity.OnTile = true;
                    containsEntity = true;
                }
                else if (entity.Rect.Top <= rect.Bottom && preventity.Top >= prevTile.Bottom)
                {
                    //top collision
                    entity.Position = new Vector2(entity.Position.X, position.Y + layer.TileDimensions.Y);
                    entity.ActivateGravity = true;
                }
                else if (entity.Rect.Right >= rect.Left && preventity.Right <= prevTile.Left)
                {
                    entity.Position = new Vector2(position.X - entity.Animation.FrameWidth, entity.Position.Y);
                    entity.Direction = (entity.Direction == 1) ? entity.Direction = 2 : entity.Direction = 1;
                    entity.CanJump = true;
                    entity.Velocity = new Vector2(0, entity.Velocity.Y);
                }
                else if (entity.Rect.Left <= rect.Right && preventity.Left >= prevTile.Left)
                {
                    entity.Position = new Vector2(position.X + layer.TileDimensions.X, entity.Position.Y);
                    entity.Direction = (entity.Direction == 1) ? entity.Direction = 2 : entity.Direction = 1;
                    entity.CanJump = true;
                    entity.Velocity = new Vector2(0, entity.Velocity.Y);
                }
            }
            else if (entity.Rect.Intersects(rect) && state == State.Platform)
            {
                FloatRect preventity = new FloatRect(entity.PrevPosition.X, entity.PrevPosition.Y, entity.Animation.FrameWidth, entity.Animation.FrameHeight);

                FloatRect prevTile = new FloatRect(this.prevPosition.X, this.prevPosition.Y, layer.TileDimensions.X, layer.TileDimensions.Y);

                if (entity.Rect.Bottom >= rect.Top && preventity.Bottom <= prevTile.Top && inputManager.KeyDown(Keys.Down))
                {
                    //bottom collision
                    //entity.Position = new Vector2(entity.Position.X, position.Y - entity.Animation.FrameHeight);
                    entity.ActivateGravity = true;
                    entity.OnTile = false;
                    containsEntity = false;
                }
                else if (entity.Rect.Bottom >= rect.Top && preventity.Bottom <= prevTile.Top)
                {
                    //bottom collision
                    entity.Position = new Vector2(entity.Position.X, position.Y - entity.Animation.FrameHeight);
                    entity.ActivateGravity = false;
                    entity.OnTile = true;
                    containsEntity = true;
                }
            }

            entity.Animation.Position = entity.Position;
        }