class Demiurge::StateItem

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.

Attributes

engine[R]

@return [Demiurge::Engine] The Engine this item is part of

name[R]

@return [String] The unique, registered or registerable name of this {Demiurge::StateItem} @since 0.0.1

Public Class Methods

from_name_type(engine, type, name, state) click to toggle source

Create a single StateItem from structured array format

@see Demiurge::StateItem @return [Demiurge::StateItem] The deserialized StateItem @since 0.0.1

# File lib/demiurge.rb, line 799
def self.from_name_type(engine, type, name, state)
  engine.get_type(type).new(name, engine, state)
end
new(name, engine, state) click to toggle source

The constructor. This does not register the StateItem with the Engine. For that, see {Demiurge::Engine#register_state_item}.

@param name [String] The Engine-unique item name @param engine [Demiurge::Engine] The Engine this item is part of @param state [Hash] State data to initialize from @see Demiurge::Engine#register_state_item @return [void] @since 0.0.1

# File lib/demiurge.rb, line 746
def initialize(name, engine, state)
  @name = name
  @engine = engine
  @state = state
end

Public Instance Methods

agent?() click to toggle source

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.

@return [Boolean] Whether this item is considered an Agent. @since 0.0.1

# File lib/demiurge.rb, line 772
def agent?
  false
end
get_structure() click to toggle source

The StateItem's serialized state in structured array format.

@see Demiurge::StateItem @return [Array] The serialized state @since 0.0.1

# File lib/demiurge.rb, line 790
def get_structure()
  [state_type, @name, @state]
end
intentions_for_next_step(*args) click to toggle source

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.

@return [Array<Demiurge::Intention>] The intentions for the next tick @since 0.0.1

# File lib/demiurge.rb, line 810
def intentions_for_next_step(*args)
  raise "StateItem must be subclassed to be used!"
end
state() click to toggle source

Return this StateItem's current state in a JSON-serializable form.

@return [Object] The JSON-serializable state, usually a Hash @since 0.0.1

# File lib/demiurge.rb, line 781
def state
  @state
end
state_type() click to toggle source

@return [String] The default StateItem type of this item. Can be overridden by child classes. @since 0.0.1

# File lib/demiurge.rb, line 733
def state_type
  self.class.name.split("::")[-1]
end
zone?() click to toggle source

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.

@return [Boolean] Whether this item is considered a Zone. @since 0.0.1

# File lib/demiurge.rb, line 760
def zone?
  false
end