Module: Zif::Layers::Layerable
- Included in:
- ActiveLayer, SimpleLayer
- Defined in:
- lib/zif/layers/layerable.rb
Overview
Functionality shared between SimpleLayer & ActiveLayer
Instance Attribute Summary collapse
-
#layer_name ⇒ Symbol
The name of the layer, used for indexing.
-
#map ⇒ Zif::Layers::LayerGroup
The LayerGroup this layer belongs to.
-
#should_render ⇒ Boolean
(For SimpleLayer only) Should this layer rerender this tick?.
-
#z_index ⇒ Integer
Where this layer is in the top to bottom sort order (
0
is bottom).
Instance Method Summary collapse
-
#clicked?(point, kind = :up) ⇒ Object?
Converts the given screen
point
into coordinates within the layer, then passes this point to each sprite on this layer. - #exclude_from_serialize ⇒ Object
-
#intersecting_sprites(left:, bottom:, right:, top:) ⇒ Array<Zif::Sprite>
The sprites on this layer which are within the bounds of the
compare
params. -
#position_sprite(sprite:, logical_x:, logical_y:) ⇒ Object
Removes a sprite from its logical x / y position on the layer.
-
#remove_positioned_sprite(sprite) ⇒ Object
Removes a sprite from its logical x / y position on the layer.
-
#target_layer_name ⇒ String
The combination of name of the map plus the name of the layer.
-
#visible_sprites(given_rect = nil) ⇒ Array<Zif::Sprite>
This is not very performant with lots of sprites! Consider using TiledLayer instead.
Instance Attribute Details
#layer_name ⇒ Symbol
Returns The name of the layer, used for indexing.
8 9 10 |
# File 'lib/zif/layers/layerable.rb', line 8 def layer_name @layer_name end |
#map ⇒ Zif::Layers::LayerGroup
Returns The LayerGroup this layer belongs to.
6 7 8 |
# File 'lib/zif/layers/layerable.rb', line 6 def map @map end |
#should_render ⇒ Boolean
Returns (For SimpleLayer only) Should this layer rerender this tick?.
12 13 14 |
# File 'lib/zif/layers/layerable.rb', line 12 def should_render @should_render end |
#z_index ⇒ Integer
Returns Where this layer is in the top to bottom sort order (0
is bottom).
10 11 12 |
# File 'lib/zif/layers/layerable.rb', line 10 def z_index @z_index end |
Instance Method Details
#clicked?(point, kind = :up) ⇒ Object?
Converts the given screen point
into coordinates within the layer, then passes this point to each sprite on this layer.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/zif/layers/layerable.rb', line 88 def clicked?(point, kind=:up) x, y = Zif.add_positions( Zif.position_math( :mult, point, Zif.position_math( :fdiv, containing_sprite.source_wh, containing_sprite.wh ) ), containing_sprite.source_xy ) # puts "Layerable#clicked?(#{point}): #{@layer_name} #{x} #{y}" intersecting_sprites(left: x, bottom: y, right: x, top: y).reverse_each.find do |sprite| # puts " clicked? -> #{sprite}" sprite.respond_to?(:clicked?) && sprite.clicked?([x, y], kind) end end |
#exclude_from_serialize ⇒ Object
109 110 111 |
# File 'lib/zif/layers/layerable.rb', line 109 def exclude_from_serialize %w[sprites primitives] end |
#intersecting_sprites(left:, bottom:, right:, top:) ⇒ Array<Zif::Sprite>
Returns The sprites on this layer which are within the bounds of the compare
params.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/zif/layers/layerable.rb', line 65 def intersecting_sprites(left:, bottom:, right:, top:) # puts "Layerable#intersecting_sprites: #{@layer_name} #{source_sprites.length}" source_sprites.reject do |sprite| x = sprite.x y = sprite.y w = sprite.w h = sprite.h # puts "Layerable#intersecting_sprites: #{x} #{y} #{w} #{h}" ( (x > right) || (y > top) || (x + w < left) || (y + h < bottom) ) end end |
#position_sprite(sprite:, logical_x:, logical_y:) ⇒ Object
Removes a sprite from its logical x / y position on the layer.
24 25 26 27 28 29 30 31 |
# File 'lib/zif/layers/layerable.rb', line 24 def position_sprite(sprite:, logical_x:, logical_y:) # Skip Sprite#assign, this is perf critical sprite.x = logical_x * @map.tile_width sprite.y = logical_y * @map.tile_height sprite.logical_x = logical_x sprite.logical_y = logical_y sprite end |
#remove_positioned_sprite(sprite) ⇒ Object
Removes a sprite from its logical x / y position on the layer.
16 17 18 |
# File 'lib/zif/layers/layerable.rb', line 16 def remove_positioned_sprite(sprite) remove_sprite(sprite) end |
#target_layer_name ⇒ String
Returns The combination of name of the map plus the name of the layer.
34 35 36 |
# File 'lib/zif/layers/layerable.rb', line 34 def target_layer_name "#{@map.name}_#{@layer_name}" end |
#visible_sprites(given_rect = nil) ⇒ Array<Zif::Sprite>
This is not very performant with lots of sprites! Consider using TiledLayer instead.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/zif/layers/layerable.rb', line 43 def visible_sprites(given_rect=nil) if given_rect.nil? containing_sprite.view_actual_size! unless containing_sprite.source_is_set? left = containing_sprite.source_x bottom = containing_sprite.source_y right = left + containing_sprite.source_w top = bottom + containing_sprite.source_h else left = given_rect.x bottom = given_rect.y right = left + given_rect.w top = bottom + given_rect.h end intersecting_sprites(left: left, bottom: bottom, right: right, top: top) end |