Sha256: ad7c9d7d5c3a1c0fa8c32999a73c556eec68379e647d9be222fea6b6d4c57134

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

# A thing is destroyed if a number of lives has been passed.
#
module Lives
  
  # TODO def revive!
  #
  
  # Prints an amount of information on these capabilities.
  #
  manual <<-MANUAL
    Defines:
      lives <some trait>
    
    Example:
      lives 10
    
    Call kill! to remove a live. Override killed! to exhibit behaviour.
  MANUAL
  
  def self.included target_class
    target_class.extend IncludeMethods
  end
  
  module IncludeMethods
    
    # Define the amount of lives in the class.
    #
    def lives amount
      include InstanceMethods
      class_inheritable_accessor :prototype_lives
      self.prototype_lives = amount
      
      hook = lambda { self.lives = self.class.prototype_lives }
      InitializerHooks.register self, &hook
    end
    
  end
  
  module InstanceMethods
    
    attr_accessor :lives
    
    # Does three things:
    # * Deduct 1 live.
    # * Check to see if the amount is 0.
    # * Calls #destroy! if yes.
    #
    def killed!
      
    end
    def kill!
      self.lives -= 1
      killed! if self.lives > 0
      destroy! if self.lives == 0
    end
    
  end
  
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gosu_extensions-0.1.14 lib/traits/lives.rb
gosu_extensions-0.1.13 lib/traits/lives.rb
gosu_extensions-0.1.12 lib/traits/lives.rb
gosu_extensions-0.1.11 lib/traits/lives.rb
gosu_extensions-0.1.10 lib/traits/lives.rb
gosu_extensions-0.1.9 lib/traits/lives.rb
gosu_extensions-0.1.8 lib/traits/lives.rb
gosu_extensions-0.1.7 lib/traits/lives.rb
gosu_extensions-0.1.6 lib/traits/lives.rb