Sha256: 94b72a76d80db2a02e721b26acfe78d8640d7a14ee78887fb005f73d8442d8c1

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 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 trait>
    
    Example:
      hitpoints 10_000
    
    Call hit(damage = 1) to remove hitpoints. This will call
    * hit! if hitpoints are still higher than 0.
    * kill!, and if not available, destroy! if hitpoints are 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
      include InstanceMethods
      class_inheritable_accessor :prototype_hitpoints
      self.prototype_hitpoints = amount
      
      hook = lambda { self.hitpoints = self.class.prototype_hitpoints }
      InitializerHooks.register self, &hook
    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
    
  end
  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gosu_extensions-0.3.2 lib/traits/hitpoints.rb
gosu_extensions-0.3.1 lib/traits/hitpoints.rb
gosu_extensions-0.3.0 lib/traits/hitpoints.rb
gosu_extensions-0.2.9 lib/traits/hitpoints.rb
gosu_extensions-0.2.8 lib/traits/hitpoints.rb
gosu_extensions-0.2.7 lib/traits/hitpoints.rb