class Rubygoal::Player

Constants

STRAIGHT_ANGLE

Attributes

coach_defined_position[RW]
error[R]
name[R]
player_movement[RW]
side[R]
time_to_kick_again[RW]
type[R]

Public Class Methods

new(game, side, name) click to toggle source
Calls superclass method Rubygoal::Moveable::new
# File lib/rubygoal/player.rb, line 17
def initialize(game, side, name)
  super()

  @time_to_kick_again = 0
  @side = side
  @player_movement = PlayerMovement.new(game, self)
  @name = name
end

Public Instance Methods

can_kick?(ball) click to toggle source
# File lib/rubygoal/player.rb, line 26
def can_kick?(ball)
  !waiting_to_kick_again? && control_ball?(ball)
end
kick(ball, target) click to toggle source
# File lib/rubygoal/player.rb, line 30
def kick(ball, target)
  direction = random_direction(target)
  strength = random_strength

  ball.move(direction, strength, name: name, side: side)
  reset_waiting_to_kick!
end
move_to_coach_position() click to toggle source
# File lib/rubygoal/player.rb, line 38
def move_to_coach_position
  move_to(coach_defined_position)
end
update(elapsed_time) click to toggle source
Calls superclass method Rubygoal::Moveable#update
# File lib/rubygoal/player.rb, line 42
def update(elapsed_time)
  update_waiting_to_kick(elapsed_time)
  player_movement.update(elapsed_time) if moving?

  super
end

Private Instance Methods

control_ball?(ball) click to toggle source
# File lib/rubygoal/player.rb, line 69
def control_ball?(ball)
  distance(ball.position) < Rubygoal.configuration.distance_control_ball
end
random_direction(target) click to toggle source
# File lib/rubygoal/player.rb, line 79
def random_direction(target)
  direction = Util.angle(position.x, position.y, target.x, target.y)

  max_angle_error = STRAIGHT_ANGLE * error
  angle_error_range = -max_angle_error..max_angle_error

  direction += Random.rand(angle_error_range)
end
random_strength() click to toggle source
# File lib/rubygoal/player.rb, line 73
def random_strength
  error_range = (1 - error)..(1 + error)
  error_coef = Random.rand(error_range)
  Rubygoal.configuration.kick_strength * error_coef
end
reset_waiting_to_kick!() click to toggle source
# File lib/rubygoal/player.rb, line 61
def reset_waiting_to_kick!
  self.time_to_kick_again = Rubygoal.configuration.kick_again_delay
end
update_waiting_to_kick(time_elapsed) click to toggle source
# File lib/rubygoal/player.rb, line 65
def update_waiting_to_kick(time_elapsed)
  self.time_to_kick_again -= time_elapsed if waiting_to_kick_again?
end
waiting_to_kick_again?() click to toggle source
# File lib/rubygoal/player.rb, line 57
def waiting_to_kick_again?
  time_to_kick_again > 0
end