Class: Demiurge::StateItem
- Inherits:
-
Object
- Object
- Demiurge::StateItem
- Defined in:
- lib/demiurge.rb,
lib/demiurge.rb
Overview
StateItems can be serialized at any time to "structured array" format. That format consists of a set of Plain Old Ruby Objects (POROs) which are guaranteed to be serializable as JSON, and thus consist of only basic data structures like Strings, Arrays, Booleans and Hashes. A single StateItem will serialize itself to a short Array of this form: ["ObjectType", "item name", state_hash]. The ObjectType is the type registered with the engine, such as "ActionItem". The "item name" is the object-unique item name. And the state_hash is a JSON-serializable Ruby Hash with the object's current state. A dump of multiple StateItems will be an Array of these Arrays.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#engine ⇒ Demiurge::Engine
readonly
The Engine this item is part of.
-
#name ⇒ String
readonly
The unique, registered or registerable name of this StateItem.
Class Method Summary collapse
-
.from_name_type(engine, type, name, state) ⇒ Demiurge::StateItem
Create a single StateItem from structured array format.
Instance Method Summary collapse
-
#agent? ⇒ Boolean
This method determines whether the item will be treated as an agent.
-
#get_structure ⇒ Array
The StateItem's serialized state in structured array format.
-
#initialize(name, engine, state) ⇒ void
constructor
The constructor.
-
#intentions_for_next_step(*args) ⇒ Array<Demiurge::Intention>
Get this StateItem's Intentions for the upcoming tick, with its current internal state.
-
#state ⇒ Object
Return this StateItem's current state in a JSON-serializable form.
-
#state_type ⇒ String
The default StateItem type of this item.
-
#zone? ⇒ Boolean
This method determines whether the item will be treated as a top-level zone.
Constructor Details
#initialize(name, engine, state) ⇒ void
The constructor. This does not register the StateItem with the Engine. For that, see Engine#register_state_item.
751 752 753 754 755 |
# File 'lib/demiurge.rb', line 751 def initialize(name, engine, state) @name = name @engine = engine @state = state end |
Instance Attribute Details
#engine ⇒ Demiurge::Engine (readonly)
Returns The Engine this item is part of
734 735 736 |
# File 'lib/demiurge.rb', line 734 def engine @engine end |
#name ⇒ String (readonly)
Returns The unique, registered or registerable name of this Demiurge::StateItem
731 732 733 |
# File 'lib/demiurge.rb', line 731 def name @name end |
Class Method Details
.from_name_type(engine, type, name, state) ⇒ Demiurge::StateItem
Create a single StateItem from structured array format
804 805 806 |
# File 'lib/demiurge.rb', line 804 def self.from_name_type(engine, type, name, state) engine.get_type(type).new(name, engine, state) end |
Instance Method Details
#agent? ⇒ Boolean
This method determines whether the item will be treated as an agent. Inheriting from Demiurge::Agent will cause that to occur. So will redefining the agent? method to return true. Whether agent? returns true should not depend on state, which may not be set when this method is called.
777 778 779 |
# File 'lib/demiurge.rb', line 777 def agent? false end |
#get_structure ⇒ Array
The StateItem's serialized state in structured array format.
795 796 797 |
# File 'lib/demiurge.rb', line 795 def get_structure() [state_type, @name, @state] end |
#intentions_for_next_step(*args) ⇒ Array<Demiurge::Intention>
Get this StateItem's Intentions for the upcoming tick, with its current internal state. Note that this can change over the course of a tick as the internal state changes, but it should not change if the state doesn't.
815 816 817 |
# File 'lib/demiurge.rb', line 815 def intentions_for_next_step(*args) raise "StateItem must be subclassed to be used!" end |
#state ⇒ Object
Return this StateItem's current state in a JSON-serializable form.
786 787 788 |
# File 'lib/demiurge.rb', line 786 def state @state end |
#state_type ⇒ String
Returns The default StateItem type of this item. Can be overridden by child classes.
738 739 740 |
# File 'lib/demiurge.rb', line 738 def state_type self.class.name.split("::")[-1] end |
#zone? ⇒ Boolean
This method determines whether the item will be treated as a top-level zone. Inheriting from Demiurge::Zone will cause that to occur. So will redefining the zone? method to return true. Whether zone? returns true should not depend on state, which may not be set when this method is called.
765 766 767 |
# File 'lib/demiurge.rb', line 765 def zone? false end |