Sha256: d844fe2a03ad59178d3e16bb02688c350582e51b432d898296f3465479370c77

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

# Use this for a thing that damages.
#
# Example:
#   class Rocket
#     it_is Damaging
#
#     damage { rand(10) + 5 }
#   end
#   
#   OR
#   class Rocket
#     it_is Damaging
#
#     damage 15
#   end
#   
#   OR
#   
#   class Rocket
#     it_is Damaging
#
#     def damage
#       # Define your own damage method.
#     end
#   end
#
module Damaging extend Trait
  
  class DamageMissingError < RuntimeError
    def initialize
      super <<-MESSAGE
        
        In a Damaging thing, you need to define method
          damage damage = nil, &block
        with params
          damage 13 # some value
        or
          damage { 13 + rand(7) } # some block
        to define how much damage the thing does.
        
      MESSAGE
    end
  end
  
  def self.included klass
    klass.extend ClassMethods
  end
  
  def initialize *args
    raise DamageMissingError.new unless respond_to?(:damage)
    super *args
  end
  
  module ClassMethods
    def damage damage = nil, &block
      block ||= lambda { damage }
      define_method :damage, &block
    end
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

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