Class: Gamefic::Plot::Darkroom

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic/plot/darkroom.rb

Overview

Create and restore plot snapshots.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plot) ⇒ Darkroom

Returns a new instance of Darkroom.



7
8
9
# File 'lib/gamefic/plot/darkroom.rb', line 7

def initialize plot
  @plot = plot
end

Instance Attribute Details

#plotObject (readonly)

Returns the value of attribute plot.



5
6
7
# File 'lib/gamefic/plot/darkroom.rb', line 5

def plot
  @plot
end

Instance Method Details

#restore(snapshot) ⇒ Object

Restore a snapshot.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gamefic/plot/darkroom.rb', line 40

def restore snapshot
  entity_store.clear
  player_store.clear
  plot.subplots.each { |s| s.conclude }
  plot.entities[plot.initial_state[:entities].length..-1].each { |e| plot.destroy e }
  entity_store.concat plot.entities[0..plot.initial_state[:entities].length-1]
  entity_store.uniq!
  player_store.concat plot.players
  i = 0
  snapshot[:entities].each { |h|
    if entity_store[i].nil?
      e = plot.stage do
        cls = self.const_get(h[:class])
        make cls
      end
      entity_store.push e
    end
    i += 1
  }
  snapshot[:subplots].each { |s|
    sp = plot.stage do
      cls = const_get(s[:class])
      branch cls
    end
    # @todo Assuming one player
    sp.introduce player_store[0] unless player_store.empty?
    rebuild_subplot sp, s
  }
  i = 0
  snapshot[:entities].each { |h|
    rebuild1 entity_store[i], h
    i += 1
  }
  i = 0
  snapshot[:players].each { |p|
    rebuild1 player_store[i], p
    i += 1
  }
  i = 0
  snapshot[:entities].each { |h|
    rebuild2 entity_store[i], h
    i += 1
  }
  i = 0
  snapshot[:players].each { |h|
    rebuild2 player_store[i], h
    i += 1
  }
  snapshot[:instance_variables].each_pair { |k, v|
    plot.theater.instance_variable_set(k, unserialize(v))
  }
end

#saveHash

Create a snapshot of the plot.

Returns:

  • (Hash)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gamefic/plot/darkroom.rb', line 14

def save
  result = { entities: [], players: [], subplots: [], instance_variables: {} }
  entity_store.clear
  player_store.clear
  entity_store.concat plot.entities
  player_store.concat plot.players
  plot.subplots.each { |s| entity_store.concat s.entities }
  entity_store.uniq!
  entity_store.each do |e|
    result[:entities].push hash_entity(e)
  end
  player_store.each do |p|
    result[:players].push hash_entity(p)
  end
  plot.theater.instance_variables.each { |i|
    v = plot.theater.instance_variable_get(i)
    result[:instance_variables][i] = serialize(v) if can_serialize?(v)
  }
  plot.subplots.each { |s|
    result[:subplots].push hash_subplot(s)
  }
  result
end