class Peregrine::Component

Summary

Components serve as data storage for Entity objects. Component objects, by definition, should not contain any logic – they serve only as a way to store information or as a “flag” for an Entity to be used by a System to implement the actual logic.

Usage

It is expected that developers will subclass the Component class in order to create their own individual Components. When subclassing the Component, it is important to note that you should not overwrite the #initialize method, and instead overwite the #initialize_data method in order to instantiate a Component. Any additional arguments given to Component.new are passed along to the #initialize_data method.

Example

class Mortal < Peregrine::Component
  attr_reader :health

  # Initializing the data for this Component object.
  def initialize_data(health = 100, max_health = 100)
    @health  = health
    @maximum = max_health
  end

  # Do not implement logic, but define limits for data storage.
  def health=(value)
    @health = [0, value, @maximum].sort[1]
  end
end

Public Class Methods

new(*data_args) { |self| ... } click to toggle source

Create a new Component instance. Any arguments given to this method are passed to the initialize_data method. Yields the newly instanced Component if a block is given.

# File lib/peregrine/component.rb, line 42
def initialize(*data_args)
  initialize_data(*data_args)
  yield self if block_given?
end

Public Instance Methods

initialize_data() click to toggle source

Intended to be overwritten by subclasses of the Component to initialize the data used. Additional arguments passed to Component.new are passed to this method directly. This method does nothing on its own.

# File lib/peregrine/component.rb, line 50
def initialize_data
end
inspect()
Alias for: to_s
to_s() click to toggle source

Presents a human-readable summary of the Component.

# File lib/peregrine/component.rb, line 54
def to_s
  "Component '#{name}' #{id}"
end
Also aliased as: inspect