Class: Chingu::Parallax

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

Instance Attribute Summary collapse

Attributes inherited from GameObject

#angle, #center, #center_x, #center_y, #color, #factor, #factor_x, #factor_y, #image, #mode, #x, #y, #zorder

Attributes inherited from BasicGameObject

#options, #parent, #paused, #visible

Instance Method Summary collapse

Methods inherited from GameObject

#distance_to, #inside_window?, #outside_window?

Methods included from Helpers::RotationCenter

#rotation_center

Methods included from Helpers::InputClient

#input, #input=

Methods inherited from BasicGameObject

all, create, #destroy, destroy_all, destroy_if, #draw_trait, has_trait, has_traits, #hide!, #pause!, #setup_trait, #show!, size, #unpause!, #update_trait

Constructor Details

#initialize(options = {}) ⇒ Parallax

Options (in hash-format):

repeat: [true|false] When one layer ends within the screen, repeat/loop it



15
16
17
18
19
# File 'lib/chingu/parallax.rb', line 15

def initialize(options = {})
  super(options)
  @repeat = options[:repeat] || true
  @layers = Array.new
end

Instance Attribute Details

#layersObject (readonly)

Returns the value of attribute layers.



8
9
10
# File 'lib/chingu/parallax.rb', line 8

def layers
  @layers
end

Instance Method Details

#add_layer(arg) ⇒ Object Also known as: <<

Add one layer, either an ParallaxLayer-object or a Hash of options to create one You can also add new layers with the shortcut “<<”:

@parallax << {:image => "landscape.png", :damping => 1}


26
27
28
# File 'lib/chingu/parallax.rb', line 26

def add_layer(arg)
  @layers << (arg.is_a?(ParallaxLayer) ? arg : ParallaxLayer.new(arg))
end

#camera_xObject

Get the x-coordinate for the camera (inverse to x)



49
50
51
# File 'lib/chingu/parallax.rb', line 49

def camera_x
  -@x
end

#camera_x=(x) ⇒ Object

Parallax#camera_x= works in inverse to Parallax#x (moving the “camera”, not the image)



35
36
37
# File 'lib/chingu/parallax.rb', line 35

def camera_x=(x)
  @x = -x
end

#camera_yObject

Get the y-coordinate for the camera (inverse to y)



56
57
58
# File 'lib/chingu/parallax.rb', line 56

def camera_y
  -@y
end

#camera_y=(y) ⇒ Object

Parallax#camera_y= works in inverse to Parallax#y (moving the “camera”, not the image)



42
43
44
# File 'lib/chingu/parallax.rb', line 42

def camera_y=(y)
  @y = -y
end

#drawObject

Draw



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chingu/parallax.rb', line 76

def draw
  @layers.each do |layer|
    layer.draw
    
    save_x = layer.x
    
    ## If layer lands inside our screen, repeat it
    while (layer.x + layer.image.width) < $window.width
      layer.x += layer.image.width
      layer.draw
    end
            
    layer.x = save_x
  end
  self
end

#updateObject

TODO: make use of $window.milliseconds_since_last_update here!



63
64
65
66
67
68
69
70
71
# File 'lib/chingu/parallax.rb', line 63

def update
  @layers.each do |layer|
    layer.x = @x / layer.damping
    layer.y = @y / layer.damping
    
    # This is the magic that repeats the layer to the left and right
    layer.x -= layer.image.width  while layer.x > 0
  end
end