Module: Rubygoal::Moveable
Constant Summary collapse
- MIN_DISTANCE =
10
Instance Attribute Summary collapse
-
#position ⇒ Object
Returns the value of attribute position.
-
#velocity ⇒ Object
Returns the value of attribute velocity.
Instance Method Summary collapse
- #distance(position) ⇒ Object
- #initialize ⇒ Object
- #move_to(destination) ⇒ Object
- #moving? ⇒ Boolean
- #update ⇒ Object
Instance Attribute Details
#position ⇒ Object
Returns the value of attribute position.
9 10 11 |
# File 'lib/rubygoal/moveable.rb', line 9 def position @position end |
#velocity ⇒ Object
Returns the value of attribute velocity.
9 10 11 |
# File 'lib/rubygoal/moveable.rb', line 9 def velocity @velocity end |
Instance Method Details
#distance(position) ⇒ Object
22 23 24 |
# File 'lib/rubygoal/moveable.rb', line 22 def distance(position) Gosu.distance(self.position.x, self.position.y, position.x, position.y) end |
#initialize ⇒ Object
11 12 13 14 15 16 |
# File 'lib/rubygoal/moveable.rb', line 11 def initialize @position = Position.new(0, 0) @velocity = Velocity.new(0, 0) @speed = 0 @destination = nil end |
#move_to(destination) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/rubygoal/moveable.rb', line 26 def move_to(destination) self.destination = destination angle = Gosu.angle(position.x, position.y, destination.x, destination.y) velocity.x = Gosu.offset_x(angle, speed) velocity.y = Gosu.offset_y(angle, speed) end |
#moving? ⇒ Boolean
18 19 20 |
# File 'lib/rubygoal/moveable.rb', line 18 def moving? velocity.nonzero? end |
#update ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rubygoal/moveable.rb', line 34 def update return unless moving? if destination && distance(destination) < MIN_DISTANCE stop else position.x += velocity.x position.y += velocity.y end end |