Class: RubyArena::Robot

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Movable

#move

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

#aiObject (readonly)

Returns the value of attribute ai.



13
14
15
# File 'lib/ruby_arena/robot.rb', line 13

def ai
  @ai
end

#arenaObject (readonly)

Returns the value of attribute arena.



13
14
15
# File 'lib/ruby_arena/robot.rb', line 13

def arena
  @arena
end

#command_parserObject (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

#energyObject (readonly)

Returns the value of attribute energy.



15
16
17
# File 'lib/ruby_arena/robot.rb', line 15

def energy
  @energy
end

#gun_headingObject

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_heatObject (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

#headingObject

Returns the value of attribute heading.



14
15
16
# File 'lib/ruby_arena/robot.rb', line 14

def heading
  @heading
end

#radar_headingObject

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_angleObject

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

#robotObject (readonly)

Returns the value of attribute robot.



14
15
16
# File 'lib/ruby_arena/robot.rb', line 14

def robot
  @robot
end

#speedObject (readonly)

Returns the value of attribute speed.



14
15
16
# File 'lib/ruby_arena/robot.rb', line 14

def speed
  @speed
end

#tankObject (readonly)

Returns the value of attribute tank.



13
14
15
# File 'lib/ruby_arena/robot.rb', line 13

def tank
  @tank
end

#xObject (readonly)

Returns the value of attribute x.



14
15
16
# File 'lib/ruby_arena/robot.rb', line 14

def x
  @x
end

#yObject (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

#accelerateObject



64
65
66
# File 'lib/ruby_arena/robot.rb', line 64

def accelerate
  @speed += 1 if speed < MAX_SPEED
end

#actionsObject



56
57
58
# File 'lib/ruby_arena/robot.rb', line 56

def actions
  command_parser.actions
end

#dead?Boolean

Returns:



103
104
105
# File 'lib/ruby_arena/robot.rb', line 103

def dead?
  energy < 0
end

#decelerateObject



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

#fireObject



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_rangeObject



119
120
121
# File 'lib/ruby_arena/robot.rb', line 119

def radar_range
  RADAR_RANGE
end

#reset_actionsObject



60
61
62
# File 'lib/ruby_arena/robot.rb', line 60

def reset_actions
  command_parser.reset_actions
end

#scanObject



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

#sizeObject



115
116
117
# File 'lib/ruby_arena/robot.rb', line 115

def size
  SIZE
end

#tickObject



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

#timeObject



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

#updateObject



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