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

#destinationObject

Returns the value of attribute destination.



58
59
60
# File 'lib/rubygoal/moveable.rb', line 58

def destination
  @destination
end

#positionObject

Returns the value of attribute position.



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

def position
  @position
end

#rotationObject

Returns the value of attribute rotation.



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

def rotation
  @rotation
end

#velocityObject

Returns the value of attribute velocity.



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

def velocity
  @velocity
end

Instance Method Details

#distance(position) ⇒ Object



22
23
24
# File 'lib/rubygoal/moveable.rb', line 22

def distance(position)
  Util.distance(self.position.x, self.position.y, position.x, position.y)
end

#initializeObject



10
11
12
13
14
15
16
# File 'lib/rubygoal/moveable.rb', line 10

def initialize
  @position = Position.new(0, 0)
  @velocity = Velocity.new(0, 0)
  @speed = 0
  @destination = nil
  @rotation = 0
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

  self.rotation = Util.angle(position.x, position.y, destination.x, destination.y)
  velocity.x = Util.offset_x(rotation, speed)
  velocity.y = Util.offset_y(rotation, speed)
end

#moving?Boolean

Returns:

  • (Boolean)


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

def moving?
  velocity.nonzero?
end

#position_after_update(elapsed_time) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rubygoal/moveable.rb', line 50

def position_after_update(elapsed_time)
  custom_frame_rate = 1 / 60.0
  coef = elapsed_time / custom_frame_rate
  movement = velocity.mult(coef)

  position.add(movement)
end

#stopObject



45
46
47
48
# File 'lib/rubygoal/moveable.rb', line 45

def stop
  self.destination = nil
  self.velocity = Velocity.new(0, 0)
end

#update(elapsed_time) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/rubygoal/moveable.rb', line 34

def update(elapsed_time)
  return unless moving?

  if destination && distance(destination) < MIN_DISTANCE
    stop
    reset_rotation
  else
    self.position = position_after_update(elapsed_time)
  end
end