Module: Gamefic::World::Entities

Included in:
Gamefic::World, Commands, Players
Defined in:
lib/gamefic/world/entities.rb

Instance Method Summary collapse

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
52
53
54
55
56
57
58
# 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


  # @todo It might make sense to destroy the entity completely now. It

  #   will still have a reference in the index, but that shouldn't impact

  #   the current state of the plot.

  return if static.include?(entity)
  entities.delete entity
  players.delete entity
end

#entitiesArray<Gamefic::Entity>

Get an array of entities associated with this plot.



86
87
88
# File 'lib/gamefic/world/entities.rb', line 86

def entities
  @entities ||= []
end

#make(cls, args = {}, &block) ⇒ cls

Make a new Entity with the provided properties.

Examples:

Create an Entity

chair = make Entity, name: 'red chair'
chair.name #=> 'red chair'

Raises:

  • (ArgumentError)

    if class is not an Entity



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.

Examples:

Select the Entity that matches the description

red_chair = make Entity, :name => 'red chair'
blue_chair = make Entity, :name => 'blue chair'
pick "red chair" #=> red_chair
pick "blue chair" #=> blue_chair
pick "chair" #=> IndexError: description is ambiguous


73
74
75
76
77
78
79
80
81
# File 'lib/gamefic/world/entities.rb', line 73

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

#playersArray<Gamefic::Actor>

Get an array of players associated with this plot.



93
94
95
# File 'lib/gamefic/world/entities.rb', line 93

def players
  @players ||= []
end