Exemplo n.º 1
0
        public void DetectTouchedBlocks(ICollisionEngineContext context, IMovableObject checkedObject)
        {
            checkedObject.TouchedObjects.Clear();

            var areaToCheck = IntRectangle.FromPositionAndSize(checkedObject.Position, checkedObject.Size).Extend(Constants.BlockSize);

            var checkedObjBounds = checkedObject.Bounds;

            foreach (var obj in context.GetCollidableObjectsWithin(areaToCheck))
            {
                if (obj == checkedObject)
                {
                    continue;
                }

                if (obj.IsSolid)
                {
                    var objBounds = CoordinateSystem.Denormalize(obj.Bounds, checkedObjBounds);

                    if (checkedObjBounds.TryGetTouchedSide(objBounds, out var touchedSide))
                    {
                        checkedObject.TouchedObjects[touchedSide].Add(obj);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void MoveUp(ICollisionEngineContext context, IMovableObject movingObj, int distance)
        {
            var movingObjBounds = movingObj.Bounds;

            var sweptArea = new IntRectangle(movingObjBounds.Left, movingObjBounds.Top - distance, movingObjBounds.Front, movingObjBounds.Width, distance, movingObjBounds.Depth);

            var areaToCheck = sweptArea.Extend(Constants.BlockSize);

            foreach (var obj in context.GetCollidableObjectsWithin(areaToCheck))
            {
                if (obj == movingObj)
                {
                    continue;
                }

                var objBounds = CoordinateSystem.Denormalize(obj.Bounds, sweptArea);

                if (obj.IsSolid && sweptArea.Intersects(objBounds))
                {
                    sweptArea = new IntRectangle(sweptArea.Left, objBounds.Bottom, sweptArea.Front, sweptArea.Width, sweptArea.Bottom - objBounds.Bottom, sweptArea.Depth);
                }
            }

            movingObj.Position = movingObj.Position.MoveUp(sweptArea.Height);
        }
Exemplo n.º 3
0
 public void MoveVertical(ICollisionEngineContext context, IMovableObject movingObj, int offset)
 {
     if (offset > 0)
     {
         MoveDown(context, movingObj, offset);
     }
     else if (offset < 0)
     {
         MoveUp(context, movingObj, -offset);
     }
 }
Exemplo n.º 4
0
 public void MoveHorizontal(ICollisionEngineContext context, IMovableObject movingObj, int distance)
 {
     if (distance > 0)
     {
         MoveRight(context, movingObj, distance);
     }
     else if (distance < 0)
     {
         MoveLeft(context, movingObj, -distance);
     }
 }
Exemplo n.º 5
0
        public void Move(ICollisionEngineContext context, TimeSpan elspsedTime)
        {
            var leftMovingObjects  = new List <IMovableObject>();
            var rightMovingObjects = new List <IMovableObject>();
            var upMovingObjects    = new List <IMovableObject>();
            var downMovingObjects  = new List <IMovableObject>();

            foreach (var entity in context.GetMovableObjects())
            {
                if (entity.Velocity.X < 0)
                {
                    leftMovingObjects.Add(entity);
                }
                else if (entity.Velocity.X > 0)
                {
                    rightMovingObjects.Add(entity);
                }

                if (entity.Velocity.Y < 0)
                {
                    upMovingObjects.Add(entity);
                }
                else if (entity.Velocity.Y > 0)
                {
                    downMovingObjects.Add(entity);
                }
            }

            leftMovingObjects  = leftMovingObjects.OrderBy(entity => entity.Position.X).ToList();
            rightMovingObjects = rightMovingObjects.OrderByDescending(entity => entity.Position.X).ToList();
            upMovingObjects    = upMovingObjects.OrderBy(entity => entity.Position.Y).ToList();
            downMovingObjects  = downMovingObjects.OrderByDescending(entity => entity.Position.Y).ToList();

            foreach (var entity in leftMovingObjects)
            {
                MoveLeft(context, entity, (int)(-entity.Velocity.X * elspsedTime.TotalSeconds));
            }

            foreach (var entity in rightMovingObjects)
            {
                MoveRight(context, entity, (int)(entity.Velocity.X * elspsedTime.TotalSeconds));
            }

            foreach (var entity in upMovingObjects)
            {
                MoveUp(context, entity, (int)(-entity.Velocity.Y * elspsedTime.TotalSeconds));
            }

            foreach (var entity in downMovingObjects)
            {
                MoveDown(context, entity, (int)(entity.Velocity.Y * elspsedTime.TotalSeconds));
            }
        }
Exemplo n.º 6
0
 public void Move(ICollisionEngineContext context, IMovableObject movingObj, IntVector offset)
 {
     MoveHorizontal(context, movingObj, offset.X);
     MoveVertical(context, movingObj, offset.Y);
 }