Sha256: cbad4096ced3b08aa0f39eb7b775e3e37d1f0552b7a665b7b1d2dcf48c64c4c1

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

# A thing is destroyed if a number of lives has been passed.
#
module Lives extend Trait
  
  # 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
    target_class.send :include, InstanceMethods
  end
  
  module IncludeMethods
    
    # Define the amount of lives in the class.
    #
    def lives amount
      InitializerHooks.register self do
        self.lives = amount
      end
      attr_reader :lives # override the default
    end
    
  end
  
  module InstanceMethods
    
    attr_writer :lives
    
    def lives
      3
    end
    
    # Override to handle killed!
    #
    def killed!
      
    end
    
    # Does three things:
    # * Deduct 1 live.
    # * Check to see if the amount is 0.
    # * Calls #destroy! if yes.
    #
    def kill!
      self.lives -= 1
      killed! if self.lives > 0
      destroy! if self.lives == 0
    end
    
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gosu_extensions-0.2.9 lib/traits/lives.rb
gosu_extensions-0.2.8 lib/traits/lives.rb
gosu_extensions-0.2.7 lib/traits/lives.rb