Module: Gamefic::Scripting::Entities
Overview
Methods related to managing entities.
Instance Method Summary collapse
- #destroy(entity) ⇒ Object
- #entities ⇒ Array<Gamefic::Entity>
- #find(*args) ⇒ Object
-
#make(klass, **opts) ⇒ Gamefic::Entity
Create an entity.
-
#pick(*args) ⇒ Gamefic::Entity?
Pick a unique entity based on the given arguments.
-
#pick!(*args) ⇒ Gamefic::Entity
Same as #pick, but raise an error if a unique match could not be found.
- #players ⇒ Array<Gamefic::Actor, Gamefic::Active>
Methods included from Proxies
Instance Method Details
#destroy(entity) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/gamefic/scripting/entities.rb', line 36 def destroy(entity) entity.children.each { |child| destroy child } entity.parent = nil entity_set.delete entity entity end |
#entities ⇒ Array<Gamefic::Entity>
14 15 16 |
# File 'lib/gamefic/scripting/entities.rb', line 14 def entities entity_set.to_a end |
#find(*args) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gamefic/scripting/entities.rb', line 43 def find *args args.inject(entities) do |entities, arg| case arg when String result = Scanner.scan(entities, arg) result.remainder.empty? ? result.match : [] else entities.that_are(arg) end end end |
#make(klass, **opts) ⇒ Gamefic::Entity
Create an entity.
32 33 34 |
# File 'lib/gamefic/scripting/entities.rb', line 32 def make klass, **opts klass.new(**unproxy(opts)).tap { |entity| entity_set.add entity } end |
#pick(*args) ⇒ Gamefic::Entity?
Pick a unique entity based on the given arguments. String arguments are used to scan the entities for matching names and synonyms. Return nil if an entity could not be found or there is more than one possible match.
61 62 63 64 65 66 |
# File 'lib/gamefic/scripting/entities.rb', line 61 def pick *args matches = find(*args) return nil unless matches.one? matches.first end |
#pick!(*args) ⇒ Gamefic::Entity
Same as #pick, but raise an error if a unique match could not be found.
75 76 77 78 79 80 81 |
# File 'lib/gamefic/scripting/entities.rb', line 75 def pick! *args matches = find(*args) raise "no entity matching '#{args.inspect}'" if matches.empty? raise "multiple entities matching '#{args.inspect}': #{matches.join_and}" unless matches.one? matches.first end |
#players ⇒ Array<Gamefic::Actor, Gamefic::Active>
19 20 21 |
# File 'lib/gamefic/scripting/entities.rb', line 19 def players player_set.to_a end |