class Rubygoal::PlayerMovement

Constants

CLOSE_TO_DESTINATION
PLAYERS_CLOSE_DISTANCE
PLAYERS_MIN_DISTANCE

Attributes

elapsed_time[RW]
game[R]
player[R]

Public Class Methods

new(game, player) click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 12
def initialize(game, player)
  @game = game
  @player = player
end

Public Instance Methods

update(elapsed_time) click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 17
def update(elapsed_time)
  self.elapsed_time = elapsed_time

  if blocking_player
    if close_to_destination? || any_moving_and_very_close_player?
      player.stop
    elsif blocking_player_very_close?
      adapt_velocity_when_very_close
    elsif blocking_player_close?
      adapt_velocity_when_close
    end
  end
end

Private Instance Methods

adapt_velocity_when_close() click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 81
def adapt_velocity_when_close
  distance_to_run =
    blocking_player.distance(position_after_update) - PLAYERS_MIN_DISTANCE

  close_range_distance = (PLAYERS_CLOSE_DISTANCE - PLAYERS_MIN_DISTANCE).to_f

  # We want to decelerate when close, but we do not want to
  # have velocity = 0, so we add 0.5 to still be in movement
  deceleration_coef = (distance_to_run * 0.5) / close_range_distance + 0.5

  player.velocity = velocity.mult(deceleration_coef)
end
adapt_velocity_when_very_close() click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 71
def adapt_velocity_when_very_close
  vel_angle = Util.angle(0, 0, velocity.x, velocity.y) - 45
  vel_magnitude = Util.distance(0, 0, velocity.x, velocity.y)

  player.velocity = Velocity.new(
    Util.offset_x(vel_angle, vel_magnitude),
    Util.offset_y(vel_angle, vel_magnitude),
  )
end
any_moving_and_very_close_player?() click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 65
def any_moving_and_very_close_player?
  game_players_closer_to_destination.any? do |p|
    p.moving? && p.distance(position_after_update) < PLAYERS_MIN_DISTANCE
  end
end
blocking_player() click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 47
def blocking_player
  game_players_closer_to_destination.min_by do |p|
    position_after_update.distance(p.position)
  end
end
blocking_player_close?() click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 61
def blocking_player_close?
  blocking_player.distance(position) < PLAYERS_CLOSE_DISTANCE
end
blocking_player_very_close?() click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 57
def blocking_player_very_close?
  blocking_player.distance(position) < PLAYERS_MIN_DISTANCE
end
close_to_destination?() click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 53
def close_to_destination?
  position_after_update.distance(destination) < CLOSE_TO_DESTINATION
end
game_players_closer_to_destination() click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 40
def game_players_closer_to_destination
  game_players_except_me.select do |p|
    destination.distance(position_after_update) >
      destination.distance(p.position)
  end
end
game_players_except_me() click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 36
def game_players_except_me
  game.players - [player]
end
position_after_update() click to toggle source
# File lib/rubygoal/players/player_movement.rb, line 94
def position_after_update
  player.position_after_update(elapsed_time)
end