Module: Rubygoal::Moveable

Included in:
Ball, Player
Defined in:
lib/rubygoal/moveable.rb

Constant Summary collapse

MIN_DISTANCE =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#positionObject

Returns the value of attribute position.



9
10
11
# File 'lib/rubygoal/moveable.rb', line 9

def position
  @position
end

#velocityObject

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

#initializeObject



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

Returns:

  • (Boolean)


18
19
20
# File 'lib/rubygoal/moveable.rb', line 18

def moving?
  velocity.nonzero?
end

#updateObject



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