Class: Draco::Entity
- Inherits:
-
Object
- Object
- Draco::Entity
- Defined in:
- lib/draco.rb
Overview
Public: A general purpose game object that consists of a unique id and a collection of Components.
Defined Under Namespace
Classes: ComponentStore
Constant Summary collapse
- @@next_id =
1
Class Attribute Summary collapse
-
.default_components ⇒ Object
readonly
Internal: Returns the default components for the class.
Instance Attribute Summary collapse
-
#components ⇒ Object
readonly
Public: Returns the Array of the Entity’s components.
-
#id ⇒ Object
readonly
Public: Returns the Integer id of the Entity.
Class Method Summary collapse
-
.component(component, defaults = {}) ⇒ Object
Public: Adds a default component to the Entity.
-
.inherited(sub) ⇒ Object
Internal: Resets the default components for each class that inherites Entity.
-
.Tag(name) ⇒ Object
Public: Creates a tag Component.
Instance Method Summary collapse
-
#after_component_added(component) ⇒ Object
Public: Callback run after a component is added.
-
#after_component_removed(component) ⇒ Object
Public: Callback run after a component is deleted.
-
#after_initialize ⇒ Object
Public: Callback run after the entity is initialized.
-
#before_component_added(component) ⇒ Object
Public: Callback run before a component is added.
-
#before_component_removed(component) ⇒ Object
Public: Callback run before a component is deleted.
-
#initialize(args = {}) ⇒ Entity
constructor
Public: Initialize a new Entity.
-
#inspect ⇒ Object
Public: Returns a String representation of the Entity.
-
#method_missing(method, *args, &block) ⇒ Object
Signature.
- #respond_to_missing?(method, _include_private = false) ⇒ Boolean
-
#serialize ⇒ Object
Public: Serializes the Entity to save the current state.
-
#setup_components(args) ⇒ Object
Internal: Sets up the default components for the class.
-
#subscribe(subscriber) ⇒ Object
Public: Subscribe to an Entity’s Component updates.
-
#to_s ⇒ Object
Public: Returns a String representation of the Entity.
Constructor Details
#initialize(args = {}) ⇒ Entity
75 76 77 78 79 80 81 82 |
# File 'lib/draco.rb', line 75 def initialize(args = {}) @id = args.fetch(:id, @@next_id) @@next_id = [@id + 1, @@next_id].max @subscriptions = [] setup_components(args) after_initialize end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Signature
<underscored_component_name>
underscored_component_name - The component to access the data from
Public: Get the component associated with this Entity. This method will be available for each component.
Examples
class Creature < Draco::Entity
component CreatureStats, strength: 10
end
creature = Creature.new
creature.creature_stats
Returns the Component instance.
195 196 197 198 199 200 |
# File 'lib/draco.rb', line 195 def method_missing(method, *args, &block) component = components[method.to_sym] return component if component super end |
Class Attribute Details
.default_components ⇒ Object (readonly)
Internal: Returns the default components for the class.
55 56 57 |
# File 'lib/draco.rb', line 55 def default_components @default_components end |
Instance Attribute Details
#components ⇒ Object (readonly)
Public: Returns the Array of the Entity’s components
62 63 64 |
# File 'lib/draco.rb', line 62 def components @components end |
#id ⇒ Object (readonly)
Public: Returns the Integer id of the Entity.
59 60 61 |
# File 'lib/draco.rb', line 59 def id @id end |
Class Method Details
.component(component, defaults = {}) ⇒ Object
Public: Adds a default component to the Entity.
component - The class of the Component to add by default. defaults - The Hash of default values for the Component data. (default: {})
Examples
component(Visible)
component(Position, x: 0, y: 0)
Returns nothing.
40 41 42 |
# File 'lib/draco.rb', line 40 def self.component(component, defaults = {}) @default_components[component] = defaults end |
.inherited(sub) ⇒ Object
Internal: Resets the default components for each class that inherites Entity.
sub - The class that is inheriting Entity.
Returns nothing.
23 24 25 26 |
# File 'lib/draco.rb', line 23 def self.inherited(sub) super sub.instance_variable_set(:@default_components, {}) end |
.Tag(name) ⇒ Object
Public: Creates a tag Component. If the tag already exists, return it.
name - The string or symbol name of the component.
Returns a class with subclass Draco::Component.
49 50 51 |
# File 'lib/draco.rb', line 49 def self.Tag(name) # rubocop:disable Naming/MethodName Draco::Tag(name) end |
Instance Method Details
#after_component_added(component) ⇒ Object
Public: Callback run after a component is added.
component - The component that was added.
Returns the added component.
128 129 130 131 |
# File 'lib/draco.rb', line 128 def after_component_added(component) @subscriptions.each { |sub| sub.component_added(self, component) } component end |
#after_component_removed(component) ⇒ Object
Public: Callback run after a component is deleted.
component - The component that was removed.
Returns the removed component.
147 148 149 150 |
# File 'lib/draco.rb', line 147 def after_component_removed(component) @subscriptions.each { |sub| sub.component_removed(self, component) } component end |
#after_initialize ⇒ Object
Public: Callback run after the entity is initialized.
This is empty by default but is present to allow plugins to tie into.
Returns nothing.
103 |
# File 'lib/draco.rb', line 103 def after_initialize; end |
#before_component_added(component) ⇒ Object
Public: Callback run before a component is added.
component - The component that will be added.
Returns the component to add.
119 120 121 |
# File 'lib/draco.rb', line 119 def before_component_added(component) component end |
#before_component_removed(component) ⇒ Object
Public: Callback run before a component is deleted.
component - The component that will be removed.
Returns the component to remove.
138 139 140 |
# File 'lib/draco.rb', line 138 def before_component_removed(component) component end |
#inspect ⇒ Object
Public: Returns a String representation of the Entity.
166 167 168 |
# File 'lib/draco.rb', line 166 def inspect serialize.to_s end |
#respond_to_missing?(method, _include_private = false) ⇒ Boolean
202 203 204 |
# File 'lib/draco.rb', line 202 def respond_to_missing?(method, _include_private = false) !!components[method.to_sym] or super end |
#serialize ⇒ Object
Public: Serializes the Entity to save the current state.
Returns a Hash representing the Entity.
155 156 157 158 159 160 161 162 163 |
# File 'lib/draco.rb', line 155 def serialize serialized = { class: self.class.name.to_s, id: id } components.each do |component| serialized[Draco.underscore(component.class.name.to_s).to_sym] = component.serialize end serialized end |
#setup_components(args) ⇒ Object
Internal: Sets up the default components for the class.
args - A hash of arguments to pass into the generated components.
Returns nothing.
89 90 91 92 93 94 95 96 |
# File 'lib/draco.rb', line 89 def setup_components(args) @components = ComponentStore.new(self) self.class.default_components.each do |component, default_args| arguments = default_args.merge(args[Draco.underscore(component.name.to_s).to_sym] || {}) @components << component.new(arguments) end end |
#subscribe(subscriber) ⇒ Object
Public: Subscribe to an Entity’s Component updates.
subscriber - The object to notify when Components change.
Returns nothing.
110 111 112 |
# File 'lib/draco.rb', line 110 def subscribe(subscriber) @subscriptions << subscriber end |
#to_s ⇒ Object
Public: Returns a String representation of the Entity.
171 172 173 |
# File 'lib/draco.rb', line 171 def to_s serialize.to_s end |