Class: TankRoamingState

Inherits:
TankMotionState show all
Defined in:
lib/entities/components/ai/tank_roaming_state.rb

Instance Method Summary collapse

Methods inherited from TankMotionState

#drive, #enter, #on_collision, #should_change_direction?, #substate_expired?, #wait, #waiting?

Constructor Details

#initialize(object, vision) ⇒ TankRoamingState

Returns a new instance of TankRoamingState.



2
3
4
5
6
# File 'lib/entities/components/ai/tank_roaming_state.rb', line 2

def initialize(object, vision)
  super
  @object = object
  @vision = vision
end

Instance Method Details

#change_directionObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/entities/components/ai/tank_roaming_state.rb', line 33

def change_direction
  closest_powerup = @vision.closest_powerup(
    *required_powerups)
  if closest_powerup
    @seeking_powerup = true
    angle = Utils.angle_between(
      @object.x, @object.y,
      closest_powerup.x, closest_powerup.y)
    @object.physics.change_direction(
      angle - angle % 45)
    # Go away from inaccessable powerup
    unless @vision.can_go_forward?
      @seeking_powerup = false
      @object.physics.change_direction(
        @object.direction + 180)
    end
  else
    @seeking_powerup = false
    change = case rand(0..100)
    when 0..30
      -45
    when 30..60
      45
    when 60..80
      90
    when 80..100
      -90
    end
    @object.physics.change_direction(
      @object.direction + change)
  end
  @changed_direction_at = Gosu.milliseconds
  @will_keep_direction_for = turn_time
end

#drive_timeObject



72
73
74
# File 'lib/entities/components/ai/tank_roaming_state.rb', line 72

def drive_time
  rand(1000..3000)
end

#required_powerupsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/entities/components/ai/tank_roaming_state.rb', line 15

def required_powerups
  required = []
  health = @object.health.health
  if @object.fire_rate_modifier < 2 && health > 50
    required << FireRatePowerup
  end
  if @object.speed_modifier < 1.5 && health > 50
    required << TankSpeedPowerup
  end
  if health < 100
    required << RepairPowerup
  end
  if health < 190
    required << HealthPowerup
  end
  required
end

#turn_timeObject



76
77
78
79
80
81
82
# File 'lib/entities/components/ai/tank_roaming_state.rb', line 76

def turn_time
  if @seeking_powerup
    rand(100..300)
  else
    rand(1000..3000)
  end
end

#updateObject



8
9
10
11
12
13
# File 'lib/entities/components/ai/tank_roaming_state.rb', line 8

def update
  change_direction if should_change_direction?
  if substate_expired?
    rand > 0.2 ? drive : wait
  end
end

#wait_timeObject



68
69
70
# File 'lib/entities/components/ai/tank_roaming_state.rb', line 68

def wait_time
  rand(50..400)
end