class Demiurge::ActionItemInternal::ActionItemBlockRunner
The ActionItemBlockRunner
is a good, general-purpose block runner that supplies more context and more “gentle” object accessors to the block code. The methods of this class are generally intended to be used in the block code.
@since 0.0.1
Attributes
@return [Demiurge::Intention, nil] The current intention, if any @since 0.0.1
Public Class Methods
The constructor
@param item The item receiving the block and (usually) taking action @param current_intention
The current intention, if any; used for canceling @since 0.0.1
Demiurge::ActionItemInternal::BlockRunner::new
# File lib/demiurge/action_item.rb, line 263 def initialize(item, current_intention:) super(item) @current_intention = current_intention end
Public Instance Methods
Create an action to be executed immediately. This doesn't go through an agent's action queue or make anybody busy. It just happens during the current tick, but it uses the normal allow/offer/execute/notify cycle.
@param name [String] The action name @param args [Array] Additional arguments to send to the action @return [void] @since 0.0.1
# File lib/demiurge/action_item.rb, line 316 def action(name, *args) intention = ActionItemInternal::ActionIntention.new(engine, @item.name, name, *args) @item.engine.queue_intention(intention) nil end
Cancel the current intention. Raise a NoCurrentIntentionError if there isn't one.
@param reason [String] The reason to cancel @param extra_info [Hash] Additional cancellation info, if any @return [void] @since 0.0.1
# File lib/demiurge/action_item.rb, line 338 def cancel_intention(reason, extra_info = {}) raise ::Demiurge::Errors::NoCurrentIntentionError.new("No current intention in action of item #{@item.name}!", { "script_item": @item.name }, execution_context: @item.engine.execution_context) unless @current_intention @current_intention.cancel(reason, extra_info) nil end
Cancel the current intention. Do nothing if there isn't one.
@param reason [String] The reason to cancel @param extra_info [Hash] Additional cancellation info, if any @return [void] @since 0.0.1
# File lib/demiurge/action_item.rb, line 351 def cancel_intention_if_present(reason, extra_info = {}) @current_intention.cancel(reason, extra_info) if @current_intention end
Send a notification, starting from the location of the ActionItem
. Any fields other than the special “type”, “zone”, “location” and “actor” fields will be sent as additional notification fields.
@param data [Hash] The fields for the notification to send @option data [String] type The notification type to send @option data [String] zone The zone name to send the notification in; defaults to ActionItem's zone @option data [String] location The location name to send the notification in; defaults to ActionItem's location @option data [String] actor The acting item's name; defaults to this ActionItem
@return [void] @since 0.0.1
# File lib/demiurge/action_item.rb, line 298 def notification(data) type = data.delete("type") || data.delete(:type) || data.delete("type") || data.delete(:type) zone = to_demiurge_name(data.delete("zone") || data.delete(:zone) || @item.zone) location = to_demiurge_name(data.delete("location") || data.delete(:location) || @item.location) actor = to_demiurge_name(data.delete("actor") || data.delete(:actor) || @item) @item.engine.send_notification(data, type: type.to_s, zone: zone, location: location, actor: actor, include_context: true) nil end
For tiled maps, cut the position string apart into a location and X and Y tile coordinates within that location.
@param position [String] The position string @return [String, Integer, Integer] The location string, the X coordinate and the Y coordinate @since 0.0.1
# File lib/demiurge/action_item.rb, line 328 def position_to_location_and_tile_coords(position) ::Demiurge::TiledLocation.position_to_loc_coords(position) end
Access the item's state via a state wrapper. This only allows setting new fields or reading fields that already exist.
@return [Demiurge::ActionItemInternal::ActionItemStateWrapper] The state wrapper to control access @since 0.0.1
# File lib/demiurge/action_item.rb, line 273 def state @state_wrapper ||= ActionItemInternal::ActionItemStateWrapper.new(@item) end
Private Instance Methods
# File lib/demiurge/action_item.rb, line 278 def to_demiurge_name(item) return item if item.is_a?(String) return item.name if item.respond_to?(:name) raise Demiurge::Errors::BadScriptError.new("Not sure how to convert PORO to Demiurge name: #{item.inspect}!", execution_context: @item.engine.execution_context) end