Class: Demiurge::Location
- Inherits:
-
Container
- Object
- StateItem
- ActionItem
- Container
- Demiurge::Location
- Defined in:
- lib/demiurge/location.rb
Overview
A Location is generally found inside a Zone. It may contain items and agents.
Direct Known Subclasses
Constant Summary
Constants inherited from ActionItem
Instance Attribute Summary
Attributes inherited from StateItem
Instance Method Summary collapse
- #add_exit(from: any_legal_position, to:, to_location: nil, properties: {}) ⇒ Object
-
#adjacent_positions(pos, options = {}) ⇒ Array<String>
Returns an array of position strings for positions adjacent to the one given.
-
#any_legal_position ⇒ Object
Return a legal position of some kind within this Location.
-
#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.
-
#exits ⇒ Object
This isn't guaranteed to be in a particular format for all Locations everywhere.
- #finished_init ⇒ Object
-
#initialize(name, engine, state) ⇒ void
constructor
Constructor - set up exits.
-
#location ⇒ Object
A Location isn't located "inside" somewhere else.
-
#location_name ⇒ Object
A Location isn't located "inside" somewhere else.
-
#valid_position?(pos) ⇒ Boolean
Is this position valid in this location?.
- #zone ⇒ Object
- #zone_name ⇒ Object
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
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.
91 92 93 |
# File 'lib/demiurge/location.rb', line 91 def adjacent_positions(pos, = {}) @state["exits"].map { |e| e["to"] } end |
#any_legal_position ⇒ Object
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.
45 46 47 |
# File 'lib/demiurge/location.rb', line 45 def can_accomodate_agent?(agent, position) position == @name end |
#exits ⇒ Object
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_init ⇒ Object
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 |
#location ⇒ Object
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_name ⇒ Object
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?
58 59 60 |
# File 'lib/demiurge/location.rb', line 58 def valid_position?(pos) pos == @name end |
#zone ⇒ Object
37 38 39 |
# File 'lib/demiurge/location.rb', line 37 def zone @engine.item_by_name(@state["zone"]) end |
#zone_name ⇒ Object
33 34 35 |
# File 'lib/demiurge/location.rb', line 33 def zone_name @state["zone"] end |