Class: Zif::Layers::SimpleLayer

Inherits:
RenderTarget show all
Includes:
Layerable
Defined in:
lib/zif/layers/simple_layer.rb

Overview

Designed to be used with LayerGroup.

This layer is based on RenderTarget and therefore the component sprites will not be rendered until RenderTarget#redraw or RenderTarget#redraw_from_buffer is called - typically via #rerender.

In contrast, ActiveLayer is built on CompoundSprite and therefore must rerender every sprite on every tick. This is balanced by not incurring a performance / memory penalty by rendering a sprite the size of the entire LayerGroup width times height.

Deciding between SimpleLayer and ActiveLayer depends on your application. Try organizing your layers into those that don’t change at all, or only change when action (like camera movement) isn’t happening, and put those sprites into a SimpleLayer. Then take all of the sprites which do need to change often, or are necessary for action, and put those in ActiveLayers.

You can use this or SimpleLayer directly when the sprites contained don’t need to snap to the tile grid set up in the LayerGroup. Otherwise, you should use TiledLayer or ActiveTiledLayer

Direct Known Subclasses

BitmaskedTiledLayer, TiledLayer

Instance Attribute Summary collapse

Attributes included from Layerable

#layer_name, #map, #should_render, #z_index

Attributes inherited from RenderTarget

#bg_color, #height, #labels, #name, #primitives, #sprites, #width, #z_index

Instance Method Summary collapse

Methods included from Layerable

#clicked?, #exclude_from_serialize, #intersecting_sprites, #position_sprite, #remove_positioned_sprite, #target_layer_name, #visible_sprites

Methods inherited from RenderTarget

#clicked?, #containing_sprite, #cut_containing_sprites, #exclude_from_serialize, #full_containing_sprite, #project_from, #project_to, #redraw, #redraw_from_buffer, #relative_point, #resize, #set_inactive_buffer_name, #switch_buffer

Methods included from Serializable

#exclude_from_serialize, #inspect, #serialize, #to_s

Constructor Details

#initialize(map, name, z_index: 0, render_only_visible: false, clear_sprites_after_draw: false) ⇒ SimpleLayer

Returns a new instance of SimpleLayer.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/zif/layers/simple_layer.rb', line 33

def initialize(map, name, z_index: 0, render_only_visible: false, clear_sprites_after_draw: false)
  @map                      = map
  @layer_name               = name
  @z_index                  = z_index
  @render_only_visible      = render_only_visible
  @clear_sprites_after_draw = clear_sprites_after_draw
  @should_render            = true
  @rerender_rect            = nil
  reinitialize_sprites

  super(target_layer_name, bg_color: :black, width: @map.max_width, height: @map.max_height, z_index: @z_index)
end

Instance Attribute Details

#clear_sprites_after_drawBoolean

Returns Should #rerender clear #source_sprites after render?.

Returns:



28
29
30
# File 'lib/zif/layers/simple_layer.rb', line 28

def clear_sprites_after_draw
  @clear_sprites_after_draw
end

#render_only_visibleBoolean

Returns Should #rerender only consider Layerable#visible_sprites?.

Returns:



26
27
28
# File 'lib/zif/layers/simple_layer.rb', line 26

def render_only_visible
  @render_only_visible
end

#rerender_rectArray<Integer>

Returns A rectangle [x, y, w, h] defining the extent of #rerender. This is used to implement the double buffering technique described at RenderTarget.

Returns:

  • (Array<Integer>)

    A rectangle [x, y, w, h] defining the extent of #rerender. This is used to implement the double buffering technique described at RenderTarget.



31
32
33
# File 'lib/zif/layers/simple_layer.rb', line 31

def rerender_rect
  @rerender_rect
end

#source_spritesArray<Zif::Sprite>

Returns The array of sprites used as a source for the render target when being #rerendered.

Returns:

  • (Array<Zif::Sprite>)

    The array of sprites used as a source for the render target when being #rerendered



24
25
26
# File 'lib/zif/layers/simple_layer.rb', line 24

def source_sprites
  @source_sprites
end

Instance Method Details

#add_positioned_sprite(sprite:, logical_x:, logical_y:) ⇒ Object

Parameters:

  • logical_x (Integer)

    The logical X value of the given sprite

  • logical_y (Integer)

    The logical Y value of the given sprite

  • sprite (Zif::Sprite)

    The sprite to add to this layer.



55
56
57
58
# File 'lib/zif/layers/simple_layer.rb', line 55

def add_positioned_sprite(sprite:, logical_x:, logical_y:)
  # puts "SimpleLayer#add_positioned_sprite: #{logical_x} #{logical_y}"
  @source_sprites << position_sprite(sprite: sprite, logical_x: logical_x, logical_y: logical_y)
end

#reinitialize_spritesObject

This will clear the @sprites and #source_sprites arrays.



47
48
49
50
# File 'lib/zif/layers/simple_layer.rb', line 47

def reinitialize_sprites
  @source_sprites = []
  @sprites = []
end

#remove_sprite(sprite) ⇒ Object

This only removes it from the data layer, you’ll need to redraw to remove it visually

Parameters:

  • sprite (Zif::Sprite)

    The sprite to remove from this layer.



62
63
64
# File 'lib/zif/layers/simple_layer.rb', line 62

def remove_sprite(sprite)
  @source_sprites.delete(sprite)
end

#rerenderBoolean

First this checks Layerable#should_render and returns early if that is false.

Then it checks for the presence of a #rerender_rect, if it exists it will use that to RenderTarget#redraw_from_buffer

Otherwise it decides whether or not only visible sprites should be rendered - #render_only_visible - and then calls RenderTarget#redraw

Finally, it may clear #source_sprites if #clear_sprites_after_draw is true.

Returns:

  • (Boolean)

    Did this layer rerender?



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/zif/layers/simple_layer.rb', line 76

def rerender
  return false unless @should_render

  if @rerender_rect
    redraw_from_buffer(sprites: visible_sprites(@rerender_rect).to_a, cut_rect: @rerender_rect)
  else
    @sprites = if @render_only_visible
                 visible_sprites.to_a
               else
                 source_sprites
               end

    redraw
  end

  reinitialize_sprites if @clear_sprites_after_draw

  true
end