Module: Gamefic::Scripting::Entities

Includes:
Proxies
Included in:
Gamefic::Scripting
Defined in:
lib/gamefic/scripting/entities.rb

Overview

Methods related to managing entities.

Instance Method Summary collapse

Methods included from Proxies

#unproxy

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

#entitiesArray<Gamefic::Entity>

Returns:



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.

Examples:

class MyPlot < Gamefic::Plot
  seed { make Gamefic::Entity, name: 'thing' }
end

Parameters:

Returns:



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.

Returns:



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.

Parameters:

Returns:

Raises:

  • (RuntimeError)

    if a unique match was not 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

#playersArray<Gamefic::Actor, Gamefic::Active>



19
20
21
# File 'lib/gamefic/scripting/entities.rb', line 19

def players
  player_set.to_a
end