Module: Gamefic::World::Entities
- Included in:
- Gamefic::World, Commands, Players
- Defined in:
- lib/gamefic/world/entities.rb
Instance Method Summary collapse
-
#cast(cls, args = {}, &block) ⇒ Gamefic::Actor, Gamefic::Active
Cast an active entity.
-
#destroy(entity) ⇒ Object
Safely remove an entity from a plot.
-
#entities ⇒ Array<Gamefic::Entity>
Get an array of entities associated with this plot.
-
#make(cls, args = {}, &block) ⇒ cls
Make a new Entity with the provided properties.
-
#pick(description) ⇒ Gamefic::Entity
Pick an entity based on its description.
-
#players ⇒ Array<Gamefic::Actor>
Get an array of players associated with this plot.
Instance Method Details
#cast(cls, args = {}, &block) ⇒ Gamefic::Actor, Gamefic::Active
Cast an active entity. This method is similar to make, but it also provides the plot’s playbook to the entity so it can perform actions. The entity should either be a kind of Gamefic::Actor or include the Gamefic::Active module.
30 31 32 33 34 |
# File 'lib/gamefic/world/entities.rb', line 30 def cast cls, args = {}, &block ent = make cls, args, &block ent.playbooks.push playbook ent end |
#destroy(entity) ⇒ Object
Safely remove an entity from a plot.
If the entity is dynamic (e.g., created after a plot is already running), it is safe to delete it completely. Otherwise the entity will still be referenced in the entities array, but its parent will be set to nil.
44 45 46 47 48 49 50 51 |
# File 'lib/gamefic/world/entities.rb', line 44 def destroy entity entity.parent = nil index = entities.index(entity) return if index.nil? || index < static_entity_length - 1 entities.delete_at index players.delete entity entity.destroy end |
#entities ⇒ Array<Gamefic::Entity>
Get an array of entities associated with this plot.
79 80 81 |
# File 'lib/gamefic/world/entities.rb', line 79 def entities @entities ||= [] end |
#make(cls, args = {}, &block) ⇒ cls
Make a new Entity with the provided properties.
16 17 18 19 20 21 |
# File 'lib/gamefic/world/entities.rb', line 16 def make cls, args = {}, &block raise ArgumentError, "Invalid Entity class" unless cls.is_a?(Class) && cls <= Entity ent = cls.new args, &block entities.push ent ent end |
#pick(description) ⇒ Gamefic::Entity
Pick an entity based on its description. The description provided must match exactly one entity; otherwise an error is raised.
66 67 68 69 70 71 72 73 74 |
# File 'lib/gamefic/world/entities.rb', line 66 def pick(description) result = Query::Matches.execute(entities, description) if result.objects.length == 0 raise IndexError.new("Unable to find entity from '#{description}'") elsif result.objects.length > 1 raise IndexError.new("Ambiguous entities found from '#{description}'") end result.objects[0] end |
#players ⇒ Array<Gamefic::Actor>
Get an array of players associated with this plot.
86 87 88 |
# File 'lib/gamefic/world/entities.rb', line 86 def players @players ||= [] end |