Class: NPCS

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/Olib/npcs/npcs.rb

Overview

a collection for managing all of the npcs in a room

Constant Summary collapse

ALL =
-> npc { true }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&predicate) ⇒ NPCS

Returns a new instance of NPCS.



11
12
13
# File 'lib/Olib/npcs/npcs.rb', line 11

def initialize(&predicate)
  @predicate = predicate
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/Olib/npcs/npcs.rb', line 25

def method_missing(method, *args)
  if to_a.respond_to?(method)
    to_a.send(method, *args)
  else
    super(method, *args)
  end
end

Instance Attribute Details

#predicateObject (readonly)

Returns the value of attribute predicate.



9
10
11
# File 'lib/Olib/npcs/npcs.rb', line 9

def predicate
  @predicate
end

Class Method Details

.method_missing(method, *args, &block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/Olib/npcs/npcs.rb', line 33

def self.method_missing(method, *args, &block)
  if respond_to?(method)
    NPCS.new.send(method, *args, &block)
  else
    super(method, *args, &block)
  end
end

.respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/Olib/npcs/npcs.rb', line 41

def self.respond_to?(method)
  return super(method) unless NPCS.new.respond_to?(method)
  return true
end

Instance Method Details

#eachObject



15
16
17
18
19
# File 'lib/Olib/npcs/npcs.rb', line 15

def each()    
  GameObj.npcs.to_a.map do |obj| NPC.new(obj) end
    .select(&@predicate)
    .each do |npc| yield(npc) unless npc.tags.include?(:aggressive) end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/Olib/npcs/npcs.rb', line 21

def respond_to_missing?(method, include_private = false)
  to_a.respond_to?(method) or super
end