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

Instance Method Summary collapse

Instance Attribute Details

#layer_nameSymbol

Returns The name of the layer, used for indexing.

Returns:

  • (Symbol)

    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

#mapZif::Layers::LayerGroup

Returns The LayerGroup this layer belongs to.

Returns:



6
7
8
# File 'lib/zif/layers/layerable.rb', line 6

def map
  @map
end

#should_renderBoolean

Returns (For SimpleLayer only) Should this layer rerender this tick?.

Returns:

  • (Boolean)

    (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_indexInteger

Returns Where this layer is in the top to bottom sort order (0 is bottom).

Returns:

  • (Integer)

    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.

Parameters:

  • point (Array<Integer>)

    [x, y] position Array of the current mouse click with respect to the screen.

  • kind (Symbol) (defaults to: :up)

    The kind of click coming through, one of [:up, :down, :changed]

Returns:

  • (Object, nil)

    The result of sprite.clicked? for any sprite intersecting the translated point

See Also:



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_serializeObject



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.

Parameters:

  • left (Integer)

    Look for sprites to the right of this value

  • bottom (Integer)

    Look for sprites above this value

  • right (Integer)

    Look for sprites to the left of this value

  • top (Integer)

    Look for sprites below this value

Returns:

  • (Array<Zif::Sprite>)

    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.

Parameters:

  • sprite (Zif::Sprite)

    The sprite to add

  • logical_x (Integer)

    Where to place the sprite in terms of logical X position

  • logical_y (Integer)

    Where to place the sprite in terms of logical Y position



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.

Parameters:



16
17
18
# File 'lib/zif/layers/layerable.rb', line 16

def remove_positioned_sprite(sprite)
  remove_sprite(sprite)
end

#target_layer_nameString

Returns The combination of name of the map plus the name of the layer.

Returns:

  • (String)

    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.

Parameters:

  • given_rect (Array<Integer>) (defaults to: nil)

    The [x, y, w, h] rectangle we want to check for sprites Natural rectangle, not logical If not provided, it will use the source settings for the containing sprite

Returns:

  • (Array<Zif::Sprite>)

    The sprites on this layer which are within given_rect



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