Class: Health

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

Direct Known Subclasses

TankHealth

Instance Attribute Summary collapse

Attributes inherited from Component

#object

Instance Method Summary collapse

Constructor Details

#initialize(object, object_pool, health, explodes) ⇒ Health

Returns a new instance of Health.



4
5
6
7
8
9
10
# File 'lib/entities/components/health.rb', line 4

def initialize(object, object_pool, health, explodes)
  super(object)
  @explodes = explodes
  @object_pool = object_pool
  @initial_health = @health = health
  @health_updated = true
end

Instance Attribute Details

#healthObject

Returns the value of attribute health.



2
3
4
# File 'lib/entities/components/health.rb', line 2

def health
  @health
end

Instance Method Details

#damaged?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/entities/components/health.rb', line 22

def damaged?
  @health < @initial_health
end

#dead?Boolean

Returns:

  • (Boolean)


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

def dead?
  @health < 1
end

#draw(viewport) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/entities/components/health.rb', line 49

def draw(viewport)
  return unless draw?
  @image && @image.draw(
    x - @image.width / 2,
    y - object.graphics.height / 2 -
    @image.height, 100)
end

#increase(amount) ⇒ Object



17
18
19
20
# File 'lib/entities/components/health.rb', line 17

def increase(amount)
  @health = [@health + 25, @initial_health * 2].min
  @health_updated = true
end

#inflict_damage(amount, cause) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/entities/components/health.rb', line 34

def inflict_damage(amount, cause)
  if @health > 0
    @health_updated = true
    if object.respond_to?(:input)
      object.input.stats.add_damage(amount)
      # Don't count damage to trees and boxes
      if cause.respond_to?(:input) && cause != object
        cause.input.stats.add_damage_dealt(amount)
      end
    end
    @health = [@health - amount.to_i, 0].max
    after_death(cause) if dead?
  end
end

#restoreObject



12
13
14
15
# File 'lib/entities/components/health.rb', line 12

def restore
  @health = @initial_health
  @health_updated = true
end

#updateObject



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

def update
  update_image
end