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?, #children, #flatten, #has?

Methods included from Describable

default_description, default_description=, #definitely, #description, #description=, #description?, #indefinitely, #keywords, #proper_named=, #proper_named?, #to_s

Constructor Details

#initialize(**args) {|_self| ... } ⇒ Entity

include Messaging

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gamefic/entity.rb', line 14

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 values for attributes when creating an instance.

Returns:

  • (Hash)


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

def default_attributes
  @default_attributes ||= {}
end

.set_default(attrs = {}) ⇒ Object

Set or update the default values for new instances.

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes to be merged into the defaults.



63
64
65
# File 'lib/gamefic/entity.rb', line 63

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



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

def [](key)
  session[key]
end

#[]=(key, value) ⇒ Object

Parameters:

  • key (Symbol)

    The property’s name

  • value

    The value to set



51
52
53
# File 'lib/gamefic/entity.rb', line 51

def []=(key, value)
  session[key] = value
end

#inspectObject



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

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

#post_initializeObject

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



31
# File 'lib/gamefic/entity.rb', line 31

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)


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

def session
  @session ||= {}
end