Module: Rubygame::Sprites::LimitGroup
- Defined in:
- lib/rubygame/sprite.rb
Overview
LimitGroup is a mix-in module that extends Group to limit the number of sprites it can contain. If the limit has been reached, each new sprite will “push out” the oldest sprite, on a First-In-First-Out basis.
The limit can be set either when the LimitGroup is created, or at any time during execution. However, if you reduce the limit, excess sprites will not be removed until the next time a sprite is added. (You can work around this by pushing the last sprite in the group again; the duplicate will be removed.)
Please note that, because Group#push is defined in terms of Group#<<, both LimitGroup#<< and LimitGroup#push work properly, even though only LimitGroup#<< is defined.
Instance Attribute Summary collapse
-
#limit ⇒ Object
Returns the value of attribute limit.
Class Method Summary collapse
-
.extend_object(obj) ⇒ Object
Defines and sets @limit = 1 when an existing object is extended.
Instance Method Summary collapse
-
#<<(sprite) ⇒ Object
Add
spriteto the LimitGroup, removing the oldest member if necessary to keep under the limit. -
#initialize(limit = 1) ⇒ Object
Initialize the LimitGroup and define @limit (as 1, by default).
Instance Attribute Details
#limit ⇒ Object
Returns the value of attribute limit.
443 444 445 |
# File 'lib/rubygame/sprite.rb', line 443 def limit @limit end |
Class Method Details
.extend_object(obj) ⇒ Object
Defines and sets @limit = 1 when an existing object is extended.
446 447 448 449 |
# File 'lib/rubygame/sprite.rb', line 446 def LimitGroup.extend_object(obj) super obj.limit = 1 end |
Instance Method Details
#<<(sprite) ⇒ Object
Add sprite to the LimitGroup, removing the oldest member if necessary to keep under the limit. If sprite is already in the LimitGroup, it will be moved to the top of the queue.
459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/rubygame/sprite.rb', line 459 def <<(sprite) if not include? sprite super(sprite) while length > @limit self.slice!(0) end else # move sprite to the back of the queue self.delete(sprite) super(sprite) end end |
#initialize(limit = 1) ⇒ Object
Initialize the LimitGroup and define @limit (as 1, by default).
452 453 454 |
# File 'lib/rubygame/sprite.rb', line 452 def initialize(limit=1) @limit = limit end |