Class: Zif::Sprite
- Inherits:
-
Object
- Object
- Zif::Sprite
- Includes:
- Actions::Actionable, Actions::Animatable, Assignable, Clickable, Serializable
- Defined in:
- lib/zif/sprite.rb
Overview
A basic sprite which combines actions / animations, click handling, mass assignment and more.
The foundation for most of the Zif library.
Includes attr_sprite
– supports what DRGTK provides for these classes in addition to what is documented here.
See DRGTK docs on attr_sprite
: docs.dragonruby.org/#—-attr_sprite.rb
# This only needs to be done once globally, usually in your Zif::Scene#prepare_scene method
$game.services[:input_service].register_clickable(dragon)
# Turn the dragon red when the mouse is clicked down.
# This lambda is called by the input service with this sprite (dragon) plus the mouse location.
# We don't need the mouse location for this example so we prefix that argument with _ to indicate it is unused
dragon.on_mouse_down = lambda {|sprite, _point|
sprite.r = 255
sprite.g = 0
sprite.b = 0
}
# Turn the dragon green when the mouse is clicked down & moved
dragon.on_mouse_changed = lambda {|sprite, _point|
sprite.r = 0
sprite.g = 255
sprite.b = 0
}
# Turn the dragon blue when the mouse click ends - the dragon stays blue after this until you click again.
dragon.on_mouse_up = lambda {|sprite, _point|
sprite.r = 0
sprite.g = 0
sprite.b = 255
}
Direct Known Subclasses
ExampleApp::Avatar, ExampleApp::FocusCheck, ExampleApp::Pixie, CompoundSprite
Constant Summary collapse
- BLENDMODE =
{ none: 0, alpha: 1, add: 2, mod: 3, multiply: 4 }.freeze
Instance Attribute Summary collapse
-
#a ⇒ Numeric
Alpha channel (Transparency) (
0-255
). -
#angle ⇒ Numeric
Rotation angle in degrees.
-
#b ⇒ Numeric
Blue color (
0-255
). -
#g ⇒ Numeric
Green color (
0-255
). -
#h ⇒ Numeric
Height.
-
#logical_x ⇒ Integer
Used for unit positioning (tile map addressing, for example - See Layers::LayerGroup).
-
#logical_y ⇒ Integer
Used for unit positioning (tile map addressing, for example - See Layers::LayerGroup).
-
#name ⇒ Symbol, String
The name of this instance.
-
#path ⇒ Symbol, String
The source of this image.
-
#r ⇒ Numeric
Red color (
0-255
). -
#render_target ⇒ Zif::RenderTarget
If this sprite is a parent container for a RenderTarget (sources it via #path), reference it here.
-
#source_h ⇒ Numeric
The Y axis extent to which we will source the #path image.
-
#source_w ⇒ Numeric
The X axis extent to which we will source the #path image.
-
#source_x ⇒ Numeric
X axis position of the #path image we want to start sourcing the image from.
-
#source_y ⇒ Numeric
Y axis position of the #path image we want to start sourcing the image from.
-
#w ⇒ Numeric
Width.
-
#x ⇒ Numeric
X axis position.
-
#y ⇒ Numeric
Y axis position.
-
#z_index ⇒ Integer
Stacking order, used to determine which sprite is above another if overlapping.
Attributes included from Clickable
#on_mouse_changed, #on_mouse_down, #on_mouse_up
Attributes included from Actions::Animatable
#animation_sequences, #cur_animation
Attributes included from Actions::Actionable
Class Method Summary collapse
-
.rect_array_to_hash(arr = []) ⇒ Hash<Symbol, Numeric>
Converts the array into a hash with those values mapped like {x: … }.
-
.rect_array_to_source_hash(arr = []) ⇒ Hash<Symbol, Numeric>
Converts the array into a hash with those values mapped like {source_x: … }.
-
.rect_hash_to_source_hash(rect = {}) ⇒ Hash<Symbol, Numeric>
Converts keys in
rect
like {source_x: …, source_y: …}.
Instance Method Summary collapse
-
#blend ⇒ Integer
(also: #blendmode_enum)
The integer value for the specified blend mode.
-
#blend=(new_blendmode) ⇒ Integer
Set blend mode using either symbol names or the enum integer values.
- #center ⇒ Array<Numeric>
- #center_x ⇒ Integer
- #center_y ⇒ Integer
-
#clicked?(point, kind = :up) ⇒ Object?
If this sprite has a #render_target, pass the click through to it.
- #color ⇒ Hash<Symbol, Numeric>
- #color=(rgba_array = []) ⇒ Object
-
#dup_and_assign(sprite) ⇒ Zif::Sprite
A copy of this sprite, with the changes applied.
- #exclude_from_serialize ⇒ Object private
-
#hide ⇒ Object
Sets #a alpha to 0 (fully transparent).
-
#initialize(name = Zif.unique_name('sprite')) ⇒ Sprite
constructor
A new instance of Sprite.
- #rect ⇒ Array<Numeric>
- #rect_hash ⇒ Hash<Symbol, Numeric>
-
#show ⇒ Object
Sets #a alpha to 255 (fully opaque).
-
#source_as_rect_hash ⇒ Hash<Symbol, Numeric>
If for some reason you want
source_
attrs without “source_” keys. -
#source_center ⇒ Array<Numeric>
[source center x, source center y].
- #source_is_set? ⇒ Boolean
- #source_rect ⇒ Array<Numeric>
- #source_rect_hash ⇒ Hash<Symbol, Numeric>
- #source_wh ⇒ Array<Numeric>
- #source_xy ⇒ Array<Numeric>
-
#to_h ⇒ Hash<Symbol, Numeric>
Hash of #color + #rect_hash + #source_rect_hash + #path.
- #view_actual_size! ⇒ Object
- #wh ⇒ Array<Numeric>
- #xy ⇒ Array<Numeric>
- #zoom_factor ⇒ Array<Numeric>
Methods included from Clickable
Methods included from Actions::Animatable
#new_basic_animation, #new_tiled_animation, #register_animation_sequence, #run_animation_sequence, #stop_animating
Methods included from Actions::Actionable
#bounce_forever_around, #delayed_action, #fade_in, #fade_out, #fade_out_and_in_forever, #new_action, #perform_actions, #run_action, #running_actions?, #stop_action
Methods included from Serializable
Methods included from Assignable
Constructor Details
#initialize(name = Zif.unique_name('sprite')) ⇒ Sprite
Returns a new instance of Sprite.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/zif/sprite.rb', line 145 def initialize(name=Zif.unique_name('sprite')) @name = name @logical_x = 0 @logical_y = 0 @x = 0 @y = 0 @z_index = 0 @w = 0 @h = 0 @a = 255 @r = 255 @g = 255 @b = 255 @angle = 0 self.blend = :alpha end |
Instance Attribute Details
#a ⇒ Numeric
Returns Alpha channel (Transparency) (0-255
).
|
# File 'lib/zif/sprite.rb', line 111
|
#angle ⇒ Numeric
Returns Rotation angle in degrees.
|
# File 'lib/zif/sprite.rb', line 111
|
#b ⇒ Numeric
Returns Blue color (0-255
).
|
# File 'lib/zif/sprite.rb', line 111
|
#g ⇒ Numeric
Returns Green color (0-255
).
|
# File 'lib/zif/sprite.rb', line 111
|
#h ⇒ Numeric
Returns Height.
|
# File 'lib/zif/sprite.rb', line 111
|
#logical_x ⇒ Integer
Returns Used for unit positioning (tile map addressing, for example - See Layers::LayerGroup).
93 94 95 |
# File 'lib/zif/sprite.rb', line 93 def logical_x @logical_x end |
#logical_y ⇒ Integer
Returns Used for unit positioning (tile map addressing, for example - See Layers::LayerGroup).
96 97 98 |
# File 'lib/zif/sprite.rb', line 96 def logical_y @logical_y end |
#name ⇒ Symbol, String
Returns The name of this instance. This helps differentiate multiple copies when debugging.
90 91 92 |
# File 'lib/zif/sprite.rb', line 90 def name @name end |
#path ⇒ Symbol, String
Returns The source of this image. Either the path to the image file relative to app/
, or the name of the render target.
|
# File 'lib/zif/sprite.rb', line 111
|
#r ⇒ Numeric
Returns Red color (0-255
).
|
# File 'lib/zif/sprite.rb', line 111
|
#render_target ⇒ Zif::RenderTarget
If this sprite is a parent container for a RenderTarget (sources it via #path), reference it here
Not for sprites which are children (rendered inside) of a Render Target!
109 110 111 |
# File 'lib/zif/sprite.rb', line 109 def render_target @render_target end |
#source_h ⇒ Numeric
Returns The Y axis extent to which we will source the #path image.
|
# File 'lib/zif/sprite.rb', line 111
|
#source_w ⇒ Numeric
Returns The X axis extent to which we will source the #path image.
|
# File 'lib/zif/sprite.rb', line 111
|
#source_x ⇒ Numeric
Returns X axis position of the #path image we want to start sourcing the image from.
|
# File 'lib/zif/sprite.rb', line 111
|
#source_y ⇒ Numeric
Returns Y axis position of the #path image we want to start sourcing the image from.
|
# File 'lib/zif/sprite.rb', line 111
|
#w ⇒ Numeric
Returns Width.
|
# File 'lib/zif/sprite.rb', line 111
|
#x ⇒ Numeric
Returns X axis position.
|
# File 'lib/zif/sprite.rb', line 111
|
#y ⇒ Numeric
Returns Y axis position.
|
# File 'lib/zif/sprite.rb', line 111
|
#z_index ⇒ Integer
Layers::LayerGroup has it’s own stacking order, each layer has a z-index. Sprites contained within a layer are ordered amongst themselves using this attribute, but are constrained to the layer they are on.
Returns Stacking order, used to determine which sprite is above another if overlapping.
103 104 105 |
# File 'lib/zif/sprite.rb', line 103 def z_index @z_index end |
Class Method Details
.rect_array_to_hash(arr = []) ⇒ Hash<Symbol, Numeric>
Returns Converts the array into a hash with those values mapped like {x: … }.
212 213 214 215 216 217 218 219 |
# File 'lib/zif/sprite.rb', line 212 def self.rect_array_to_hash(arr=[]) { x: arr[0], y: arr[1], w: arr[2], h: arr[3] } end |
.rect_array_to_source_hash(arr = []) ⇒ Hash<Symbol, Numeric>
Returns Converts the array into a hash with those values mapped like {source_x: … }.
223 224 225 226 227 228 229 230 |
# File 'lib/zif/sprite.rb', line 223 def self.rect_array_to_source_hash(arr=[]) { source_x: arr[0], source_y: arr[1], source_w: arr[2], source_h: arr[3] } end |
.rect_hash_to_source_hash(rect = {}) ⇒ Hash<Symbol, Numeric>
Returns Converts keys in rect
like {source_x: …, source_y: …}.
234 235 236 |
# File 'lib/zif/sprite.rb', line 234 def self.rect_hash_to_source_hash(rect={}) rect.transform_keys { |key| key.include?('source_') ? key : "source_#{key}".to_sym } end |
Instance Method Details
#blend ⇒ Integer Also known as: blendmode_enum
Returns The integer value for the specified blend mode. See BLENDMODE.
180 181 182 |
# File 'lib/zif/sprite.rb', line 180 def blend @blendmode end |
#blend=(new_blendmode) ⇒ Integer
Set blend mode using either symbol names or the enum integer values.
172 173 174 |
# File 'lib/zif/sprite.rb', line 172 def blend=(new_blendmode) @blendmode = BLENDMODE.fetch(new_blendmode, new_blendmode) end |
#center ⇒ Array<Numeric>
259 260 261 |
# File 'lib/zif/sprite.rb', line 259 def center [center_x, center_y] end |
#center_x ⇒ Integer
249 250 251 |
# File 'lib/zif/sprite.rb', line 249 def center_x (@x + @w.idiv(2)).to_i end |
#center_y ⇒ Integer
254 255 256 |
# File 'lib/zif/sprite.rb', line 254 def center_y (@y + @h.idiv(2)).to_i end |
#clicked?(point, kind = :up) ⇒ Object?
Returns If this sprite has a #render_target, pass the click through to it. Otherwise, call Clickable#clicked? and return this sprite or nil
.
204 205 206 207 208 |
# File 'lib/zif/sprite.rb', line 204 def clicked?(point, kind=:up) return super(point, kind) unless @render_target && !absorb_click? @render_target.clicked?(point, kind) end |
#color ⇒ Hash<Symbol, Numeric>
326 327 328 329 330 331 332 333 |
# File 'lib/zif/sprite.rb', line 326 def color { r: @r, g: @g, b: @b, a: @a } end |
#color=(rgba_array = []) ⇒ Object
Use #assign
if you want to assign with a hash. This works with positional array.
337 338 339 340 341 342 |
# File 'lib/zif/sprite.rb', line 337 def color=(rgba_array=[]) @r = rgba_array[0] if rgba_array[0] @g = rgba_array[1] if rgba_array[1] @b = rgba_array[2] if rgba_array[2] @a = rgba_array[3] if rgba_array[3] end |
#dup_and_assign(sprite) ⇒ Zif::Sprite
Returns A copy of this sprite, with the changes applied.
165 166 167 |
# File 'lib/zif/sprite.rb', line 165 def dup_and_assign(sprite) dup.assign(sprite) end |
#exclude_from_serialize ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
350 351 352 |
# File 'lib/zif/sprite.rb', line 350 def exclude_from_serialize %w[render_target] end |
#hide ⇒ Object
Some processing may be skipped if this sprite is hidden, to increase performance.
Sets #a alpha to 0 (fully transparent).
194 195 196 |
# File 'lib/zif/sprite.rb', line 194 def hide @a = 0 end |
#rect ⇒ Array<Numeric>
265 266 267 |
# File 'lib/zif/sprite.rb', line 265 def rect [@x, @y, @w, @h] end |
#rect_hash ⇒ Hash<Symbol, Numeric>
270 271 272 |
# File 'lib/zif/sprite.rb', line 270 def rect_hash Sprite.rect_array_to_hash(rect) end |
#show ⇒ Object
Sets #a alpha to 255 (fully opaque)
187 188 189 |
# File 'lib/zif/sprite.rb', line 187 def show @a = 255 end |
#source_as_rect_hash ⇒ Hash<Symbol, Numeric>
If for some reason you want source_
attrs without “source_” keys
321 322 323 |
# File 'lib/zif/sprite.rb', line 321 def source_as_rect_hash Sprite.rect_array_to_hash(source_rect) end |
#source_center ⇒ Array<Numeric>
Returns [source center x, source center y].
305 306 307 |
# File 'lib/zif/sprite.rb', line 305 def source_center [(@source_x + @source_w.idiv(2)).to_i, (@source_y + @source_h.idiv(2)).to_i] end |
#source_is_set? ⇒ Boolean
285 286 287 |
# File 'lib/zif/sprite.rb', line 285 def source_is_set? !(@source_x.nil? || @source_y.nil? || @source_w.nil? || @source_h.nil?) end |
#source_rect ⇒ Array<Numeric>
300 301 302 |
# File 'lib/zif/sprite.rb', line 300 def source_rect [@source_x, @source_y, @source_w, @source_h] end |
#source_rect_hash ⇒ Hash<Symbol, Numeric>
315 316 317 |
# File 'lib/zif/sprite.rb', line 315 def source_rect_hash Sprite.rect_array_to_source_hash(source_rect) end |
#source_wh ⇒ Array<Numeric>
295 296 297 |
# File 'lib/zif/sprite.rb', line 295 def source_wh [@source_w, @source_h] end |
#source_xy ⇒ Array<Numeric>
290 291 292 |
# File 'lib/zif/sprite.rb', line 290 def source_xy [@source_x, @source_y] end |
#to_h ⇒ Hash<Symbol, Numeric>
Returns Hash of #color + #rect_hash + #source_rect_hash + #path.
345 346 347 |
# File 'lib/zif/sprite.rb', line 345 def to_h {path: @path}.merge(source_rect_hash).merge(rect_hash).merge(color) end |
#view_actual_size! ⇒ Object
277 278 279 280 281 282 |
# File 'lib/zif/sprite.rb', line 277 def view_actual_size! @source_x = 0 @source_y = 0 @source_w = @w @source_h = @h end |
#wh ⇒ Array<Numeric>
244 245 246 |
# File 'lib/zif/sprite.rb', line 244 def wh [@w, @h] end |
#xy ⇒ Array<Numeric>
239 240 241 |
# File 'lib/zif/sprite.rb', line 239 def xy [@x, @y] end |