Пример #1
0
        private bool HandleCollision(Bomb other)
        {
            bool moving1 = IsMoving();
            bool moving2 = other.IsMoving();

            Debug.Assert(moving1 || moving2);

            if (moving1 && moving2)
            {
                if (direction == Util.Opposite(other.direction)) // moving in opposite directions
                {
                    bool backBlocked1 = HasJellyBlockingObstacle(Util.Opposite(direction));
                    bool backBlocked2 = other.HasJellyBlockingObstacle(Util.Opposite(other.direction));

                    bool cell2cell = CheckCell2CellCollision(other);
                    bool bounds2cell = CheckBounds2CellCollision(other);
                    bool cell2bounds = CheckCell2BoundsCollision(other);
                    bool bounds2bounds = CheckBounds2BoundsCollision(other);

                    if (backBlocked1 && backBlocked2)
                    {
                        if (cell2cell)
                        {
                            switch (direction)
                            {
                                case Direction.LEFT:
                                case Direction.RIGHT:
                                {
                                    float shift = 0.5f * OverlapX(other);
                                    MoveBackX(shift);
                                    other.MoveBackX(shift);
                                    break;
                                }

                                case Direction.UP:
                                case Direction.DOWN:
                                {
                                    float shift = 0.5f * OverlapY(other);
                                    MoveBackY(shift);
                                    other.MoveBackY(shift);
                                    break;
                                }
                            }
                        }
                    }
                    else if (backBlocked1)
                    {
                        if (bounds2cell)
                        {
                            HandleObstacleCollistion(other);
                            if (bounds2bounds)
                            {
                                other.HandleObstacleCollistion(this);
                            }

                            return true;
                        }
                    }
                    else if (backBlocked2)
                    {
                        if (cell2bounds)
                        {
                            other.HandleObstacleCollistion(this);
                            if (bounds2bounds)
                            {
                                HandleObstacleCollistion(other);
                            }

                            return true;
                        }
                    }
                    else
                    {
                        if (!bounds2bounds)
                        {
                            return false;
                        }
                    }

                    HandleObstacleCollistion(other);
                    other.HandleObstacleCollistion(this);

                    return true;
                }
                else if (direction == other.direction)
                {
                    if (IsMovingTowards(other))
                    {
                        HandleObstacleCollistion(other);
                        return true;
                    }

                    other.HandleObstacleCollistion(this);
                    return true;
                }
                else // perpendicular directions
                {
                    if (CheckBounds2CellCollision(other))
                    {
                        if (IsMovingTowards(other))
                        {
                            HandleObstacleCollistion(other);
                            return true;
                        }
                    }
                    else if (other.CheckBounds2CellCollision(this))
                    {
                        if (other.IsMovingTowards(this))
                        {
                            other.HandleObstacleCollistion(this);
                            return true;
                        }
                    }

                    if (CheckBounds2BoundsCollision(this))
                    {
                        if (IsMovingTowards(other))
                        {
                            HandleObstacleCollistion(other);
                            return true;
                        }

                        if (other.IsMovingTowards(this))
                        {
                            other.HandleObstacleCollistion(this);
                            return true;
                        }
                    }
                }

                return false;
            }

            if (moving1)
            {
                if (IsMovingTowards(other))
                {
                    return HandleObstacleCollistion(other);
                }

                return false;
            }

            if (moving2)
            {
                if (other.IsMovingTowards(this))
                {
                    return other.HandleObstacleCollistion(this);
                }
                return false;
            }

            return false;
        }
Пример #2
0
        private bool HandleCollision(Bomb other)
        {
            bool moving1 = IsMoving();
            bool moving2 = other.IsMoving();

            Debug.Assert(moving1 || moving2);

            if (moving1 && moving2)
            {
                if (direction == Util.Opposite(other.direction)) // moving in opposite directions
                {
                    bool backBlocked1 = HasJellyBlockingObstacle(Util.Opposite(direction));
                    bool backBlocked2 = other.HasJellyBlockingObstacle(Util.Opposite(other.direction));

                    bool cell2cell     = CheckCell2CellCollision(other);
                    bool bounds2cell   = CheckBounds2CellCollision(other);
                    bool cell2bounds   = CheckCell2BoundsCollision(other);
                    bool bounds2bounds = CheckBounds2BoundsCollision(other);

                    if (backBlocked1 && backBlocked2)
                    {
                        if (cell2cell)
                        {
                            switch (direction)
                            {
                            case Direction.LEFT:
                            case Direction.RIGHT:
                            {
                                float shift = 0.5f * OverlapX(other);
                                MoveBackX(shift);
                                other.MoveBackX(shift);
                                break;
                            }

                            case Direction.UP:
                            case Direction.DOWN:
                            {
                                float shift = 0.5f * OverlapY(other);
                                MoveBackY(shift);
                                other.MoveBackY(shift);
                                break;
                            }
                            }
                        }
                    }
                    else if (backBlocked1)
                    {
                        if (bounds2cell)
                        {
                            HandleObstacleCollistion(other);
                            if (bounds2bounds)
                            {
                                other.HandleObstacleCollistion(this);
                            }

                            return(true);
                        }
                    }
                    else if (backBlocked2)
                    {
                        if (cell2bounds)
                        {
                            other.HandleObstacleCollistion(this);
                            if (bounds2bounds)
                            {
                                HandleObstacleCollistion(other);
                            }

                            return(true);
                        }
                    }
                    else
                    {
                        if (!bounds2bounds)
                        {
                            return(false);
                        }
                    }

                    HandleObstacleCollistion(other);
                    other.HandleObstacleCollistion(this);

                    return(true);
                }
                else if (direction == other.direction)
                {
                    if (IsMovingTowards(other))
                    {
                        HandleObstacleCollistion(other);
                        return(true);
                    }

                    other.HandleObstacleCollistion(this);
                    return(true);
                }
                else // perpendicular directions
                {
                    if (CheckBounds2CellCollision(other))
                    {
                        if (IsMovingTowards(other))
                        {
                            HandleObstacleCollistion(other);
                            return(true);
                        }
                    }
                    else if (other.CheckBounds2CellCollision(this))
                    {
                        if (other.IsMovingTowards(this))
                        {
                            other.HandleObstacleCollistion(this);
                            return(true);
                        }
                    }

                    if (CheckBounds2BoundsCollision(this))
                    {
                        if (IsMovingTowards(other))
                        {
                            HandleObstacleCollistion(other);
                            return(true);
                        }

                        if (other.IsMovingTowards(this))
                        {
                            other.HandleObstacleCollistion(this);
                            return(true);
                        }
                    }
                }

                return(false);
            }

            if (moving1)
            {
                if (IsMovingTowards(other))
                {
                    return(HandleObstacleCollistion(other));
                }

                return(false);
            }

            if (moving2)
            {
                if (other.IsMovingTowards(this))
                {
                    return(other.HandleObstacleCollistion(this));
                }
                return(false);
            }

            return(false);
        }