Class: Zif::Layers::ActiveLayer

Inherits:
CompoundSprite show all
Includes:
Layerable
Defined in:
lib/zif/layers/active_layer.rb

Overview

Designed to be used with LayerGroup.

This layer is based on CompoundSprite and therefore each component sprite is rendered every tick. Use this for layers that need to be updated frequently, but have a small sprite count.

In contrast, SimpleLayer is built on RenderTarget and therefore incurs a little performance hit every time it is redrawn, but balances that by being able to cheaply display those sprites once rendered.

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

ActiveBitmaskedTiledLayer, ActiveTiledLayer

Constant Summary

Constants inherited from Sprite

Sprite::BLENDMODE

Instance Attribute Summary

Attributes included from Layerable

#layer_name, #map, #should_render, #z_index

Attributes inherited from CompoundSprite

#labels, #sprites

Attributes inherited from Sprite

#a, #angle, #b, #g, #h, #logical_x, #logical_y, #name, #path, #r, #render_target, #source_h, #source_w, #source_x, #source_y, #w, #x, #y, #z_index

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

#actions, #dirty

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 CompoundSprite

#draw_override, #source_rect

Methods inherited from Sprite

#blend, #blend=, #center, #center_x, #center_y, #clicked?, #color, #color=, #dup_and_assign, #exclude_from_serialize, #hide, #rect, rect_array_to_hash, rect_array_to_source_hash, #rect_hash, rect_hash_to_source_hash, #show, #source_as_rect_hash, #source_center, #source_is_set?, #source_rect, #source_rect_hash, #source_wh, #source_xy, #to_h, #view_actual_size!, #wh, #xy, #zoom_factor

Methods included from Clickable

#absorb_click?, #clicked?

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

#exclude_from_serialize, #inspect, #serialize, #to_s

Methods included from Assignable

#assign

Constructor Details

#initialize(map, name, z_index: 0) ⇒ ActiveLayer

Returns a new instance of ActiveLayer.

Parameters:

  • map (Zif::Layers::LayerGroup)
  • name (Symbol)

    The name of the layer

  • z_index (Integer) (defaults to: 0)

    The z-index of the layer.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/zif/layers/active_layer.rb', line 26

def initialize(map, name, z_index: 0)
  super(name)
  @map           = map
  @layer_name    = name
  @z_index       = z_index
  @should_render = true # This does not control anything in this context since we are always rendering.
  reinitialize_sprites

  @x = 0
  @y = 0
  @w = @map.max_width
  @h = @map.max_height
  @source_x = 0
  @source_y = 0
  @source_w = @w
  @source_h = @h
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.



54
55
56
57
# File 'lib/zif/layers/active_layer.rb', line 54

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

#containing_spriteZif::Layers::ActiveLayer

This is not based on RenderTarget so we don’t have a “containing sprite” here. This is for compatibility with SimpleLayer#containing_sprite

Returns:



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

def containing_sprite
  self
end

#reinitialize_spritesObject

This will clear the sprites array.



71
72
73
# File 'lib/zif/layers/active_layer.rb', line 71

def reinitialize_sprites
  @sprites = []
end

#remove_sprite(sprite) ⇒ Object

Parameters:

  • sprite (Zif::Sprite)

    The sprite to remove from this layer.



60
61
62
# File 'lib/zif/layers/active_layer.rb', line 60

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

#rerenderBoolean

No-op - ActiveLayer is always rendering!

Returns:

  • (Boolean)

    true



77
78
79
# File 'lib/zif/layers/active_layer.rb', line 77

def rerender
  true
end

#source_spritesArray<Zif::Sprite>

This is for compatibility with SimpleLayer#source_sprites

Returns:

  • (Array<Zif::Sprite>)

    The list of sprites on this layer.



66
67
68
# File 'lib/zif/layers/active_layer.rb', line 66

def source_sprites
  @sprites
end