Class: Chingu::Parallax

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

Overview

Class for simple parallaxscrolling

See en.wikipedia.org/wiki/Parallax_scrolling for information about parallaxscrolling.

Basic usage:

@parallax = Chingu::Parallax.create(:x => 0, :y => 0)
@parallax << Chingu::ParallaxLayer.new(:image => "far_away_mountins.png", :damping => 20, :center => 0)
@parallax << Chingu::ParallaxLayer.new(:image => "trees.png", :damping => 5, :center => 0)

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

#alpha, #alpha=, #distance_to

Methods included from Helpers::RotationCenter

#rotation_center, #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!, initialize_trait, #pause!, #paused?, #setup_trait, #show!, size, #trait_options, #unpause!, #update_trait, #visible?

Methods included from Helpers::ClassInheritableAccessor

included

Constructor Details

#initialize(options = {}) ⇒ Parallax

Options (in hash-format):

repeat_x: [true|false] repeat layer on X-axis repeat_y: [true|false] repeat layer on Y-axis



42
43
44
45
46
47
48
# File 'lib/chingu/parallax.rb', line 42

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

Instance Attribute Details

#layersObject (readonly)

Returns the value of attribute layers.



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

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}


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

def add_layer(arg)
  @layers << (arg.is_a?(ParallaxLayer) ? arg : ParallaxLayer.new(arg.merge({:parallax => self})))
end

#camera_xObject

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



94
95
96
# File 'lib/chingu/parallax.rb', line 94

def camera_x
  -@x
end

#camera_x=(x) ⇒ Object

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



80
81
82
# File 'lib/chingu/parallax.rb', line 80

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

#camera_yObject

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



101
102
103
# File 'lib/chingu/parallax.rb', line 101

def camera_y
  -@y
end

#camera_y=(y) ⇒ Object

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



87
88
89
# File 'lib/chingu/parallax.rb', line 87

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

#drawObject

Draw



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/chingu/parallax.rb', line 124

def draw
  @layers.each do |layer|
    #layer.draw
    
    save_x, save_y = layer.x, layer.y
    
    # If layer lands inside our window and repeat_x is true (defaults to true), draw it until window ends
    while layer.repeat_x && layer.x < $window.width
      while layer.repeat_y && layer.y < $window.height
        layer.draw
        layer.y += layer.image.height
      end
      layer.y = save_y
      
      layer.draw
      layer.x += layer.image.width
    end
    
    # Special loop for when repeat_y is true but not repeat_x
    if layer.repeat_y && !layer.repeat_x
      while layer.repeat_y && layer.y < $window.height
        layer.draw
        layer.y += layer.image.height
      end
    end

    layer.x = save_x
  end
  self
end

#inside_window?Boolean

returns true if any part of the parallax-scroller is inside the window

Returns:

  • (Boolean)


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

def inside_window?
  return true if @repeat_x || @repeat_y
  @layers.each { |layer| return true if layer.inside_window? }
  return false
end

#outside_window?Boolean

Returns true if all parallax-layers are outside the window

Returns:

  • (Boolean)


73
74
75
# File 'lib/chingu/parallax.rb', line 73

def outside_window?
  not inside_window?
end

#updateObject

TODO: make use of $window.milliseconds_since_last_update here!



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chingu/parallax.rb', line 108

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.repeat_x && layer.x > 0)
   
    # This is the magic that repeats the layer to the left and right
    layer.y -= layer.image.height while (layer.repeat_y && layer.y > 0)
  end
end