class Demiurge::Location

A Location is generally found inside a Zone. It may contain items and agents.

Public Class Methods

new(name, engine, state) click to toggle source

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

Calls superclass method Demiurge::Container::new
# File lib/demiurge/location.rb, line 12
def initialize(name, engine, state)
  super
  state["exits"] ||= []
end

Public Instance Methods

add_exit(from:any_legal_position, to:, to_location: nil, properties:{}) click to toggle source
# 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
adjacent_positions(pos, options = {}) click to toggle source

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
can_accomodate_agent?(agent, position) click to toggle source

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
exits() click to toggle source

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
finished_init() click to toggle source
Calls superclass method 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
location() click to toggle source

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
location_name() click to toggle source

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
valid_position?(pos) click to toggle source

Is this position valid in this location?

# File lib/demiurge/location.rb, line 58
def valid_position?(pos)
  pos == @name
end
zone() click to toggle source
# File lib/demiurge/location.rb, line 37
def zone
  @engine.item_by_name(@state["zone"])
end
zone_name() click to toggle source
# File lib/demiurge/location.rb, line 33
def zone_name
  @state["zone"]
end