class RubyArena::Robot
Constants
- DEFAULT_RADAR_VIEW_ANGLE
- MAX_SPEED
- MAX_TURN_ANGLE
- MAX_TURN_GUN_ANGLE
- MAX_TURN_RADAR_ANGLE
- RADAR_RANGE
- SIZE
Attributes
ai[R]
arena[R]
command_parser[R]
energy[R]
gun_heading[R]
gun_heat[R]
heading[R]
radar_heading[R]
radar_view_angle[RW]
robot[R]
speed[R]
tank[R]
x[R]
y[R]
Public Class Methods
new(args)
click to toggle source
# File lib/ruby_arena/robot.rb, line 18 def initialize(args) @command_parser = CommandParser.new @ai = args.fetch(:ai).new(robot: self, command_parser: command_parser) @arena = args.fetch(:arena) @x = args[:x] || rand(arena.width - 2*size) @y = args[:y] || rand(arena.height - 2*size) @speed = args[:speed] || 0 @heading = args[:heading] || 0 @gun_heading = args[:gun_heading] || heading @radar_heading = args[:radar_heading] || gun_heading @energy = args[:energy] || 100 @scanned_robots = [] @gun_heat = args[:gun_heat] || 0 @radar_view_angle = args[:radar_view_angle] || DEFAULT_RADAR_VIEW_ANGLE end
Public Instance Methods
accelerate()
click to toggle source
# File lib/ruby_arena/robot.rb, line 64 def accelerate @speed += 1 if speed < MAX_SPEED end
actions()
click to toggle source
# File lib/ruby_arena/robot.rb, line 56 def actions command_parser.actions end
dead?()
click to toggle source
# File lib/ruby_arena/robot.rb, line 103 def dead? energy < 0 end
decelerate()
click to toggle source
# File lib/ruby_arena/robot.rb, line 68 def decelerate @speed -= 1 if speed > -MAX_SPEED end
execute_actions(actions)
click to toggle source
# File lib/ruby_arena/robot.rb, line 46 def execute_actions(actions) fire if actions[:fire] turn(actions[:turn]) if actions[:turn] turn_gun(actions[:turn_gun]) if actions[:turn_gun] turn_radar(actions[:turn_radar]) if actions[:turn_radar] accelerate if actions[:accelerate] decelerate if actions[:decelerate] self.radar_view_angle = actions[:radar_view_angle] if actions[:radar_view_angle] end
fire()
click to toggle source
# File lib/ruby_arena/robot.rb, line 90 def fire unless gun_heat > 0 bullet = new_bullet arena.add_bullet(bullet) 3.times { bullet.update } @gun_heat += 3 end end
hit(bullet)
click to toggle source
# File lib/ruby_arena/robot.rb, line 99 def hit(bullet) @energy -= bullet.energy end
radar_range()
click to toggle source
# File lib/ruby_arena/robot.rb, line 119 def radar_range RADAR_RANGE end
reset_actions()
click to toggle source
# File lib/ruby_arena/robot.rb, line 60 def reset_actions command_parser.reset_actions end
scan()
click to toggle source
# File lib/ruby_arena/robot.rb, line 107 def scan other_robots.map do |robot| if robot_in_radar_view?(robot) Gosu.distance(x, y, robot.x, robot.y) end end.compact end
size()
click to toggle source
# File lib/ruby_arena/robot.rb, line 115 def size SIZE end
tick()
click to toggle source
# File lib/ruby_arena/robot.rb, line 34 def tick ai.tick(tick_events) @gun_heat -= 0.1 if @gun_heat > 0 end
time()
click to toggle source
# File lib/ruby_arena/robot.rb, line 123 def time arena.time end
turn(angle)
click to toggle source
# File lib/ruby_arena/robot.rb, line 72 def turn(angle) angle = sanitize_maximum_value(angle, MAX_TURN_ANGLE) self.heading = heading + angle self.gun_heading = gun_heading + angle self.radar_heading = radar_heading + angle end
turn_gun(angle)
click to toggle source
# File lib/ruby_arena/robot.rb, line 79 def turn_gun(angle) angle = sanitize_maximum_value(angle, MAX_TURN_GUN_ANGLE) self.gun_heading = gun_heading + angle self.radar_heading = radar_heading + angle end
turn_radar(angle)
click to toggle source
# File lib/ruby_arena/robot.rb, line 85 def turn_radar(angle) angle = sanitize_maximum_value(angle, MAX_TURN_RADAR_ANGLE) self.radar_heading = radar_heading + angle end
update()
click to toggle source
# File lib/ruby_arena/robot.rb, line 39 def update execute_actions(actions) reset_actions move keep_robot_in_arena end
Private Instance Methods
angle_diff(angle1, angle2)
click to toggle source
# File lib/ruby_arena/robot.rb, line 161 def angle_diff(angle1, angle2) Gosu.angle_diff(angle1, angle2) end
enemy_angle(robot)
click to toggle source
# File lib/ruby_arena/robot.rb, line 157 def enemy_angle(robot) Gosu.angle(x, y, robot.x, robot.y) end
gun_heading=(angle)
click to toggle source
# File lib/ruby_arena/robot.rb, line 133 def gun_heading=(angle) @gun_heading = normalize_angle(angle) end
heading=(angle)
click to toggle source
# File lib/ruby_arena/robot.rb, line 129 def heading=(angle) @heading = normalize_angle(angle) end
keep_robot_in_arena()
click to toggle source
# File lib/ruby_arena/robot.rb, line 169 def keep_robot_in_arena @x = min_x if x < min_x @x = max_x if x > max_x @y = min_y if y < min_y @y = max_y if y > max_y end
max_x()
click to toggle source
# File lib/ruby_arena/robot.rb, line 180 def max_x arena.width - min_x end
max_y()
click to toggle source
# File lib/ruby_arena/robot.rb, line 188 def max_y arena.height - min_y end
min_x()
click to toggle source
# File lib/ruby_arena/robot.rb, line 176 def min_x size/2 end
min_y()
click to toggle source
# File lib/ruby_arena/robot.rb, line 184 def min_y size/2 end
new_bullet()
click to toggle source
# File lib/ruby_arena/robot.rb, line 149 def new_bullet Bullet.new(x: x, y: y, heading: gun_heading, arena: arena, origin: self) end
normalize_angle(angle)
click to toggle source
# File lib/ruby_arena/robot.rb, line 141 def normalize_angle(angle) angle % 360 end
other_robots()
click to toggle source
# File lib/ruby_arena/robot.rb, line 165 def other_robots arena.robots.find_all { |robot| robot != self } end
radar_heading=(angle)
click to toggle source
# File lib/ruby_arena/robot.rb, line 137 def radar_heading=(angle) @radar_heading = normalize_angle(angle) end
robot_in_radar_view?(robot)
click to toggle source
# File lib/ruby_arena/robot.rb, line 153 def robot_in_radar_view?(robot) angle_diff(radar_heading, enemy_angle(robot)).abs <= radar_view_angle/2 end
sanitize_maximum_value(number, maximum_change)
click to toggle source
# File lib/ruby_arena/robot.rb, line 192 def sanitize_maximum_value(number, maximum_change) ValueSanitizer.sanitize(number, maximum_change) end
tick_events()
click to toggle source
# File lib/ruby_arena/robot.rb, line 145 def tick_events { scanned_robots: scan } end