Sha256: 181296ff414740164c1d3d8409b776305977ef3f1d8acb3f61b6a60957a92698

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

# When hitpoints are at zero or lower, it calls kill! if available, destroy! else.
#
module Hitpoints extend Trait
  
  # Prints an amount of information on these capabilities.
  #
  manual <<-MANUAL
    Defines:
      hitpoints <some number>
      hitpoints { <some dynamically generated number> }
      
    Example:
      hitpoints 10_000
      hitpoints { rand(100) + 50 }
    
    Call hit!(damage = 1) to remove hitpoints. This will call
    * callback hit if hitpoints are still higher than 0.
    * kill!, and if not available, destroy! if hitpoints are equal or lower than 0.
  MANUAL
  
  def self.included target_class
    target_class.extend ClassMethods
  end
  
  module ClassMethods
    
    # Define the amount of hitpoints of the thing.
    #
    def hitpoints amount = nil, &block
      include InstanceMethods
      class_inheritable_accessor :prototype_hitpoints
      self.prototype_hitpoints = amount
      
      InitializerHooks.register self do
        self.hitpoints = amount || block.call
      end
      
    end
    
  end
  
  module InstanceMethods
    
    attr_accessor :hitpoints
    
    # Override to handle hit.
    #
    def hit; end
    
    # Hit the thing with that much damage.
    #
    # hit!-s if hitpoints higher than 0
    # kill!-s if lower, or destroy!-s if kill!
    # is not available.
    #
    def hit! damage = 1
      self.hitpoints -= damage
      hit if hitpoints > 0
      respond_to?(:kill!) ? kill! : destroy! if hitpoints < 0
    end
    
    # kill! must reset hitpoints
    #
    # TODO Do irrespective of kill! call order
    #
    def kill!
      super
      self.hitpoints = self.class.prototype_hitpoints
    end
    
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gosu_extensions-0.3.8 lib/traits/hitpoints.rb
gosu_extensions-0.3.7 lib/traits/hitpoints.rb
gosu_extensions-0.3.6 lib/traits/hitpoints.rb