Sha256: b340be7145c1690d53d018f155bf0804e52110f513a3fd5df1160555e877d2cd

Contents?: true

Size: 1.46 KB

Versions: 9

Compression:

Stored size: 1.46 KB

Contents

# When hitpoints are at zero or lower, it calls kill! if available, destroy! else.
#
module Hitpoints extend Trait
  
  # TODO def revive!
  #
  
  # 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
      if self.hitpoints > 0
        hit!
      else
        if respond_to?(:kill!)
          kill!
        else
          destroy!
        end
      end
    end
    
  end
  
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gosu_extensions-0.2.5 lib/traits/hitpoints.rb
gosu_extensions-0.2.4 lib/traits/hitpoints.rb
gosu_extensions-0.2.3 lib/traits/hitpoints.rb
gosu_extensions-0.2.2 lib/traits/hitpoints.rb
gosu_extensions-0.2.1 lib/traits/hitpoints.rb
gosu_extensions-0.2.0 lib/traits/hitpoints.rb
gosu_extensions-0.1.28 lib/traits/hitpoints.rb
gosu_extensions-0.1.27 lib/traits/hitpoints.rb
gosu_extensions-0.1.26 lib/traits/hitpoints.rb