示例#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;
 }
示例#2
0
        public void UpdateCollision(ref Entity e)
        {
            FloatRect rect = new FloatRect(position.X, position.Y, Layer.TileDimensions.X, Layer.TileDimensions.Y);

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

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

            }
            if (e.Rect.Intersects(rect) && state == State.Solid)
            {
                FloatRect preve = new FloatRect(e.PrevPosition.X, e.PrevPosition.Y, e.Animation.FrameWidth, e.Animation.FrameHeight);
                FloatRect prevTile = new FloatRect(prevPosition.X, prevPosition.Y, Layer.TileDimensions.X, Layer.TileDimensions.Y);

                if (e.Rect.Bottom >= rect.Top && preve.Bottom <= prevTile.Top) //if bottom of player is lower than top of tile and prev player bottom was above prev tile top
                {
                    e.Position = new Vector2(e.Position.X, position.Y - e.Animation.FrameHeight);
                    e.ActivateGravity = false;
                    e.OnTile = true;
                    containsEntity = true;
                }
                else if (e.Rect.Top <= rect.Bottom && preve.Top >= prevTile.Bottom)
                {
                    e.Position = new Vector2(e.Position.X, position.Y + Layer.TileDimensions.Y);
                    e.Velocity = new Vector2(e.Velocity.X, 0);
                    e.ActivateGravity = true;
                }
                else if(e.Rect.Right >= rect.Left && preve.Right <= prevTile.Left)
                {
                    e.Position = new Vector2(position.X - e.Animation.FrameWidth, e.Position.Y);
                    e.Direction = (e.Direction == 1) ? e.Direction = 2 : e.Direction = 1;
                }
                else if(e.Rect.Left <= rect.Right && preve.Left >= prevTile.Left)
                {
                    e.Position = new Vector2(position.X + Layer.TileDimensions.X, e.Position.Y);
                    e.Direction = (e.Direction == 1) ? e.Direction = 2 : e.Direction = 1;
                }
            }
            e.Animation.Position = e.Position;

        }