Class: Health
Direct Known Subclasses
Instance Attribute Summary collapse
-
#health ⇒ Object
Returns the value of attribute health.
Attributes inherited from Component
Instance Method Summary collapse
- #damaged? ⇒ Boolean
- #dead? ⇒ Boolean
- #draw(viewport) ⇒ Object
- #increase(amount) ⇒ Object
- #inflict_damage(amount, cause) ⇒ Object
-
#initialize(object, object_pool, health, explodes) ⇒ Health
constructor
A new instance of Health.
- #restore ⇒ Object
- #update ⇒ Object
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
#health ⇒ Object
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
22 23 24 |
# File 'lib/entities/components/health.rb', line 22 def damaged? @health < @initial_health end |
#dead? ⇒ 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() 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 |
#restore ⇒ Object
12 13 14 15 |
# File 'lib/entities/components/health.rb', line 12 def restore @health = @initial_health @health_updated = true end |
#update ⇒ Object
30 31 32 |
# File 'lib/entities/components/health.rb', line 30 def update update_image end |