Class: OR2D::Composites::Sprite
- Inherits:
-
OR2D::Composite
- Object
- OR2D::Composite
- OR2D::Composites::Sprite
- Includes:
- Animations::CompositeAnimations
- Defined in:
- lib/or2d/composites/sprite.rb
Overview
The Composite Sprite class is a composite entity that is made up of multiple sprites entities that behave as the composite layers.
Instance Attribute Summary
Attributes inherited from OR2D::Composite
Instance Method Summary collapse
-
#add_layer(type, options, entity: nil, position: :top) ⇒ Object
Add a layer to the Sprite.
-
#bring_to_front(id) ⇒ Object
Bring a layer to the front of the Sprite.
-
#get_layer_height(layer_idx = -1)) ⇒ Object
Get the height of a specific layer.
-
#get_layer_width(layer_idx = -1)) ⇒ Object
Get the width of a specific layer.
-
#get_layer_x(layer_idx = -1)) ⇒ Object
Get the x-coordinate of a specific layer.
-
#get_layer_y(layer_idx = -1)) ⇒ Object
Get the y-coordinate of a specific layer.
-
#height=(height) ⇒ Object
Set the height of all layers in the Sprite.
-
#initialize(options) ⇒ Sprite
constructor
Constructs a new Composite Sprite object.
-
#play(options = {}) ⇒ Object
Plays all animations for layers that are visible.
-
#remove_layer(id) ⇒ Object
Remove a layer from the Sprite.
-
#send_to_back(id) ⇒ Object
Send a layer to the back of the Sprite.
-
#set_layer_height(layer_id, height, offsets: true) ⇒ Object
Set the height of the Sprite or a specific layer with or without offsets.
-
#set_layer_width(layer_id, width, offsets: true) ⇒ Object
Set the width of the Sprite or a specific layer with or without offsets.
-
#set_layer_x(layer_id, x_coordinate, offsets: true) ⇒ Object
Set the x-coordinate of a specific layer with or without offsets.
-
#set_layer_y(layer_id, y_coordinate, offsets: true) ⇒ Object
Set the y-coordinate of the Sprite or a specific layer with or without offsets.
-
#sort_by_z ⇒ Object
Sort the layers of the Sprite by their by z-coordinate.
-
#swap(from_idx, to_idx) ⇒ Object
Swap the z-index of two layers in the Sprite.
-
#width=(width) ⇒ Object
Set the width of the Sprite.
-
#x ⇒ Integer
Get the x-coordinate of the Sprite.
-
#x=(x_coordinate) ⇒ Object
Set the x-coordinate of the Sprite with offsets.
-
#y ⇒ Integer
Get the y-coordinate of the Sprite.
-
#y=(y_coordinate) ⇒ Object
Set the y-coordinate of the Sprite with offsets.
Methods included from Animations::CompositeAnimations
#fade, #fade_in, #fade_out, #grow, #grow_entity, #grow_text_composite, #rotation, #shake, #shrink, #shrink_entity, #shrink_text
Methods inherited from OR2D::Composite
#destroy, #hide, #show, #toggle
Constructor Details
#initialize(options) ⇒ Sprite
Constructs a new Composite Sprite object.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/or2d/composites/sprite.rb', line 8 def initialize() super([:id] || "SpriteComposite_#{SecureRandom.uuid}") if [:layers] [:layers].each do |layer| add_layer(layer[:type], layer, entity: layer[:entity]) end else add_layer(:sprite, ) end end |
Instance Method Details
#add_layer(type, options, entity: nil, position: :top) ⇒ Object
Add a layer to the Sprite. This will also reorder the z-index of all other layers in the Sprite.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/or2d/composites/sprite.rb', line 24 def add_layer(type, , entity: nil, position: :top) if @layers.empty? super(type, , entity: entity) id = @layers.keys.first elsif position == :top super(type, .merge(z: OR2D.game.entities[@layers.keys.first].resource.z + 1), entity: entity) sort_by_z id = @layers.keys.first elsif position == :bottom super(type, .merge(z: OR2D.game.entities[@layers.keys.last].resource.z - 1), entity: entity) sort_by_z id = @layers.keys.last else add_layer(type, , entity: entity, position: :top) end @layers[id][:offsets] = [[:x_offset] || 0, [:y_offset] || 0, [:width_offset] || 0, [:height_offset] || 0] apply_offsets(id) id end |
#bring_to_front(id) ⇒ Object
Bring a layer to the front of the Sprite. This will also reorder the z-index of all other layers in the Sprite.
57 58 59 60 61 62 63 64 65 |
# File 'lib/or2d/composites/sprite.rb', line 57 def bring_to_front(id) OR2D.game.entities[id].resource.z = OR2D.game.entities[@layers.keys.first].resource.z @layers.each_key do |key| next if key == id OR2D.game.entities[key].resource.z -= 1 end sort_by_z end |
#get_layer_height(layer_idx = -1)) ⇒ Object
Get the height of a specific layer.
99 100 101 |
# File 'lib/or2d/composites/sprite.rb', line 99 def get_layer_height(layer_idx = -1) OR2D.game.entities[@layers.keys[layer_idx]].height end |
#get_layer_width(layer_idx = -1)) ⇒ Object
Get the width of a specific layer.
121 122 123 |
# File 'lib/or2d/composites/sprite.rb', line 121 def get_layer_width(layer_idx = -1) OR2D.game.entities[@layers.keys[layer_idx]].width end |
#get_layer_x(layer_idx = -1)) ⇒ Object
Get the x-coordinate of a specific layer.
149 150 151 |
# File 'lib/or2d/composites/sprite.rb', line 149 def get_layer_x(layer_idx = -1) OR2D.game.entities[@layers.keys[layer_idx]].x end |
#get_layer_y(layer_idx = -1)) ⇒ Object
Get the y-coordinate of a specific layer.
177 178 179 |
# File 'lib/or2d/composites/sprite.rb', line 177 def get_layer_y(layer_idx = -1) OR2D.game.entities[@layers.keys[layer_idx]].y end |
#height=(height) ⇒ Object
Set the height of all layers in the Sprite.
91 92 93 94 95 |
# File 'lib/or2d/composites/sprite.rb', line 91 def height=(height) @layers.each_key do |id| OR2D.game.entities[id].height = height + @layers[id][:offsets][3] end end |
#play(options = {}) ⇒ Object
Animations should be passed using the following convention:
Plays all animations for layers that are visible.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/or2d/composites/sprite.rb', line 192 def play( = {}) if .key?(:layers) [:layers].each do |layer_id, | next unless @layers[layer_id][:show] next unless OR2D.game.entities[layer_id].resource.respond_to?(:play) next if [:except].include?(layer_id) OR2D.game.entities[layer_id].resource.play(animation: [:animation], loop: [:loop], flip: [:flip], &[:done]) end elsif .key?(:layer) return unless @layers[[:layer]][:show] return unless OR2D.game.entities[[:layer]].resource.respond_to?(:play) OR2D.game.entities[[:layer]].resource.play(animation: [:animation], loop: [:loop], flip: [:flip], &[:done]) else @layers.each do |id, properties| next unless properties[:show] next unless OR2D.game.entities[id].resource.respond_to?(:play) next if [:except].include?(id) OR2D.game.entities[id].resource.play(animation: [:animation], loop: [:loop], flip: [:flip], &[:done]) end end end |
#remove_layer(id) ⇒ Object
Remove a layer from the Sprite. This will also reorder the z-index of all other layers in the Sprite.
50 51 52 53 |
# File 'lib/or2d/composites/sprite.rb', line 50 def remove_layer(id) super(id) sort_by_z unless @layers.empty? || @layers.size == 1 end |
#send_to_back(id) ⇒ Object
Send a layer to the back of the Sprite. This will also reorder the z-index of all other layers in the Sprite.
69 70 71 72 73 74 75 76 77 |
# File 'lib/or2d/composites/sprite.rb', line 69 def send_to_back(id) OR2D.game.entities[id].resource.z = OR2D.game.entities[@layers.keys.last].resource.z @layers.each_key do |key| next if key == id OR2D.game.entities[key].resource.z += 1 end sort_by_z end |
#set_layer_height(layer_id, height, offsets: true) ⇒ Object
Set the height of the Sprite or a specific layer with or without offsets.
107 108 109 |
# File 'lib/or2d/composites/sprite.rb', line 107 def set_layer_height(layer_id, height, offsets: true) OR2D.game.entities[layer_id].height = offsets ? height + @layers[layer_id][:offsets][3] : height end |
#set_layer_width(layer_id, width, offsets: true) ⇒ Object
Set the width of the Sprite or a specific layer with or without offsets.
129 130 131 |
# File 'lib/or2d/composites/sprite.rb', line 129 def set_layer_width(layer_id, width, offsets: true) OR2D.game.entities[layer_id].width = offsets ? width + @layers[layer_id][:offsets][2] : width end |
#set_layer_x(layer_id, x_coordinate, offsets: true) ⇒ Object
Set the x-coordinate of a specific layer with or without offsets.
157 158 159 |
# File 'lib/or2d/composites/sprite.rb', line 157 def set_layer_x(layer_id, x_coordinate, offsets: true) OR2D.game.entities[layer_id].x = offsets ? x_coordinate + @layers[layer_id][:offsets][0] : x_coordinate end |
#set_layer_y(layer_id, y_coordinate, offsets: true) ⇒ Object
Set the y-coordinate of the Sprite or a specific layer with or without offsets.
185 186 187 |
# File 'lib/or2d/composites/sprite.rb', line 185 def set_layer_y(layer_id, y_coordinate, offsets: true) OR2D.game.entities[layer_id].y = offsets ? y_coordinate + @layers[layer_id][:offsets][1] : y_coordinate end |
#sort_by_z ⇒ Object
Sort the layers of the Sprite by their by z-coordinate.
228 229 230 231 232 |
# File 'lib/or2d/composites/sprite.rb', line 228 def sort_by_z @layers.keys .sort_by! { |key| -OR2D.game.entities[key].resource.z } .each { |k| @layers[k] = @layers.delete(k) } end |
#swap(from_idx, to_idx) ⇒ Object
Swap the z-index of two layers in the Sprite. This will also reorder the z-index of all other layers in the Sprite.
82 83 84 85 86 87 |
# File 'lib/or2d/composites/sprite.rb', line 82 def swap(from_idx, to_idx) from = @layers.keys[from_idx] to = @layers.keys[to_idx] OR2D.game.entities[from].resource.z, OR2D.game.entities[to].resource.z = OR2D.game.entities[to].resource.z, OR2D.game.entities[from].resource.z sort_by_z end |
#width=(width) ⇒ Object
Set the width of the Sprite.
113 114 115 116 117 |
# File 'lib/or2d/composites/sprite.rb', line 113 def width=(width) @layers.each_key do |id| OR2D.game.entities[id].width = width + @layers[id][:offsets][2] end end |
#x ⇒ Integer
Get the x-coordinate of the Sprite. This is determined by the last layer’s x-coordinate.
135 136 137 |
# File 'lib/or2d/composites/sprite.rb', line 135 def x OR2D.game.entities[@layers.keys.last].x end |
#x=(x_coordinate) ⇒ Object
Set the x-coordinate of the Sprite with offsets.
141 142 143 144 145 |
# File 'lib/or2d/composites/sprite.rb', line 141 def x=(x_coordinate) @layers.each_key do |id| OR2D.game.entities[id].x = x_coordinate + @layers[id][:offsets][0] end end |