Class: Rubots::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/rubots/robot.rb

Constant Summary collapse

MAX_ANGLE =

Some constants

360
ANGLE_STEP =

Maximum angle. Wraps around to zero after this.

1
MAX_THROTTLE =

How much we change angle each tick.

10
THROTTLE_STEP =

Max throttle. Can’t go above.

1
SPEED_FACTOR =

How much we change throttle each tick.

1
MAX_GUN_ANGLE =

How much movement does each throttle step represent

360
GUN_ANGLE_STEP =

Maximum gun angle. Wraps around to zero after this.

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strategy_class, game, x, y) ⇒ Robot

Returns a new instance of Robot.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Attribute Details

#angleObject (readonly)

Returns the value of attribute angle.



5
6
7
# File 'lib/rubots/robot.rb', line 5

def angle
  @angle
end

#desired_angle=(value) ⇒ Object (writeonly)

Sets the attribute desired_angle

Parameters:

  • value

    the value to set the attribute desired_angle to.



6
7
8
# File 'lib/rubots/robot.rb', line 6

def desired_angle=(value)
  @desired_angle = value
end

#desired_gun_angle=(value) ⇒ Object (writeonly)

Sets the attribute desired_gun_angle

Parameters:

  • value

    the value to set the attribute desired_gun_angle to.



6
7
8
# File 'lib/rubots/robot.rb', line 6

def desired_gun_angle=(value)
  @desired_gun_angle = value
end

#desired_throttle=(value) ⇒ Object (writeonly)

Sets the attribute desired_throttle

Parameters:

  • value

    the value to set the attribute desired_throttle to.



6
7
8
# File 'lib/rubots/robot.rb', line 6

def desired_throttle=(value)
  @desired_throttle = value
end

#gun_angleObject (readonly)

Returns the value of attribute gun_angle.



5
6
7
# File 'lib/rubots/robot.rb', line 5

def gun_angle
  @gun_angle
end

#xObject (readonly)

Returns the value of attribute x.



5
6
7
# File 'lib/rubots/robot.rb', line 5

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



5
6
7
# File 'lib/rubots/robot.rb', line 5

def y
  @y
end

Instance Method Details

#destroyObject



72
73
74
75
# File 'lib/rubots/robot.rb', line 72

def destroy
  @throttle = 0
  @destroyed = true
end

#destroyed?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/rubots/robot.rb', line 77

def destroyed?
  @destroyed
end

#distance_to(other) ⇒ Object



66
67
68
69
70
# 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_fireObject



62
63
64
# File 'lib/rubots/robot.rb', line 62

def do_fire
  @firing = true
end

#nameObject



58
59
60
# File 'lib/rubots/robot.rb', line 58

def name
  @strategy.name
end

#process_commandObject



36
37
38
39
40
41
42
43
44
45
46
# 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

#tickObject



48
49
50
51
52
53
54
55
56
# File 'lib/rubots/robot.rb', line 48

def tick
  return if @destroyed

  tick_angle
  tick_throttle
  tick_movement
  tick_gun
  tick_fire
end