Class: Chingu::Parallax
- Inherits:
-
GameObject
- Object
- BasicGameObject
- GameObject
- Chingu::Parallax
- Defined in:
- lib/chingu/parallax.rb
Instance Attribute Summary collapse
-
#layers ⇒ Object
readonly
Returns the value of attribute layers.
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
-
#add_layer(arg) ⇒ Object
(also: #<<)
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 << => “landscape.png”, :damping => 1.
-
#camera_x ⇒ Object
Get the x-coordinate for the camera (inverse to x).
-
#camera_x=(x) ⇒ Object
Parallax#camera_x= works in inverse to Parallax#x (moving the “camera”, not the image).
-
#camera_y ⇒ Object
Get the y-coordinate for the camera (inverse to y).
-
#camera_y=(y) ⇒ Object
Parallax#camera_y= works in inverse to Parallax#y (moving the “camera”, not the image).
-
#draw ⇒ Object
Draw.
-
#initialize(options = {}) ⇒ Parallax
constructor
Options (in hash-format):.
-
#update ⇒ Object
TODO: make use of $window.milliseconds_since_last_update here!.
Methods inherited from GameObject
#distance_to, #inside_window?, #outside_window?
Methods included from Helpers::RotationCenter
Methods included from Helpers::InputClient
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( = {}) super() @repeat = [:repeat] || true @layers = Array.new end |
Instance Attribute Details
#layers ⇒ Object (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_x ⇒ Object
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_y ⇒ Object
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 |
#draw ⇒ Object
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 |
#update ⇒ Object
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 |