Class: Worlds::World

Inherits:
Object
  • Object
show all
Defined in:
lib/worlds/world.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorld

Returns a new instance of World.



28
29
30
# File 'lib/worlds/world.rb', line 28

def initialize
  @areas = []
end

Class Attribute Details

.instanceObject (readonly)

Returns the value of attribute instance.



12
13
14
# File 'lib/worlds/world.rb', line 12

def instance
  @instance
end

Instance Attribute Details

#areasObject (readonly)

Returns the value of attribute areas.



9
10
11
# File 'lib/worlds/world.rb', line 9

def areas
  @areas
end

Class Method Details

.input(input) ⇒ Object



20
21
22
# File 'lib/worlds/world.rb', line 20

def self.input(input)
  instance.input(input)
end

.setupObject



15
16
17
18
# File 'lib/worlds/world.rb', line 15

def self.setup
  @instance = new
  @instance.demo_setup
end

.update(ms) ⇒ Object



24
25
26
# File 'lib/worlds/world.rb', line 24

def self.update(ms)
  instance.update(ms)
end

Instance Method Details

#demo_setupObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/worlds/world.rb', line 63

def demo_setup
  hero = Entity.new
  hero.components << Components::Travel.new(hero, commands: %w[travel t go leave])
  Entity.player = hero

  clock = Entity.new
  clock.components << Components::Ticker.new(clock)

  street = Area.new("a quiet street")
  street.entities << hero

  shop = Area.new("Rolf's Clock Shop")
  shop.linked_areas << street
  shop.entities << clock
  street.linked_areas << shop

  @areas << street
  @areas << shop

  output = Entity.player.components.find { _1.is_a? Components::Travel }.invoke(street)

  [output]
end

#input(input) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/worlds/world.rb', line 32

def input(input)
  Entity.player.components.each do |component|
    if component.commands.include? input[:command]
      if input[:type] == :select
        selection_index = input[:selection] - 1

        if selection_index >= component.select_options.count
          return { color: :red, content: "Invalid selection" }
        end

        return component.invoke(selection_index)
      else
        return [component.select] || [component.invoke]
      end
    end
  end

  nil
end

#update(ms) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/worlds/world.rb', line 52

def update(ms)
  outputs = []

  areas.each { |area|
    area_outputs = area.update(ms)
    outputs = area_outputs.compact if area == Entity.player.area
  }

  return outputs
end