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
Public Instance Methods
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