Skip to content
Browse files

Fixed a bug on positioning players.

  • Loading branch information...
1 parent 288aa57 commit f14b7b8726dc33b67c4806c5df03bc67b63a7c9b @mauricioszabo committed Dec 3, 2011
Showing with 14 additions and 3 deletions.
  1. +6 −1 lib/player.rb
  2. +8 −2 spec/player_spec.rb
View
7 lib/player.rb
@@ -23,6 +23,7 @@ def initialize(file, x=0, y=0)
@animations = (1..8).collect { |number| Sprite.new "#{file}-#{number}" }
@current_animation = 0
@x, @y = x, y
+ @destination_x, @destination_y = x, y
@turn_time = 0
@collision_box = Square.new
end
@@ -39,7 +40,11 @@ def update_steps
distance_x = @destination_x.to_i - @x
distance_y = @destination_y.to_i - @y
total_distance = (distance_x.abs + distance_y.abs).to_f
- return if total_distance <= STEP_SIZE
+ if total_distance <= STEP_SIZE
+ @x = @destination_x
+ @y = @destination_y
+ return
+ end
new_animation = direction_to(@destination_x, @destination_y)
if new_animation == @current_animation
View
10 spec/player_spec.rb
@@ -17,6 +17,12 @@
player.direction_to(10, 0).should == 0
end
+ it 'should not move if direction is not given' do
+ player.update
+ player.x.should == 10
+ player.y.should == 10
+ end
+
it 'should calculate the turning animation' do
player.turn_to(Player::UPPER_RIGHT).should == Player::UPPER
player.turn_to(Player::LOWER_LEFT).should == Player::LEFT
@@ -41,11 +47,11 @@
player.current_animation.should == 7
end
- it 'should not update the position if it\'s not enough distant' do
+ it 'should end the animation on final position' do
player.goto 10, 9
player.update
player.x.should == 10
- player.y.should == 10
+ player.y.should == 9
end
it 'should update the current position, but not walk more than 5 steps' do

0 comments on commit f14b7b8

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