Class: Chingu::ParallaxLayer

Inherits:
GameObject show all
Defined in:
lib/chingu/parallax.rb

Overview

ParallaxLayer is mainly used by class Parallax to keep track of the different layers. If you @parallax << { :image => “foo.png” } a ParallaxLayer will be created automaticly from that Hash.

If no zorder is provided the ParallaxLayer-class increments an internal zorder number which will put the last layer added on top of the rest.

Instance Attribute Summary collapse

Attributes inherited from BasicGameObject

#options, #parent, #paused

Instance Method Summary collapse

Methods included from Helpers::InputClient

#add_inputs, #holding?, #holding_all?, #holding_any?, #input, #input=, #on_input

Methods inherited from BasicGameObject

all, create, #destroy, destroy_all, destroy_if, #draw, #draw_trait, each, each_with_index, #filename, initialize_trait, #pause!, #paused?, select, #setup, #setup_trait, size, trait, #trait_options, traits, #unpause!, #update, #update_trait

Methods included from Helpers::ClassInheritableAccessor

included

Constructor Details

#initialize(options) ⇒ ParallaxLayer

Returns a new instance of ParallaxLayer



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/chingu/parallax.rb', line 165

def initialize(options)      
  @parallax = options[:parallax]      
  # No auto update/draw, the parentclass Parallax takes care of that!
  options.merge!(:visible => false, :paused => true)

  options = {:rotation_center => @parallax.options[:rotation_center]}.merge(options)  if @parallax
  
  #
  # Default arguments for repeat_x and repeat_y
  # If no zorder is given, use a global incrementing counter. 
  # First added, furthest behind when drawn.
  #
  options = {
      :repeat_x => true, 
      :repeat_y => false, 
      :zorder   => @parallax ? (@parallax.zorder + @parallax.layers.count) : 100
  }.merge(options)
        
  @repeat_x = options[:repeat_x]
  @repeat_y = options[:repeat_y]
        
  super(options)
  
  @damping = options[:damping] || 1
end

Instance Attribute Details

#dampingObject (readonly)

Returns the value of attribute damping



162
163
164
# File 'lib/chingu/parallax.rb', line 162

def damping
  @damping
end

#repeat_xObject

Returns the value of attribute repeat_x



163
164
165
# File 'lib/chingu/parallax.rb', line 163

def repeat_x
  @repeat_x
end

#repeat_yObject

Returns the value of attribute repeat_y



163
164
165
# File 'lib/chingu/parallax.rb', line 163

def repeat_y
  @repeat_y
end

Instance Method Details

#get_pixel(x, y) ⇒ Object

Gets pixel from layers image The pixel is from the window point of view, so coordinates are converted:

@parallax.layers.first.get_pixel(10, 10)        # the visible pixel at 10, 10
@parallax.layers.first.image.get_pixel(10, 10)  # gets pixel 10, 10 from layers image no matter where layer is positioned


198
199
200
201
202
203
204
205
206
# File 'lib/chingu/parallax.rb', line 198

def get_pixel(x, y)
  image_x = x - @x
  image_y = y - @y
  
  # On a 100 x 100 image, get_pixel works to 99 x 99
  image_x -= @image.width   while image_x >= @image.width 
  
  @image.get_pixel(image_x, image_y)
end