Class: Gamefic::Entity

Inherits:
Object
  • Object
show all
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

Actor

Instance Attribute Summary

Attributes included from Node

#parent

Attributes included from Describable

#definite_article, #indefinite_article, #name, #synonyms

Class Method Summary collapse

Instance Method Summary collapse

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.

Yields:

  • (_self)

Yield Parameters:



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_attributesHash

A hash of default attributes when creating an instance.

Returns:

  • (Hash)


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.

Parameters:

  • key (Symbol)

    The property’s name

Returns:

  • The value of the property



44
45
46
# File 'lib/gamefic/entity.rb', line 44

def [](key)
  session[key]
end

#[]=(key, value) ⇒ Object

Parameters:

  • key (Symbol)

    The property’s name

  • value

    The value to set



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.

Parameters:



78
79
80
81
82
# 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

#inspectObject



54
55
56
# File 'lib/gamefic/entity.rb', line 54

def inspect
  "#<#{self.class} #{name}>"
end

#leavevoid

This method returns an undefined value.

Move this entity to its parent entity.

Examples:

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


70
71
72
# File 'lib/gamefic/entity.rb', line 70

def leave
  self.parent = parent&.parent
end

#post_initializeObject

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

#sessionHash

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.

Returns:

  • (Hash)


38
39
40
# File 'lib/gamefic/entity.rb', line 38

def session
  @session ||= {}
end