Class: Demiurge::Location

Inherits:
Container show all
Defined in:
lib/demiurge/location.rb

Overview

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

Direct Known Subclasses

TiledLocation

Constant Summary

Constants inherited from ActionItem

ActionItem::ACTION_LEGAL_KEYS

Instance Attribute Summary

Attributes inherited from StateItem

#engine, #name

Instance Method Summary collapse

Methods inherited from Container

#contents, #contents_names, #ensure_contains, #ensure_does_not_contain, #intentions_for_next_step, #item_change_location, #item_change_position, #move_item_inside, #receive_offer

Methods inherited from ActionItem

#__state_internal, #get_action, #get_actions_with_tags, #intentions_for_next_step, #position, #register_actions, #run_action

Methods inherited from StateItem

#agent?, from_name_type, #get_structure, #intentions_for_next_step, #state, #state_type, #zone?

Constructor Details

#initialize(name, engine, state) ⇒ void

Constructor - set up exits

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

Since:

  • 0.0.1



12
13
14
15
# File 'lib/demiurge/location.rb', line 12

def initialize(name, engine, state)
  super
  state["exits"] ||= []
end

Instance Method Details

#add_exit(from: any_legal_position, to:, to_location: nil, properties: {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/demiurge/location.rb', line 62

def add_exit(from:any_legal_position, to:, to_location: nil, properties:{})
  to_loc = to.split("#",2)[0]
  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 = {}) ⇒ Array<String>

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.

Returns:

  • (Array<String>)

    Array of position strings

Since:

  • 0.0.1



91
92
93
# 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.



53
54
55
# File 'lib/demiurge/location.rb', line 53

def any_legal_position
  @name
end

#can_accomodate_agent?(agent, position) ⇒ Boolean

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.

Returns:

  • (Boolean)


45
46
47
# File 'lib/demiurge/location.rb', line 45

def can_accomodate_agent?(agent, position)
  position == @name
end

#exitsObject

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.



80
81
82
# File 'lib/demiurge/location.rb', line 80

def exits
  @state["exits"]
end

#finished_initObject



17
18
19
20
21
# File 'lib/demiurge/location.rb', line 17

def finished_init
  super
  # Make sure we're in our zone.
  zone.ensure_contains(@name)
end

#locationObject

A Location isn't located "inside" somewhere else. It is located in/at itself.



29
30
31
# File 'lib/demiurge/location.rb', line 29

def location
  self
end

#location_nameObject

A Location isn't located "inside" somewhere else. It is located in/at itself.



24
25
26
# File 'lib/demiurge/location.rb', line 24

def location_name
  @name
end

#valid_position?(pos) ⇒ Boolean

Is this position valid in this location?

Returns:

  • (Boolean)


58
59
60
# File 'lib/demiurge/location.rb', line 58

def valid_position?(pos)
  pos == @name
end

#zoneObject



37
38
39
# File 'lib/demiurge/location.rb', line 37

def zone
  @engine.item_by_name(@state["zone"])
end

#zone_nameObject



33
34
35
# File 'lib/demiurge/location.rb', line 33

def zone_name
  @state["zone"]
end