Skip to content
Browse files

Fixed a number of issues regarding movement & its synchronisation (sp…

…eed pill and conveyors).
  • Loading branch information...
1 parent 1a526e9 commit 1ae646e915e3df9caaf16c1bd248585cf88f1a7d @Spooner committed Nov 28, 2011
Showing with 45 additions and 27 deletions.
  1. +3 −9 lib/objects/conveyors.rb
  2. +24 −9 lib/objects/player.rb
  3. +1 −1 lib/objects/spring.rb
  4. +11 −6 lib/statuses/hyper_speed.rb
  5. +5 −1 lib/tiles/floor_tile.rb
  6. +1 −1 lib/tiles/slow_floor.rb
View
12 lib/objects/conveyors.rb
@@ -13,23 +13,17 @@ def initialize(map, tile, position)
@scroll_rect = Rect.new(0, 0, *@sprite.image.size)
init_sprite
- end
- def collide?(other)
- other.z == 0 and other.pos.inside? self and not other.riding?
+ tile.push_velocity = direction * SPEED
end
+ def collide?; false; end # Never actually touched, but asks the tile underneath it to do the pushing.
+
def z_order
Player::Z_ORDER_SQUASHED - 1
end
def update
- scene.players.each do |player|
- if collide? player
- player.pos += direction * SPEED * frame_time
- end
- end
-
@scroll_rect.y = scene.timer.elapsed * SPEED
@sprite.sub_rect = @scroll_rect
end
View
33 lib/objects/player.rb
@@ -62,7 +62,7 @@ class Player < DynamicObject
NAMES = [:zed, :ginger]
attr_reader :name
- attr_accessor :speed_modifier
+ attr_accessor :max_speed, :speed_multiplier
def shadow_width; 1.2; end
def shadow_height; 0.6; end
@@ -94,6 +94,10 @@ def velocity_x=(velocity)
@velocity.x = velocity
end
+ def push_velocity=(velocity); @push_velocity = velocity.to_vector2; end
+ def acceleration_forced?; @acceleration_forced; end
+ def acceleration_forced=(forced); @acceleration_forced = forced; end
+
def to_s; "Player##{@name}"; end
public
@@ -118,9 +122,12 @@ def initialize(scene, tile, position, sprite_sheet, name)
@sprite.scale *= 0.75
@riding_on = nil
@state = :ok
- @speed_modifier = 1.0
+ @speed_multiplier = 1.0
@score = 0
+ @push_velocity = Vector2[0, 0] # Pushed by conveyor, for example.
+ @max_speed = MAX_SPEED
+
@sounds = {}
[:died, :jump].each do |sound|
@sounds[sound] = sound sound_path "player_#{sound}.ogg"
@@ -153,7 +160,7 @@ def view_range_x
protected
def effective_velocity
- @velocity * @speed_modifier
+ @velocity * @speed_multiplier + @push_velocity
end
protected
@@ -252,7 +259,15 @@ def update
else
case @state
when :ok
- @speed_modifier = @tile.speed if @tile and z == 0 and not riding?
+ if @tile
+ if z > 0
+ @push_velocity = Vector2[0, 0]
+ @speed_multiplier = 1.0 if riding?
+ elsif
+ @speed_multiplier = @tile.speed_multiplier
+ @push_velocity = @tile.push_velocity
+ end
+ end
update_controls unless disabled? :controls
update_physics unless disabled? :physics
@@ -279,7 +294,7 @@ def update_animation
elsif z == 0
# Sitting, running or walking.
vel = effective_velocity.length
- if vel == 0
+ if @velocity == [0, 0] # Could be being pushed.
@player_animations[:sitting].update
elsif vel >= MIN_RUN_VELOCITY
@player_animations[:running].duration = (100 - vel) / 20.0
@@ -312,12 +327,12 @@ def update_controls
end
# Accelerate and decelerate.
- if holding? @controls[:left]
+ if acceleration_forced? or holding? @controls[:right]
+ @velocity.x += ACCELERATION * frame_time
+ @velocity.x = [@velocity.x, @max_speed].min
+ elsif holding? @controls[:left]
@velocity.x += DECELERATION * frame_time
@velocity.x = [@velocity.x, MIN_SPEED].max
- elsif holding? @controls[:right]
- @velocity.x += ACCELERATION * frame_time
- @velocity.x = [@velocity.x, MAX_SPEED].min
end
end
View
2 lib/objects/spring.rb
@@ -46,7 +46,7 @@ def update
player.z += 0.000001 # Just so we only collide with ONE spring.
player.velocity_z = JUMP_Z_SPEED
@bounce_sound.play
- player.speed_modifier = JUMP_SPEED_MODIFIER
+ player.speed_multiplier *= JUMP_SPEED_MODIFIER
@activated = true
break
end
View
17 lib/statuses/hyper_speed.rb
@@ -4,7 +4,6 @@ class Status
# Speeds up the player, but forces them to run.
class HyperSpeed < Status
SPEED_FACTOR = 1.5
- ACCELERATION = 300
def default_duration; 3; end
@@ -13,7 +12,8 @@ def setup
@sound.volume = 30 * (scene.user_data.effects_volume / 50.0)
@sound.play
- @speed = Player::MAX_SPEED * SPEED_FACTOR
+ owner.max_speed = owner.class::MAX_SPEED * SPEED_FACTOR
+ owner.acceleration_forced = true
end
def reapply(options = {})
@@ -22,10 +22,9 @@ def reapply(options = {})
end
def update
- if owner.ok? and not owner.disabled? :controls
- # Make sure the player goes FAST!
- owner.velocity_x = [owner.velocity_x + ACCELERATION * frame_time, @speed].min if owner.velocity_x < @speed
+ super
+ if owner.ok? and not owner.disabled? :controls
# Create a blurry trail behind the player.
time = scene.timer.elapsed
color = Color.from_hsv(360 - ((time * 250) % 360), 1.0, 1.0)
@@ -36,6 +35,12 @@ def update
else
owner.remove_status type
end
- end
+ end
+
+ def clean_up
+ super
+ owner.acceleration_forced = false
+ owner.max_speed = owner.class::MAX_SPEED
+ end
end
end
View
6 lib/tiles/floor_tile.rb
@@ -4,7 +4,9 @@ class FloorTile < Tile
SKEW = 0.5
IMAGE_SIZE = Vector2[64, 64] # Size of the tiles sprite-sheet.
- def speed; 1.0; end
+ def speed_multiplier; 1.0; end
+
+ attr_accessor :push_velocity
class << self
def height; 6; end
@@ -16,5 +18,7 @@ def initialize(sprite_position, grid_position, offset)
@sprite.x += grid_position.y * height * SKEW
@sprite.scale_y = height / width.to_f
@sprite.skew_x(SKEW * @sprite.scale_y)
+
+ @push_velocity = Vector2[0, 0] # Unless tile has a conveyor on it, no push.
end
end
View
2 lib/tiles/slow_floor.rb
@@ -23,7 +23,7 @@ def shader_time=(time)
end
end
- def speed; 0.25; end
+ def speed_multiplier; 0.25; end
def initialize(grid_position, offset)
super([2, 0], grid_position, offset)

0 comments on commit 1ae646e

Please sign in to comment.
Something went wrong with that request. Please try again.