Sha256: bf28fd96c57f3133fb0a257e64df794c511282f9b9e1de40343bdf0c10c7853f

Contents?: true

Size: 1.08 KB

Versions: 19

Compression:

Stored size: 1.08 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 window
    raise DamageMissingError.new unless respond_to?(:damage)
    super window
  end
  
  module ClassMethods
    
    def damage damage = nil, &block
      block = lambda { damage } unless block_given?
      define_method :damage, &block
    end
    
  end
  
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
gosu_extensions-0.3.5 lib/traits/damaging.rb
gosu_extensions-0.3.4 lib/traits/damaging.rb
gosu_extensions-0.3.3 lib/traits/damaging.rb
gosu_extensions-0.3.2 lib/traits/damaging.rb
gosu_extensions-0.3.1 lib/traits/damaging.rb
gosu_extensions-0.3.0 lib/traits/damaging.rb
gosu_extensions-0.2.9 lib/traits/damaging.rb
gosu_extensions-0.2.8 lib/traits/damaging.rb
gosu_extensions-0.2.7 lib/traits/damaging.rb
gosu_extensions-0.2.6 lib/traits/damaging.rb
gosu_extensions-0.2.5 lib/traits/damaging.rb
gosu_extensions-0.2.4 lib/traits/damaging.rb
gosu_extensions-0.2.3 lib/traits/damaging.rb
gosu_extensions-0.2.2 lib/traits/damaging.rb
gosu_extensions-0.2.1 lib/traits/damaging.rb
gosu_extensions-0.2.0 lib/traits/damaging.rb
gosu_extensions-0.1.28 lib/traits/damaging.rb
gosu_extensions-0.1.27 lib/traits/damaging.rb
gosu_extensions-0.1.26 lib/traits/damaging.rb