Class: RubyArena::Robot
- Inherits:
-
Object
- Object
- RubyArena::Robot
- Includes:
- Movable
- Defined in:
- lib/ruby_arena/robot.rb
Constant Summary collapse
- SIZE =
40- MAX_SPEED =
8- MAX_TURN_ANGLE =
10- MAX_TURN_GUN_ANGLE =
30- MAX_TURN_RADAR_ANGLE =
60- RADAR_RANGE =
1000- DEFAULT_RADAR_VIEW_ANGLE =
10
Instance Attribute Summary collapse
-
#ai ⇒ Object
readonly
Returns the value of attribute ai.
-
#arena ⇒ Object
readonly
Returns the value of attribute arena.
-
#command_parser ⇒ Object
readonly
Returns the value of attribute command_parser.
-
#energy ⇒ Object
readonly
Returns the value of attribute energy.
-
#gun_heading ⇒ Object
readonly
Returns the value of attribute gun_heading.
-
#gun_heat ⇒ Object
readonly
Returns the value of attribute gun_heat.
-
#heading ⇒ Object
readonly
Returns the value of attribute heading.
-
#radar_heading ⇒ Object
readonly
Returns the value of attribute radar_heading.
-
#radar_view_angle ⇒ Object
Returns the value of attribute radar_view_angle.
-
#robot ⇒ Object
readonly
Returns the value of attribute robot.
-
#speed ⇒ Object
readonly
Returns the value of attribute speed.
-
#tank ⇒ Object
readonly
Returns the value of attribute tank.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #accelerate ⇒ Object
- #actions ⇒ Object
- #dead? ⇒ Boolean
- #decelerate ⇒ Object
- #execute_actions(actions) ⇒ Object
- #fire ⇒ Object
- #hit(bullet) ⇒ Object
-
#initialize(args) ⇒ Robot
constructor
A new instance of Robot.
- #radar_range ⇒ Object
- #reset_actions ⇒ Object
- #scan ⇒ Object
- #size ⇒ Object
- #tick ⇒ Object
- #time ⇒ Object
- #turn(angle) ⇒ Object
- #turn_gun(angle) ⇒ Object
- #turn_radar(angle) ⇒ Object
- #update ⇒ Object
Methods included from Movable
Constructor Details
#initialize(args) ⇒ Robot
Returns a new instance of Robot.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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 |
Instance Attribute Details
#ai ⇒ Object (readonly)
Returns the value of attribute ai.
13 14 15 |
# File 'lib/ruby_arena/robot.rb', line 13 def ai @ai end |
#arena ⇒ Object (readonly)
Returns the value of attribute arena.
13 14 15 |
# File 'lib/ruby_arena/robot.rb', line 13 def arena @arena end |
#command_parser ⇒ Object (readonly)
Returns the value of attribute command_parser.
13 14 15 |
# File 'lib/ruby_arena/robot.rb', line 13 def command_parser @command_parser end |
#energy ⇒ Object (readonly)
Returns the value of attribute energy.
15 16 17 |
# File 'lib/ruby_arena/robot.rb', line 15 def energy @energy end |
#gun_heading ⇒ Object
Returns the value of attribute gun_heading.
14 15 16 |
# File 'lib/ruby_arena/robot.rb', line 14 def gun_heading @gun_heading end |
#gun_heat ⇒ Object (readonly)
Returns the value of attribute gun_heat.
15 16 17 |
# File 'lib/ruby_arena/robot.rb', line 15 def gun_heat @gun_heat end |
#heading ⇒ Object
Returns the value of attribute heading.
14 15 16 |
# File 'lib/ruby_arena/robot.rb', line 14 def heading @heading end |
#radar_heading ⇒ Object
Returns the value of attribute radar_heading.
14 15 16 |
# File 'lib/ruby_arena/robot.rb', line 14 def radar_heading @radar_heading end |
#radar_view_angle ⇒ Object
Returns the value of attribute radar_view_angle.
16 17 18 |
# File 'lib/ruby_arena/robot.rb', line 16 def radar_view_angle @radar_view_angle end |
#robot ⇒ Object (readonly)
Returns the value of attribute robot.
14 15 16 |
# File 'lib/ruby_arena/robot.rb', line 14 def robot @robot end |
#speed ⇒ Object (readonly)
Returns the value of attribute speed.
14 15 16 |
# File 'lib/ruby_arena/robot.rb', line 14 def speed @speed end |
#tank ⇒ Object (readonly)
Returns the value of attribute tank.
13 14 15 |
# File 'lib/ruby_arena/robot.rb', line 13 def tank @tank end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
14 15 16 |
# File 'lib/ruby_arena/robot.rb', line 14 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
14 15 16 |
# File 'lib/ruby_arena/robot.rb', line 14 def y @y end |
Instance Method Details
#accelerate ⇒ Object
64 65 66 |
# File 'lib/ruby_arena/robot.rb', line 64 def accelerate @speed += 1 if speed < MAX_SPEED end |
#actions ⇒ Object
56 57 58 |
# File 'lib/ruby_arena/robot.rb', line 56 def actions command_parser.actions end |
#dead? ⇒ Boolean
103 104 105 |
# File 'lib/ruby_arena/robot.rb', line 103 def dead? energy < 0 end |
#decelerate ⇒ Object
68 69 70 |
# File 'lib/ruby_arena/robot.rb', line 68 def decelerate @speed -= 1 if speed > -MAX_SPEED end |
#execute_actions(actions) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# 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 ⇒ Object
90 91 92 93 94 95 96 97 |
# 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) ⇒ Object
99 100 101 |
# File 'lib/ruby_arena/robot.rb', line 99 def hit(bullet) @energy -= bullet.energy end |
#radar_range ⇒ Object
119 120 121 |
# File 'lib/ruby_arena/robot.rb', line 119 def radar_range RADAR_RANGE end |
#reset_actions ⇒ Object
60 61 62 |
# File 'lib/ruby_arena/robot.rb', line 60 def reset_actions command_parser.reset_actions end |
#scan ⇒ Object
107 108 109 110 111 112 113 |
# 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 ⇒ Object
115 116 117 |
# File 'lib/ruby_arena/robot.rb', line 115 def size SIZE end |
#tick ⇒ Object
34 35 36 37 |
# File 'lib/ruby_arena/robot.rb', line 34 def tick ai.tick(tick_events) @gun_heat -= 0.1 if @gun_heat > 0 end |
#time ⇒ Object
123 124 125 |
# File 'lib/ruby_arena/robot.rb', line 123 def time arena.time end |
#turn(angle) ⇒ Object
72 73 74 75 76 77 |
# 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) ⇒ Object
79 80 81 82 83 |
# 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) ⇒ Object
85 86 87 88 |
# 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 ⇒ Object
39 40 41 42 43 44 |
# File 'lib/ruby_arena/robot.rb', line 39 def update execute_actions(actions) reset_actions move keep_robot_in_arena end |