Sha256: 68f1cc6583f3c99f086505fbec020df5afc585ba34c10dc976d2f2016a36e68c

Contents?: true

Size: 1.07 KB

Versions: 11

Compression:

Stored size: 1.07 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 self.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

11 entries across 11 versions & 1 rubygems

Version Path
gosu_extensions-0.1.25 lib/traits/damaging.rb
gosu_extensions-0.1.24 lib/traits/damaging.rb
gosu_extensions-0.1.23 lib/traits/damaging.rb
gosu_extensions-0.1.22 lib/traits/damaging.rb
gosu_extensions-0.1.21 lib/traits/damaging.rb
gosu_extensions-0.1.20 lib/traits/damaging.rb
gosu_extensions-0.1.19 lib/traits/damaging.rb
gosu_extensions-0.1.18 lib/traits/damaging.rb
gosu_extensions-0.1.17 lib/traits/damaging.rb
gosu_extensions-0.1.16 lib/traits/damaging.rb
gosu_extensions-0.1.15 lib/traits/damaging.rb