Class: OR2D::Composites::Sprite

Inherits:
OR2D::Composite show all
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.

Since:

  • 2023-04-26

Instance Attribute Summary

Attributes inherited from OR2D::Composite

#id, #layers

Instance Method Summary collapse

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.

Parameters:

  • options (Hash)

    a hash of options

Since:

  • 2023-04-26



8
9
10
11
12
13
14
15
16
17
# File 'lib/or2d/composites/sprite.rb', line 8

def initialize(options)
  super(options[:id] || "SpriteComposite_#{SecureRandom.uuid}")
  if options[:layers]
    options[:layers].each do |layer|
      add_layer(layer[:type], layer, entity: layer[:entity])
    end
  else
    add_layer(:sprite, options)
  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.

Parameters:

  • type (Symbol)

    the type of Entity object to add

  • options (Hash)

    a hash of options to pass to the Entity object

  • entity (OR2D::Entity) (defaults to: nil)

    an optional Entity object to add if the type is :entity

  • position (Symbol) (defaults to: :top)

    the position to add the layer to, either :top or :bottom

Since:

  • 2023-04-26



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, options, entity: nil, position: :top)
  if @layers.empty?
    super(type, options, entity: entity)
    id = @layers.keys.first
  elsif position == :top
    super(type, options.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, options.merge(z: OR2D.game.entities[@layers.keys.last].resource.z - 1), entity: entity)
    sort_by_z
    id = @layers.keys.last
  else
    add_layer(type, options, entity: entity, position: :top)
  end

  @layers[id][:offsets] = [options[:x_offset] || 0,
                           options[:y_offset] || 0,
                           options[:width_offset] || 0,
                           options[: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.

Parameters:

  • id (String)

    the id of the layer to bring to the front

Since:

  • 2023-04-26



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.

Parameters:

  • layer_idx (Integer) (defaults to: -1))

    the index of the layer to get the height of

Since:

  • 2023-04-26



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.

Parameters:

  • layer_idx (Integer) (defaults to: -1))

    the index of the layer to get the width of

Since:

  • 2023-04-26



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.

Parameters:

  • layer_idx (Integer) (defaults to: -1))

    the index of the layer to get the x-coordinate of

Since:

  • 2023-04-26



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.

Parameters:

  • layer_idx (Integer) (defaults to: -1))

    the index of the layer to get the y-coordinate of

Since:

  • 2023-04-26



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.

Parameters:

  • height (Integer)

    the height to set

Since:

  • 2023-04-26



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

Note:

Animations should be passed using the following convention:

Plays all animations for layers that are visible.

Parameters:

  • options (Hash) (defaults to: {})

    the options to play the animations with

Since:

  • 2023-04-26



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(options = {})
  if options.key?(:layers)
    options[:layers].each do |layer_id, options|
      next unless @layers[layer_id][:show]
      next unless OR2D.game.entities[layer_id].resource.respond_to?(:play)
      next if options[:except].include?(layer_id)

      OR2D.game.entities[layer_id].resource.play(animation: options[:animation],
                                                 loop: options[:loop],
                                                 flip: options[:flip],
                                                 &options[:done])

    end
  elsif options.key?(:layer)
    return unless @layers[options[:layer]][:show]
    return unless OR2D.game.entities[options[:layer]].resource.respond_to?(:play)

    OR2D.game.entities[options[:layer]].resource.play(animation: options[:animation],
                                                      loop: options[:loop],
                                                      flip: options[:flip],
                                                      &options[:done])
  else
    @layers.each do |id, properties|
      next unless properties[:show]
      next unless OR2D.game.entities[id].resource.respond_to?(:play)
      next if options[:except].include?(id)

      OR2D.game.entities[id].resource.play(animation: options[:animation],
                                           loop: options[:loop],
                                           flip: options[:flip],
                                           &options[: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.

Parameters:

  • id (String)

    the id of the layer to remove

Since:

  • 2023-04-26



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.

Parameters:

  • id (String)

    the id of the layer to send to the back

Since:

  • 2023-04-26



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.

Parameters:

  • height (Integer)

    the height to set

  • layer_id (String)

    the id of the layer to set the height of

  • offsets (Boolean) (defaults to: true)

    whether or not to apply offsets to the height

Since:

  • 2023-04-26



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.

Parameters:

  • width (Integer)

    the width to set

  • layer_id (String)

    the id of the layer to set the width of

  • offsets (Boolean) (defaults to: true)

    whether or not to apply offsets to the width

Since:

  • 2023-04-26



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.

Parameters:

  • x_coordinate (Integer)

    the x-coordinate to set

  • layer_id (Integer)

    the layer to set the x-coordinate of

  • offsets (Boolean) (defaults to: true)

    whether or not to apply offsets

Since:

  • 2023-04-26



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.

Parameters:

  • y_coordinate (Integer)

    the y-coordinate to set

  • layer_id (String)

    the id of the layer to set the y-coordinate of

  • offsets (Boolean) (defaults to: true)

    whether or not to apply offsets to the y-coordinate

Since:

  • 2023-04-26



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_zObject

Sort the layers of the Sprite by their by z-coordinate.

Since:

  • 2023-04-26



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.

Parameters:

  • from_idx (String)

    the index of the id of the layer to swap

  • to_idx (String)

    the index of the id of the layer to swap with

Since:

  • 2023-04-26



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.

Parameters:

  • width (Integer)

    the width to set

Since:

  • 2023-04-26



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

#xInteger

Get the x-coordinate of the Sprite. This is determined by the last layer’s x-coordinate.

Returns:

  • (Integer)

    the x-coordinate of the Sprite

Since:

  • 2023-04-26



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.

Parameters:

  • x_coordinate (Integer)

    the x-coordinate to set

Since:

  • 2023-04-26



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

#yInteger

Get the y-coordinate of the Sprite. This is determined by the last layer’s y-coordinate.

Returns:

  • (Integer)

    the y-coordinate of the Sprite

Since:

  • 2023-04-26



163
164
165
# File 'lib/or2d/composites/sprite.rb', line 163

def y
  OR2D.game.entities[@layers.keys.last].y
end

#y=(y_coordinate) ⇒ Object

Set the y-coordinate of the Sprite with offsets.

Parameters:

  • y_coordinate (Integer)

    the y-coordinate to set

Since:

  • 2023-04-26



169
170
171
172
173
# File 'lib/or2d/composites/sprite.rb', line 169

def y=(y_coordinate)
  @layers.each_key do |id|
    OR2D.game.entities[id].y = y_coordinate + @layers[id][:offsets][1]
  end
end