Class: Demiurge::StateItem

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

ActionItem, InertStateItem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • name (String)

    The Engine-unique item name

  • engine (Demiurge::Engine)

    The Engine this item is part of

  • state (Hash)

    State data to initialize from

See Also:

Since:

  • 0.0.1



744
745
746
747
748
# File 'lib/demiurge.rb', line 744

def initialize(name, engine, state)
  @name = name
  @engine = engine
  @state = state
end

Instance Attribute Details

#engineDemiurge::Engine (readonly)

Returns The Engine this item is part of.

Returns:



727
728
729
# File 'lib/demiurge.rb', line 727

def engine
  @engine
end

#nameString (readonly)

Returns The unique, registered or registerable name of this Demiurge::StateItem.

Returns:

Since:

  • 0.0.1



724
725
726
# File 'lib/demiurge.rb', line 724

def name
  @name
end

Class Method Details

.from_name_type(engine, type, name, state) ⇒ Demiurge::StateItem

Create a single StateItem from structured array format

Returns:

See Also:

Since:

  • 0.0.1



797
798
799
# File 'lib/demiurge.rb', line 797

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.

Returns:

  • (Boolean)

    Whether this item is considered an Agent.

Since:

  • 0.0.1



770
771
772
# File 'lib/demiurge.rb', line 770

def agent?
  false
end

#get_structureArray

The StateItem's serialized state in structured array format.

Returns:

  • (Array)

    The serialized state

See Also:

Since:

  • 0.0.1



788
789
790
# File 'lib/demiurge.rb', line 788

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.

Returns:

Since:

  • 0.0.1



808
809
810
# File 'lib/demiurge.rb', line 808

def intentions_for_next_step(*args)
  raise "StateItem must be subclassed to be used!"
end

#stateObject

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

Returns:

  • (Object)

    The JSON-serializable state, usually a Hash

Since:

  • 0.0.1



779
780
781
# File 'lib/demiurge.rb', line 779

def state
  @state
end

#state_typeString

Returns The default StateItem type of this item. Can be overridden by child classes.

Returns:

  • (String)

    The default StateItem type of this item. Can be overridden by child classes.

Since:

  • 0.0.1



731
732
733
# File 'lib/demiurge.rb', line 731

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.

Returns:

  • (Boolean)

    Whether this item is considered a Zone.

Since:

  • 0.0.1



758
759
760
# File 'lib/demiurge.rb', line 758

def zone?
  false
end