Example #1
0
        //////////////////////////////////////////////////////////////////////////////

        #region Collider

        protected override bool HandleCollision(MovableCell other)
        {
            Debug.Assert(isActive);

            if (other.IsPlayer())
            {
                return(other.AsPlayer().HandleCollision(this));
            }

            if (other.IsBomb())
            {
                return(HandleCollision(other.AsBomb()));
            }

            if (other.IsObstacle())
            {
                return(HandleObstacleCollistion(other));
            }

            return(false);
        }
        protected override bool HandleCollision(MovableCell other)
        {
            Debug.Assert(isActive);

            if (other.IsPlayer())
            {
                return other.AsPlayer().HandleCollision(this);
            }

            if (other.IsBomb())
            {
                return HandleCollision(other.AsBomb());
            }

            if (other.IsObstacle())
            {
                return HandleObstacleCollistion(other);
            }

            return false;
        }
 private void RemoveMovable(MovableCell cell)
 {
     if (cell.addedToList)
     {
         movableCells.Remove(cell);
         cell.addedToList = false;
     }
 }
        private bool CheckWallCollisions(MovableCell movable)
        {
            float dx = movable.moveDx;
            float dy = movable.moveDy;

            if (dx > 0)
            {
                float maxX = GetMaxPx();
                if (movable.GetPx() > maxX)
                {
                    movable.SetPosX(maxX);
                    movable.HandleWallCollision();

                    return true;
                }
            }
            else if (dx < 0)
            {
                float minX = GetMinPx();
                if (movable.GetPx() < minX)
                {
                    movable.SetPosX(minX);
                    movable.HandleWallCollision();

                    return true;
                }
            }

            if (dy > 0)
            {
                float maxY = GetMaxPy();
                if (movable.GetPy() > maxY)
                {
                    movable.SetPosY(maxY);
                    movable.HandleWallCollision();

                    return true;
                }
            }
            else if (dy < 0)
            {
                float minY = GetMinPy();
                if (movable.GetPy() < minY)
                {
                    movable.SetPosY(minY);
                    movable.HandleWallCollision();

                    return true;
                }
            }

            return false;
        }
        private bool CheckStillCollisions(MovableCell movable, FieldCellSlot slot)
        {
            if (slot != null)
            {
                FieldCell staticCell = slot.staticCell;
                if (staticCell != null)
                {
                    return CheckCollision(movable, staticCell);
                }

                bool handled = false;

                List<MovableCell> movableCells = slot.movableCells;
                for (int i = 0; i < movableCells.Count; ++i)
                {
                    MovableCell other = movableCells[i];
                    if (!other.IsMoving() && movable != other)
                    {
                        handled |= CheckCollision(movable, other);
                    }
                }

                return handled;
            }

            return false;
        }
        /* Checks collision with static cells (bricks and solids) + still movable obstacles (not moving bombs) */
        private bool CheckStillCellsCollisions(MovableCell movable)
        {
            int cx = movable.cx;
            int cy = movable.cy;

            int stepX = Math.Sign(movable.px - movable.CellCenterPx());
            int stepY = Math.Sign(movable.py - movable.CellCenterPy());

            bool hasStepX = stepX != 0;
            bool hasStepY = stepY != 0;

            bool hasCollision = false;

            hasCollision |= CheckStillCollisions(movable, GetSlot(cx, cy));

            if (hasStepX && hasStepY)
            {
                hasCollision |= CheckStillCollisions(movable, GetSlot(cx + stepX, cy));
                hasCollision |= CheckStillCollisions(movable, GetSlot(cx, cy + stepY));
                hasCollision |= CheckStillCollisions(movable, GetSlot(cx + stepX, cy + stepY));
            }
            else if (hasStepX)
            {
                hasCollision |= CheckStillCollisions(movable, GetSlot(cx + stepX, cy));
            }
            else if (hasStepY)
            {
                hasCollision |= CheckStillCollisions(movable, GetSlot(cx, cy + stepY));
            }

            return hasCollision;
        }
 private bool CheckCollision(MovableCell c1, FieldCell c2)
 {
     return c1.Collides(c2) && c1.HandleCollision(c2);
 }
        private void AddMovable(MovableCell cell)
        {
            if (!cell.addedToList)
            {
                cell.addedToList = true;

                // added into the list sorted bombs first, players next
                for (LinkedListNode<MovableCell> node = movableCells.First; node != null; node = node.Next)
                {
                    if (cell.type <= node.Value.type)
                    {
                        movableCells.AddBefore(node, cell);
                        return;
                    }
                }
                movableCells.AddLast(cell);
            }
        }
 public void MovableCellChanged(MovableCell movable, int oldCx, int oldCy)
 {
     AddCell(movable);
 }
        public bool CheckStaticCollisions(MovableCell movable)
        {
            bool hasCollision = false;

            hasCollision |= CheckWallCollisions(movable);
            hasCollision |= CheckStillCellsCollisions(movable);

            return hasCollision;
        }
 /* Movable cell */
 protected virtual bool HandleCollision(MovableCell other)
 {
     return(false);
 }
        protected override bool HandleCollision(MovableCell other)
        {
            if (other.IsBomb())
            {
                return HandleCollision(other.AsBomb());
            }

            if (other.IsPlayer())
            {
                return HandleCollision(other.AsPlayer());
            }

            return false;
        }
 /* Movable cell */
 protected virtual bool HandleCollision(MovableCell other)
 {
     return false;
 }