From adda105553aa23cc5f718db44753784c28951e3a Mon Sep 17 00:00:00 2001 From: "J. Santos" Date: Thu, 23 Aug 2012 01:12:23 -0300 Subject: [PATCH 1/4] Parallax working with trait :viewport Solved the incompatibilities between parallaxes and viewport. Some samples was been tested, include the example3_parallax.rb, found on default examples. To use the Parallax with Viewport you must change the properties camera_x and/or camera_y from Parallax, ever that you change the Viewport boundaries. --- lib/chingu/parallax.rb | 54 +++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/lib/chingu/parallax.rb b/lib/chingu/parallax.rb index ddf85036..a8d4abad 100644 --- a/lib/chingu/parallax.rb +++ b/lib/chingu/parallax.rb @@ -43,6 +43,8 @@ def initialize(options = {}) super(options) @repeat_x = options[:repeat_x] || true @repeat_y = options[:repeat_y] || false + @cx = 0 + @cy = 0 @layers = Array.new end @@ -78,38 +80,46 @@ def outside_window? # Parallax#camera_x= works in inverse to Parallax#x (moving the "camera", not the image) # def camera_x=(x) - @x = -x + @cx = -x end # # Parallax#camera_y= works in inverse to Parallax#y (moving the "camera", not the image) # def camera_y=(y) - @y = -y + @cy = -y end # # Get the x-coordinate for the camera (inverse to x) # def camera_x - -@x + -@cx end # # Get the y-coordinate for the camera (inverse to y) # def camera_y - -@y + -@cy end # # TODO: make use of $window.milliseconds_since_last_update here! # def update + # Viewport data, from GameState parent + if self.parent.respond_to? :viewport + vpX, vpY = self.camera_x, self.camera_y + else + vpX, vpY = 0, 0 + end + @layers.each do |layer| - layer.x = @x / layer.damping - layer.y = @y / layer.damping - + # Get the points which need start to draw + layer.x = self.x + (vpX - self.camera_x/layer.damping.to_f).round + layer.y = self.y + (vpY - self.camera_y/layer.damping.to_f).round + # 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) @@ -122,30 +132,42 @@ def update # Draw # def draw +# # Viewport data, from GameState parent +# if self.parent.respond_to? :viewport +# gaX, gaY, vpW, vpH = self.parent.viewport.game_area +# else +# gaX, gaY, vpW, vpH = 0, 0, $window.width, $window.height +# end +# vpX, vpY = @x, @y + + if self.parent.respond_to? :viewport + gaX, gaY, vpW, vpH = self.parent.viewport.game_area + else + gaX, gaY, vpW, vpH = 0, 0, $window.width, $window.height + end + @layers.each do |layer| - save_x, save_y = layer.x, layer.y - + saveX, saveY = 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 + while layer.repeat_x && layer.x < vpW + while layer.repeat_y && layer.y < vpH layer.draw layer.y += layer.image.height end - layer.y = save_y - + layer.y = saveY + 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 + while layer.repeat_y && layer.y < vpH layer.draw layer.y += layer.image.height end end - - layer.x = save_x + layer.x = saveX end self end From 99306d3a76ece0fbb52cc15f3078bab2a938a569 Mon Sep 17 00:00:00 2001 From: "J. Santos" Date: Thu, 23 Aug 2012 01:54:02 -0300 Subject: [PATCH 2/4] Change the README to edit the parallax note --- README.rdoc | 10 ++++++---- lib/chingu/parallax.rb | 33 ++++++++++++++------------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/README.rdoc b/README.rdoc index 3ac99e1d..d752899a 100644 --- a/README.rdoc +++ b/README.rdoc @@ -111,8 +111,9 @@ An "@image = @animation.next" in your Player#update is usually enough to get you === Chingu::Parallax A class for easy parallaxscrolling. Add layers with different damping, move the camera to generate a new snapshot. See example3.rb for more. -NOTE: Doing Parallax.create when using a trait viewport will give bad results. -If you need parallax together with viewport do Parallax.new and then manually doing parallax.update/draw. +NOTE: -Doing Parallax.create when using a trait viewport will give bad results.- +-If you need parallax together with viewport do Parallax.new and then manually doing parallax.update/draw.- +Now the Parallax.create can be used with the trait viewport. Everytime the viewport boundaries change, the Paralax.camera_x/camera_y must be updated too. === Chingu::HighScoreList A class to keep track of high scores, limit the list, automatic sorting on score, save/load to disc. See example13.rb for more. @@ -757,8 +758,9 @@ This is great for scrolling games. You also have: viewport.lag = 0.95 # Set a lag-factor to use in combination with x_target / y_target viewport.x_target = 100 # This will move viewport towards X-coordinate 100, the speed is determined by the lag-parameter. -NOTE: Doing Parallax.create when using a trait viewport will give bad results. -If you need parallax together with viewport do Parallax.new and then manually doing parallax.update/draw. +NOTE: -Doing Parallax.create when using a trait viewport will give bad results.- +-If you need parallax together with viewport do Parallax.new and then manually doing parallax.update/draw.- +Now you can use normally the Parallax with viewport. ==== Trait "collision_detection" Adds class and instance methods for basic collision detection. diff --git a/lib/chingu/parallax.rb b/lib/chingu/parallax.rb index a8d4abad..3c3fdcd3 100644 --- a/lib/chingu/parallax.rb +++ b/lib/chingu/parallax.rb @@ -80,28 +80,28 @@ def outside_window? # Parallax#camera_x= works in inverse to Parallax#x (moving the "camera", not the image) # def camera_x=(x) - @cx = -x + @cx = x end # # Parallax#camera_y= works in inverse to Parallax#y (moving the "camera", not the image) # def camera_y=(y) - @cy = -y + @cy = y end # # Get the x-coordinate for the camera (inverse to x) # def camera_x - -@cx + @cx end # # Get the y-coordinate for the camera (inverse to y) # def camera_y - -@cy + @cy end # @@ -110,15 +110,15 @@ def camera_y def update # Viewport data, from GameState parent if self.parent.respond_to? :viewport - vpX, vpY = self.camera_x, self.camera_y + vpX, vpY = @cx, @cy else vpX, vpY = 0, 0 end @layers.each do |layer| # Get the points which need start to draw - layer.x = self.x + (vpX - self.camera_x/layer.damping.to_f).round - layer.y = self.y + (vpY - self.camera_y/layer.damping.to_f).round + layer.x = self.x + (vpX - @cx/layer.damping.to_f).round + layer.y = self.y + (vpY - @cy/layer.damping.to_f).round # 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) @@ -132,14 +132,7 @@ def update # Draw # def draw -# # Viewport data, from GameState parent -# if self.parent.respond_to? :viewport -# gaX, gaY, vpW, vpH = self.parent.viewport.game_area -# else -# gaX, gaY, vpW, vpH = 0, 0, $window.width, $window.height -# end -# vpX, vpY = @x, @y - + # Viewport data, from GameState parent if self.parent.respond_to? :viewport gaX, gaY, vpW, vpH = self.parent.viewport.game_area else @@ -147,15 +140,16 @@ def draw end @layers.each do |layer| - saveX, saveY = layer.x, layer.y + 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 < vpW while layer.repeat_y && layer.y < vpH layer.draw layer.y += layer.image.height end - layer.y = saveY - + layer.y = save_y + layer.draw layer.x += layer.image.width end @@ -167,7 +161,8 @@ def draw layer.y += layer.image.height end end - layer.x = saveX + + layer.x = save_x end self end From 564afc03112ee849482e839090e04f448e880331 Mon Sep 17 00:00:00 2001 From: "J. Santos" Date: Thu, 23 Aug 2012 01:54:02 -0300 Subject: [PATCH 3/4] Change the README to edit the parallax note --- README.rdoc | 7 +++---- lib/chingu/parallax.rb | 33 ++++++++++++++------------------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/README.rdoc b/README.rdoc index 3ac99e1d..8c07a7e9 100644 --- a/README.rdoc +++ b/README.rdoc @@ -111,8 +111,8 @@ An "@image = @animation.next" in your Player#update is usually enough to get you === Chingu::Parallax A class for easy parallaxscrolling. Add layers with different damping, move the camera to generate a new snapshot. See example3.rb for more. -NOTE: Doing Parallax.create when using a trait viewport will give bad results. -If you need parallax together with viewport do Parallax.new and then manually doing parallax.update/draw. + +NOTE: Now the Parallax.create can be used with the trait viewport. Everytime the viewport boundaries change, the Paralax.camera_x/camera_y must be updated too. === Chingu::HighScoreList A class to keep track of high scores, limit the list, automatic sorting on score, save/load to disc. See example13.rb for more. @@ -757,8 +757,7 @@ This is great for scrolling games. You also have: viewport.lag = 0.95 # Set a lag-factor to use in combination with x_target / y_target viewport.x_target = 100 # This will move viewport towards X-coordinate 100, the speed is determined by the lag-parameter. -NOTE: Doing Parallax.create when using a trait viewport will give bad results. -If you need parallax together with viewport do Parallax.new and then manually doing parallax.update/draw. +NOTE: Now you can use normally the Parallax with viewport. ==== Trait "collision_detection" Adds class and instance methods for basic collision detection. diff --git a/lib/chingu/parallax.rb b/lib/chingu/parallax.rb index a8d4abad..3c3fdcd3 100644 --- a/lib/chingu/parallax.rb +++ b/lib/chingu/parallax.rb @@ -80,28 +80,28 @@ def outside_window? # Parallax#camera_x= works in inverse to Parallax#x (moving the "camera", not the image) # def camera_x=(x) - @cx = -x + @cx = x end # # Parallax#camera_y= works in inverse to Parallax#y (moving the "camera", not the image) # def camera_y=(y) - @cy = -y + @cy = y end # # Get the x-coordinate for the camera (inverse to x) # def camera_x - -@cx + @cx end # # Get the y-coordinate for the camera (inverse to y) # def camera_y - -@cy + @cy end # @@ -110,15 +110,15 @@ def camera_y def update # Viewport data, from GameState parent if self.parent.respond_to? :viewport - vpX, vpY = self.camera_x, self.camera_y + vpX, vpY = @cx, @cy else vpX, vpY = 0, 0 end @layers.each do |layer| # Get the points which need start to draw - layer.x = self.x + (vpX - self.camera_x/layer.damping.to_f).round - layer.y = self.y + (vpY - self.camera_y/layer.damping.to_f).round + layer.x = self.x + (vpX - @cx/layer.damping.to_f).round + layer.y = self.y + (vpY - @cy/layer.damping.to_f).round # 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) @@ -132,14 +132,7 @@ def update # Draw # def draw -# # Viewport data, from GameState parent -# if self.parent.respond_to? :viewport -# gaX, gaY, vpW, vpH = self.parent.viewport.game_area -# else -# gaX, gaY, vpW, vpH = 0, 0, $window.width, $window.height -# end -# vpX, vpY = @x, @y - + # Viewport data, from GameState parent if self.parent.respond_to? :viewport gaX, gaY, vpW, vpH = self.parent.viewport.game_area else @@ -147,15 +140,16 @@ def draw end @layers.each do |layer| - saveX, saveY = layer.x, layer.y + 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 < vpW while layer.repeat_y && layer.y < vpH layer.draw layer.y += layer.image.height end - layer.y = saveY - + layer.y = save_y + layer.draw layer.x += layer.image.width end @@ -167,7 +161,8 @@ def draw layer.y += layer.image.height end end - layer.x = saveX + + layer.x = save_x end self end From 7995779acf397364d760738db9d1c796a6e6f621 Mon Sep 17 00:00:00 2001 From: "J. Santos" Date: Tue, 18 Sep 2012 08:26:10 -0300 Subject: [PATCH 4/4] Created the `width` property for `Viewport` --- lib/chingu/viewport.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/chingu/viewport.rb b/lib/chingu/viewport.rb index 8cbb88ce..d1891847 100644 --- a/lib/chingu/viewport.rb +++ b/lib/chingu/viewport.rb @@ -59,8 +59,8 @@ def lag=(lag) # TODO: Add support for x,y here! # def center_around(object) - self.x = object.x * @factor_x - $window.width / 2 - self.y = object.y * @factor_y - $window.height / 2 + self.x = object.x * @factor_x - (@width || $window.width) / 2 + self.y = object.y * @factor_y - (@height || $window.height) / 2 end # @@ -88,8 +88,8 @@ def game_area_object=(game_object) # def inside?(object, y = nil) x, y = y ? [object,y] : [object.x, object.y] - x >= @x && x <= (@x + $window.width) && - y >= @y && y <= (@y + $window.height) + x >= @x && x <= (@x + (@width || $window.width)) && + y >= @y && y <= (@y + (@height || $window.height)) end # Returns true object is outside the view port @@ -135,7 +135,7 @@ def x=(x) @x = x if @game_area @x = @game_area.x * @factor_x if @x < @game_area.x * @factor_x - @x = @game_area.width * @factor_x - $window.width if @x > @game_area.width * @factor_x - $window.width + @x = @game_area.width * @factor_x - (@width || $window.width) if @x > @game_area.width * @factor_x - (@width || $window.width) end end @@ -146,9 +146,17 @@ def y=(y) @y = y if @game_area @y = @game_area.y * @factor_y if @y < @game_area.y * @factor_y - @y = @game_area.height * @factor_y - $window.height if @y > @game_area.height * @factor_y - $window.height + @y = @game_area.height * @factor_y - (@height || $window.height) if @y > @game_area.height * @factor_y - (@height || $window.height) end end + + def width=(width) + @width = width + end + + def height=(width) + @height = height + end # # Apply the X/Y viewport-translation, used by trait "viewport"