class Health

Attributes

health[RW]

Public Class Methods

new(object, object_pool, health, explodes) click to toggle source
Calls superclass method Component::new
# 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

Public Instance Methods

damaged?() click to toggle source
# File lib/entities/components/health.rb, line 22
def damaged?
  @health < @initial_health
end
dead?() click to toggle source
# File lib/entities/components/health.rb, line 26
def dead?
  @health < 1
end
draw(viewport) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/entities/components/health.rb, line 12
def restore
  @health = @initial_health
  @health_updated = true
end
update() click to toggle source
# File lib/entities/components/health.rb, line 30
def update
  update_image
end

Protected Instance Methods

after_death(cause) click to toggle source
# File lib/entities/components/health.rb, line 75
def after_death(cause)
  if @explodes
    Thread.new do
      sleep(rand(0.1..0.3))
      Explosion.new(@object_pool, x, y, cause)
      sleep 0.3
      object.mark_for_removal
    end
  else
    object.mark_for_removal
  end
end
draw?() click to toggle source
# File lib/entities/components/health.rb, line 59
def draw?
  $debug
end
update_image() click to toggle source
# File lib/entities/components/health.rb, line 63
def update_image
  return unless draw?
  if @health_updated
    text = @health.to_s
    font_size = 18
    @image = Gosu::Image.from_text(
        $window, text,
        Gosu.default_font_name, font_size)
    @health_updated = false
  end
end