Class: CoopAl::State

Inherits:
Object
  • Object
show all
Defined in:
lib/coop_al/state.rb

Overview

State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

Returns a new instance of State.



9
10
11
12
13
14
# File 'lib/coop_al/state.rb', line 9

def initialize
  @path_stack = [Path.root]
  @xp = 0
  @loot = Loot.empty
  @visited_paths = []
end

Instance Attribute Details

#current_pathObject

Returns the value of attribute current_path.



7
8
9
# File 'lib/coop_al/state.rb', line 7

def current_path
  @current_path
end

#itemsObject (readonly)

Returns the value of attribute items.



6
7
8
# File 'lib/coop_al/state.rb', line 6

def items
  @items
end

#lootObject (readonly)

Returns the value of attribute loot.



6
7
8
# File 'lib/coop_al/state.rb', line 6

def loot
  @loot
end

#xpObject (readonly)

Returns the value of attribute xp.



6
7
8
# File 'lib/coop_al/state.rb', line 6

def xp
  @xp
end

Instance Method Details

#add_items(items) ⇒ Object



73
74
75
# File 'lib/coop_al/state.rb', line 73

def add_items(items)
  @items.concat(items)
end

#add_loot(loot) ⇒ Object



69
70
71
# File 'lib/coop_al/state.rb', line 69

def add_loot(loot)
  @loot += loot
end

#add_xp(xp) ⇒ Object



65
66
67
# File 'lib/coop_al/state.rb', line 65

def add_xp(xp)
  @xp += xp
end

#all_available_paths(library) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/coop_al/state.rb', line 32

def all_available_paths(library)
  if path_depth == 1
    if in_downtime?
      library.all_entries
    else
      chapter = library.resolve(current_path)
      chapter.links + downtime_if_available(chapter)
    end
  else
    chapter = library.resolve(@path_stack[0])
    library.all_entries + chapter.links + downtime_if_available(chapter)
  end
end

#apply_path(path) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/coop_al/state.rb', line 51

def apply_path(path)
  if path_depth == 1
    if path.root?
      @path_stack.push(path)
    else
      @path_stack = [current_path + path]
      @visited_paths << current_path
    end
  else
    @path_stack = [path]
    @visited_paths << path
  end
end

#available_paths(library) ⇒ Object



28
29
30
# File 'lib/coop_al/state.rb', line 28

def available_paths(library)
  all_available_paths(library).select { |p| !@visited_paths.include?(p) }
end

#downtime_if_available(chapter) ⇒ Object



46
47
48
49
# File 'lib/coop_al/state.rb', line 46

def downtime_if_available(chapter)
  return [Path.root] if chapter.links_to_downtime? && !in_downtime?
  []
end

#history_includes?(path) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/coop_al/state.rb', line 81

def history_includes?(path)
  @visited_paths.include?(path)
end

#in_downtime?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/coop_al/state.rb', line 24

def in_downtime?
  current_path.root?
end

#path_depthObject



20
21
22
# File 'lib/coop_al/state.rb', line 20

def path_depth
  @path_stack.size
end

#treasure_valueObject



77
78
79
# File 'lib/coop_al/state.rb', line 77

def treasure_value
  @treasure.inject(Value.new) { |a, e| a + e.value }
end