Class: Gamefic::Engine::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic/engine/base.rb

Overview

Basic functionality for running a single-player game from a console.

Direct Known Subclasses

Tty

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plot) ⇒ Base

Returns a new instance of Base.



9
10
11
12
# File 'lib/gamefic/engine/base.rb', line 9

def initialize(plot)
  @plot = plot
  post_initialize
end

Instance Attribute Details

#plotObject (readonly)

Returns the value of attribute plot.



7
8
9
# File 'lib/gamefic/engine/base.rb', line 7

def plot
  @plot
end

#user_classObject



18
19
20
# File 'lib/gamefic/engine/base.rb', line 18

def user_class
  @user_class ||= Gamefic::User::Base
end

Instance Method Details

#connectObject



22
23
24
25
26
27
# File 'lib/gamefic/engine/base.rb', line 22

def connect
  @character = @plot.make Character, name: 'yourself', synonyms: 'self myself you me', proper_named: true
  @user = user_class.new
  @character.connect @user
  @character
end

#post_initializeObject



14
15
16
# File 'lib/gamefic/engine/base.rb', line 14

def post_initialize
  # Override in subclasses
end

#receiveObject



59
60
61
62
63
# File 'lib/gamefic/engine/base.rb', line 59

def receive
  print @character.scene.prompt + ' '
  input = STDIN.gets
  @character.queue.push input unless input.nil?
end

#runObject



29
30
31
32
33
34
35
# File 'lib/gamefic/engine/base.rb', line 29

def run
  connect
  @plot.introduce @character
  @user.update @character.state
  turn until @character.concluded?
  #print @user.flush
end

#turnObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gamefic/engine/base.rb', line 37

def turn
  @plot.ready
  unless @character.state[:options].nil?
    list = '<ol class="multiple_choice">'
    @character.state[:options].each { |o|
      list += "<li><a href=\"#\" rel=\"gamefic\" data-command=\"#{o}\">#{o}</a></li>"
    }
    list += "</ol>"
    @character.tell list
  end
  #print @user.flush
  @user.update @character.state
  #@character.flush
  if @character.queue.empty?
    receive
  end
  @plot.update
  #print @user.flush
  @user.update @character.state
  #@character.flush
end