class Demiurge::Location
A Location
is generally found inside a Zone
. It may contain items and agents.
Public Class Methods
Constructor - set up exits
@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 @return [void] @since 0.0.1
Demiurge::Container::new
# File lib/demiurge/location.rb, line 12 def initialize(name, engine, state) super state["exits"] ||= [] end
Public Instance Methods
# File lib/demiurge/location.rb, line 62 def add_exit(from:any_legal_position, to:, to_location: nil, properties:{}) to_loc, to_coords = to.split("#",2) if to_location == nil to_location = @engine.item_by_name(to_loc) end raise("'From' position #{from.inspect} is invalid when adding exit to #{@name.inspect}!") unless valid_position?(from) raise("'To' position #{to.inspect} is invalid when adding exit to #{@name.inspect}!") unless to_location.valid_position?(to) exit_obj = { "from" => from, "to" => to, "properties" => properties } @state["exits"] ||= [] @state["exits"].push(exit_obj) exit_obj end
Returns an array of position strings for positions adjacent to the one given. In some areas this won't be meaningful. But for most “plain” areas, this gives possibilities of where is moveable for simple AIs.
@return [Array<String>] Array of position strings @since 0.0.1
# File lib/demiurge/location.rb, line 91 def adjacent_positions(pos, options = {}) @state["exits"].map { |e| e["to"] } end
Return a legal position of some kind within this Location
. By default there is only one position, which is just the Location's name. More complicated locations (e.g. tilemaps or procedural areas) may have more interesting positions inside them.
# File lib/demiurge/location.rb, line 53 def any_legal_position @name end
By default, the location can accomodate any agent number, size or shape, as long as it's in this location itself. Subclasses of location may have different abilities to accomodate different sizes or shapes of agent, and at different positions.
# File lib/demiurge/location.rb, line 45 def can_accomodate_agent?(agent, position) position == @name end
This isn't guaranteed to be in a particular format for all Locations everywhere. Sometimes exits in this form don't even make sense. So: this is best-effort when queried from a random Location
, and a known format only if you know the specific subclass of Location
you're dealing with.
# File lib/demiurge/location.rb, line 80 def exits @state["exits"] end
Demiurge::Container#finished_init
# File lib/demiurge/location.rb, line 17 def finished_init super # Make sure we're in our zone. zone.ensure_contains(@name) end
A Location
isn't located “inside” somewhere else. It is located in/at itself.
# File lib/demiurge/location.rb, line 29 def location self end
A Location
isn't located “inside” somewhere else. It is located in/at itself.
# File lib/demiurge/location.rb, line 24 def location_name @name end
Is this position valid in this location?
# File lib/demiurge/location.rb, line 58 def valid_position?(pos) pos == @name end
# File lib/demiurge/location.rb, line 37 def zone @engine.item_by_name(@state["zone"]) end
# File lib/demiurge/location.rb, line 33 def zone_name @state["zone"] end