Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallax and Viewport compatibilities #55

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
37 changes: 27 additions & 10 deletions lib/chingu/parallax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = @cx, @cy
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 - @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)

Expand All @@ -122,12 +132,19 @@ 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

@layers.each do |layer|
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
while layer.repeat_x && layer.x < vpW
while layer.repeat_y && layer.y < vpH
layer.draw
layer.y += layer.image.height
end
Expand All @@ -139,7 +156,7 @@ def draw

# 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
Expand Down
20 changes: 14 additions & 6 deletions lib/chingu/viewport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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"
Expand Down