class Rubots::Robot

Constants

ANGLE_STEP
GUN_ANGLE_STEP
MAX_ANGLE

Some constants

MAX_GUN_ANGLE
MAX_THROTTLE
SPEED_FACTOR
THROTTLE_STEP

Attributes

angle[R]
desired_angle[W]
desired_gun_angle[W]
desired_throttle[W]
gun_angle[R]
x[R]
y[R]

Public Class Methods

new(strategy_class, game, x, y) click to toggle source
# File lib/rubots/robot.rb, line 21
def initialize(strategy_class, game, x, y)
  @x = x
  @y = y
  @throttle  = @desired_throttle  = 0.0
  @angle     = @desired_angle     = 0.0
  @gun_angle = @desired_gun_angle = 0.0
  @game = game

  @strategy = strategy_class.new(@game.map, robot_data, nil)

  @destroyed = false

  @cooldown_timer = 0
end

Public Instance Methods

destroy() click to toggle source
# File lib/rubots/robot.rb, line 72
def destroy
  @throttle = 0
  @destroyed = true
end
destroyed?() click to toggle source
# File lib/rubots/robot.rb, line 77
def destroyed?
  @destroyed
end
distance_to(other) click to toggle source
# File lib/rubots/robot.rb, line 66
def distance_to(other)
  x_dist = @x - other.x
  y_dist = @y - other.y
  Math.sqrt(x_dist ** 2 + y_dist ** 2)
end
do_fire() click to toggle source
# File lib/rubots/robot.rb, line 62
def do_fire
  @firing = true
end
name() click to toggle source
# File lib/rubots/robot.rb, line 58
def name
  @strategy.name
end
process_command() click to toggle source
# File lib/rubots/robot.rb, line 36
def process_command
  return if @destroyed

  if @cooldown_timer > 0
    @cooldown_timer -= 1
  else
    command = @strategy.get_command(robot_data, targets_data)
    command.apply_to(self)
    @cooldown_timer = command.cooldown
  end
end
tick() click to toggle source
# File lib/rubots/robot.rb, line 48
def tick
  return if @destroyed

  tick_angle
  tick_throttle
  tick_movement
  tick_gun
  tick_fire
end

Private Instance Methods

robot_data() click to toggle source
# File lib/rubots/robot.rb, line 83
def robot_data
  OpenStruct.new x: @x, y: @y, angle: @angle, throttle: @throttle, gun_angle: @gun_angle
end
targets_data() click to toggle source
# File lib/rubots/robot.rb, line 87
def targets_data
  robots_arrays = @game.robots.slice_before(self).to_a
  robots_arrays.last.shift # First is self
  robots = robots_arrays.reverse.inject(&:+)
  robots.map do |target_robot|
    next if target_robot == self
    x_dist = @x - target_robot.x
    y_dist = @y - target_robot.y
    distance = Math.sqrt(x_dist ** 2 + y_dist ** 2)
    actual_angle = Math.atan2(x_dist, -y_dist) * 180 / Math::PI + 180
    relative_angle = (actual_angle - @angle) % 360

    OpenStruct.new name: target_robot.name, distance: distance, angle: relative_angle
  end.compact
end
tick_angle() click to toggle source
# File lib/rubots/robot.rb, line 103
def tick_angle
  if @desired_angle != @angle
    if (@desired_angle - @angle).abs < ANGLE_STEP
      @angle = @desired_angle # Fractional angles
    else
      diff = @desired_angle - @angle
      sign = diff / diff.abs
      sign = sign * -1 if diff.abs > MAX_ANGLE / 2.0
      @angle += sign * ANGLE_STEP
      @angle += MAX_ANGLE if @angle < 0
      @angle -= MAX_ANGLE if @angle >= MAX_ANGLE
    end
  end
end
tick_fire() click to toggle source
# File lib/rubots/robot.rb, line 153
def tick_fire
  if @firing
    @game.laser_fire(Beam.from(self))
    @firing = false
  end
end
tick_gun() click to toggle source
# File lib/rubots/robot.rb, line 138
def tick_gun
  if @desired_gun_angle != @gun_angle
    if (@desired_gun_angle - @gun_angle).abs < GUN_ANGLE_STEP
      @gun_angle = @desired_gun_angle # Fractional angles
    else
      diff = @desired_gun_angle - @gun_angle
      sign = diff / diff.abs
      sign = sign * -1 if diff.abs > MAX_GUN_ANGLE / 2.0
      @gun_angle += sign * GUN_ANGLE_STEP
      @gun_angle += MAX_GUN_ANGLE if @gun_angle < 0
      @gun_angle -= MAX_GUN_ANGLE if @gun_angle >= MAX_GUN_ANGLE
    end
  end
end
tick_movement() click to toggle source
# File lib/rubots/robot.rb, line 129
def tick_movement
  movement = @throttle * SPEED_FACTOR
  rad_angle = angle * Math::PI / 180
  mov_x = Math.sin(rad_angle) * movement
  mov_y = Math.cos(rad_angle) * movement * -1
  @x += mov_x
  @y += mov_y
end
tick_throttle() click to toggle source
# File lib/rubots/robot.rb, line 118
def tick_throttle
  if @desired_throttle != @throttle
    diff = @desired_throttle - @throttle
    if diff.abs < THROTTLE_STEP
      @throttle = @desired_throttle
    else
      @throttle += (diff / diff.abs) * THROTTLE_STEP
    end
  end
end