Class: Gamefic::Entity
- Inherits:
-
Object
- Object
- Gamefic::Entity
- Includes:
- Describable, Node
- Defined in:
- lib/gamefic/entity.rb
Overview
Entities are the people, places, and things that exist in a Gamefic narrative. Authors are encouraged to define Entity subclasses to create entity types that have additional features or need special handling in actions.
Direct Known Subclasses
Instance Attribute Summary
Attributes included from Node
Attributes included from Describable
#definite_article, #indefinite_article, #name, #synonyms
Class Method Summary collapse
-
.default_attributes ⇒ Hash
A hash of default attributes when creating an instance.
-
.set_default(**attrs) ⇒ Object
Set or update the default attributes for new instances.
Instance Method Summary collapse
-
#[](key) ⇒ Object
The value of the property.
- #[]=(key, value) ⇒ Object
-
#broadcast(message) ⇒ void
Tell a message to all of this entity’s accessible descendants.
-
#initialize(**args) {|_self| ... } ⇒ Entity
constructor
A new instance of Entity.
- #inspect ⇒ Object
-
#leave ⇒ void
Move this entity to its parent entity.
-
#post_initialize ⇒ Object
This method can be overridden for additional processing after the entity has been created.
-
#session ⇒ Hash
A freeform property dictionary.
Methods included from Node
#accessible?, #adjacent?, #children, #flatten, #include?, #take
Methods included from Describable
default_description, default_description=, #definitely, #described?, #description, #description=, #indefinitely, #keywords, #proper_named=, #proper_named?, #to_s
Constructor Details
#initialize(**args) {|_self| ... } ⇒ Entity
Returns a new instance of Entity.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gamefic/entity.rb', line 13 def initialize **args klass = self.class defaults = {} while klass <= Entity defaults = klass.default_attributes.merge(defaults) klass = klass.superclass end defaults.merge(args).each_pair { |k, v| send "#{k}=", v } yield(self) if block_given? post_initialize end |
Class Method Details
.default_attributes ⇒ Hash
A hash of default attributes when creating an instance.
94 95 96 |
# File 'lib/gamefic/entity.rb', line 94 def default_attributes @default_attributes ||= {} end |
.set_default(**attrs) ⇒ Object
Set or update the default attributes for new instances.
87 88 89 |
# File 'lib/gamefic/entity.rb', line 87 def set_default **attrs default_attributes.merge! attrs end |
Instance Method Details
#[](key) ⇒ Object
Returns The value of the property.
44 45 46 |
# File 'lib/gamefic/entity.rb', line 44 def [](key) session[key] end |
#[]=(key, value) ⇒ Object
50 51 52 |
# File 'lib/gamefic/entity.rb', line 50 def []=(key, value) session[key] = value end |
#broadcast(message) ⇒ void
This method returns an undefined value.
Tell a message to all of this entity’s accessible descendants.
78 79 80 81 82 |
# File 'lib/gamefic/entity.rb', line 78 def broadcast Query::Scoped.new(Scope::Descendants).select(self) .that_are(Active, proc(&:acting?)) .each { |actor| actor.tell } end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/gamefic/entity.rb', line 54 def inspect "#<#{self.class} #{name}>" end |
#leave ⇒ void
This method returns an undefined value.
Move this entity to its parent entity.
70 71 72 |
# File 'lib/gamefic/entity.rb', line 70 def leave self.parent = parent&.parent end |
#post_initialize ⇒ Object
This method can be overridden for additional processing after the entity has been created.
30 |
# File 'lib/gamefic/entity.rb', line 30 def post_initialize; end |
#session ⇒ Hash
A freeform property dictionary. Authors can use the session hash to assign custom properties to the entity. It can also be referenced directly using [] without the method name, e.g., entity.session or entity.
38 39 40 |
# File 'lib/gamefic/entity.rb', line 38 def session @session ||= {} end |