Class: OR2D::Composite
- Inherits:
-
Object
- Object
- OR2D::Composite
- Defined in:
- lib/or2d/composite.rb
Overview
A Composite object behaves like a collection of Entity objects.
Direct Known Subclasses
OR2D::Composites::Projectile, OR2D::Composites::Sprite, OR2D::Composites::Text
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
The identifier of the Composite object.
-
#layers ⇒ Hash
readonly
The layers of the Composite object.
Instance Method Summary collapse
-
#add_layer(type, options, entity: nil) ⇒ Object
Add a layer to the Composite object.
-
#destroy ⇒ Object
Destroys the Composite object and all of its Entity objects.
-
#hide ⇒ Object
Hide the Composite object.
-
#initialize(id = "Composite_#{SecureRandom.uuid}") ⇒ Composite
constructor
Constructs a new Composite object.
-
#remove_layer(id) ⇒ Object
Remove a layer from the Composite object.
-
#show ⇒ Object
Show the Composite object.
-
#toggle(id) ⇒ Object
Toggle the visibility of a specific layer.
Constructor Details
#initialize(id = "Composite_#{SecureRandom.uuid}") ⇒ Composite
Constructs a new Composite object.
14 15 16 17 |
# File 'lib/or2d/composite.rb', line 14 def initialize(id = "Composite_#{SecureRandom.uuid}") @id = id @layers = {} end |
Instance Attribute Details
#id ⇒ String (readonly)
Returns the identifier of the Composite object.
11 12 13 |
# File 'lib/or2d/composite.rb', line 11 def id @id end |
#layers ⇒ Hash (readonly)
Returns the layers of the Composite object.
7 8 9 |
# File 'lib/or2d/composite.rb', line 7 def layers @layers end |
Instance Method Details
#add_layer(type, options, entity: nil) ⇒ Object
Add a layer to the Composite object.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/or2d/composite.rb', line 23 def add_layer(type, , entity: nil) entity = if type == :entity entity else OR2D::Entity.new(type, ) end @layers[entity.id] = { show: [:show].nil? ? true : [:show] } entity.id end |
#destroy ⇒ Object
Destroys the Composite object and all of its Entity objects.
64 65 66 67 68 69 70 71 |
# File 'lib/or2d/composite.rb', line 64 def destroy @layers.each_key do |id| next if OR2D.game.entities[id].nil? OR2D.game.entities[id].destroy OR2D.game.remove_entity(id) end end |
#hide ⇒ Object
Hide the Composite object.
50 51 52 53 54 |
# File 'lib/or2d/composite.rb', line 50 def hide @layers.each_key do |id| OR2D.game.entities[id].hide end end |
#remove_layer(id) ⇒ Object
Remove a layer from the Composite object.
36 37 38 39 40 |
# File 'lib/or2d/composite.rb', line 36 def remove_layer(id) @layers.delete(id) OR2D.game.entities[id]&.destroy OR2D.game.remove_entity(id) end |