class Gamefic::Entity

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.

Public Class Methods

default_attributes() click to toggle source

A hash of default attributes when creating an instance.

@return [Hash]

# File lib/gamefic/entity.rb, line 94
def default_attributes
  @default_attributes ||= {}
end
new(**args) { |self| ... } click to toggle source
# 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
set_default(**attrs) click to toggle source

Set or update the default attributes for new instances.

# File lib/gamefic/entity.rb, line 87
def set_default **attrs
  default_attributes.merge! attrs
end

Public Instance Methods

[](key) click to toggle source

@param key [Symbol] The property’s name @return The value of the property

# File lib/gamefic/entity.rb, line 44
def [](key)
  session[key]
end
[]=(key, value) click to toggle source

@param key [Symbol] The property’s name @param value The value to set

# File lib/gamefic/entity.rb, line 50
def []=(key, value)
  session[key] = value
end
broadcast(message) click to toggle source

Tell a message to all of this entity’s accessible descendants.

@param message [String] @return [void]

# File lib/gamefic/entity.rb, line 78
def broadcast message
  Query::Scoped.new(Scope::Descendants).select(self)
                                       .that_are(Active, proc(&:acting?))
                                       .each { |actor| actor.tell message }
end
inspect() click to toggle source
# File lib/gamefic/entity.rb, line 54
def inspect
  "#<#{self.class} #{name}>"
end
leave() click to toggle source

Move this entity to its parent entity.

@example

room =   Gamefic::Entity.new(name: 'room')
person = Gamefic::Entity.new(name: 'person', parent: room)
thing =  Gamefic::Entity.new(name: 'thing', parent: person)

thing.parent #=> person
thing.leave
thing.parent #=> room

@return [void]

# File lib/gamefic/entity.rb, line 70
def leave
  self.parent = parent&.parent
end
post_initialize() click to toggle source

This method can be overridden for additional processing after the entity has been created.

# File lib/gamefic/entity.rb, line 30
def post_initialize; end
session() click to toggle source

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.

@return [Hash]

# File lib/gamefic/entity.rb, line 38
def session
  @session ||= {}
end