Пример #1
0
 //A space to be divided futher
 public Space(int _topLeftX, int _topLeftY, int _width, int _height)
 {
     spaceState = State.Empty;
     sprite = null;
     size = new Rect(_topLeftX, _topLeftY, _width, _height);
     subSpaces = new List<Space>();
 }
Пример #2
0
 //A space that is being used for a sprite.
 private Space(int _topLeftX, int _topLeftY, int _width, int _height, Sprite _sprite)
 {
     spaceState = State.Sprite;
     sprite = _sprite;
     size = new Rect(_topLeftX, _topLeftY, _width, _height);
     subSpaces = new List<Space>();
 }
Пример #3
0
        //Add a sprite to the space.
        //If the space isn't split then it is split and a space is made for the sprite.
        //If the space is split then we check if the sprite will fit into one of the child spaces
        public bool Add( Sprite _sprite )
        {
            switch (spaceState)
            {
                //if the space is not already split then try to split it up.
                case State.Empty:
                    return Split(_sprite);

                //If the space is already split then see if they is any room in the children spaces
                case State.Split:
                    return subSpaces.OrderByDescending(space => space.size.Area)
                                    .Any(s => s.Add(_sprite));

                default:
                    return false;
            }
        }
Пример #4
0
        private bool Split(Sprite _sprite)
        {
            //Can the sprite fit in the remaining space?
            if (size.w >= _sprite.PaddedWidth() && size.h >= _sprite.PaddedHeight())
            {
                //Make a space for the sprite and insert it.
                subSpaces.Add(new Space(size.x,
                                        size.y,
                                        _sprite.PaddedWidth(),
                                        _sprite.PaddedHeight(),
                                        _sprite));

                //Carve up the remaining space so they can be used.
                if ((size.w - _sprite.Width) > 0)
                {
                    subSpaces.Add(new Space(size.x + _sprite.PaddedWidth(),
                                            size.y ,
                                            size.w - _sprite.PaddedWidth(),
                                            size.h));
                }
                if ((size.h - _sprite.Height) > 0)
                {
                    subSpaces.Add(new Space(size.x ,
                                            size.y + _sprite.PaddedHeight(),
                                            _sprite.PaddedWidth(),
                                            size.h - _sprite.PaddedHeight()));
                }

                //We have successfully fitted the sprite and the space is split
                spaceState = State.Split;
                return true;
            }
            //Space was too small to be split.
            spaceState = State.Empty;
            return false;
        }