Class: AiInput

Inherits:
Component show all
Defined in:
lib/entities/components/ai_input.rb

Constant Summary collapse

NAME_COLOR =

Dark red

Gosu::Color.argb(0xeeb10000)
UPDATE_RATE =

ms

10

Instance Attribute Summary collapse

Attributes inherited from Component

#object

Instance Method Summary collapse

Constructor Details

#initialize(name, object_pool) ⇒ AiInput

Returns a new instance of AiInput.



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

def initialize(name, object_pool)
  super(nil)
  @object_pool = object_pool
  @stats = Stats.new(name)
  @name = name
  @last_update = Gosu.milliseconds
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/entities/components/ai_input.rb', line 5

def name
  @name
end

#statsObject (readonly)

Returns the value of attribute stats.



6
7
8
# File 'lib/entities/components/ai_input.rb', line 6

def stats
  @stats
end

Instance Method Details

#control(obj) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/entities/components/ai_input.rb', line 16

def control(obj)
  self.object = obj
  object.components << self
  @vision = AiVision.new(obj, @object_pool,
                         rand(700..1200))
  @gun = AiGun.new(obj, @vision)
  @motion = TankMotionFSM.new(obj, @vision, @gun)
end

#draw(viewport) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/entities/components/ai_input.rb', line 46

def draw(viewport)
  @motion.draw(viewport)
  @gun.draw(viewport)
  @name_image ||= Gosu::Image.from_text(
    $window, @name, Gosu.default_font_name, 20)
  @name_image.draw(
    x - @name_image.width / 2 - 1,
    y + object.graphics.height / 2, 100,
    1, 1, Gosu::Color::WHITE)
  @name_image.draw(
    x - @name_image.width / 2,
    y + object.graphics.height / 2, 100,
    1, 1, NAME_COLOR)
end

#on_collision(with) ⇒ Object



25
26
27
28
# File 'lib/entities/components/ai_input.rb', line 25

def on_collision(with)
  return if object.health.dead?
  @motion.on_collision(with)
end

#on_damage(amount) ⇒ Object



30
31
32
33
# File 'lib/entities/components/ai_input.rb', line 30

def on_damage(amount)
  @motion.on_damage(amount)
  @stats.add_damage(amount)
end

#updateObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/entities/components/ai_input.rb', line 35

def update
  return respawn if object.health.dead?
  @gun.adjust_angle
  now = Gosu.milliseconds
  return if now - @last_update < UPDATE_RATE
  @last_update = now
  @vision.update
  @gun.update
  @motion.update
end