Class: T_BAG::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/tbag/game.rb

Overview

Defines a game and its run behavior.

Instance Method Summary collapse

Constructor Details

#initialize(title, author) ⇒ Game

Returns a new instance of Game.

Parameters:

  • title (String)

    the game title

  • author (String)

    the game author



34
35
36
37
38
39
40
41
42
43
# File 'lib/tbag/game.rb', line 34

def initialize(title, author)
  @title = title
  @author = author
  @scenes = {}
  @current_scene = nil
  @start_scene = nil
  @next_scene = nil
  @menu_scene = nil
  @called = false
end

Instance Method Details

#game_over(&block) ⇒ Void

Add a game over scene to the scene queue.

Parameters:

  • block (Block)

Returns:

  • (Void)


59
60
61
62
63
# File 'lib/tbag/game.rb', line 59

def game_over(&block)
  game_over = T_BAG::Game_Over.new
  @scenes[:endgame] = game_over
  Docile.dsl_eval(game_over, &block)
end

#load_game(infile) ⇒ Void

Load a game state from infile.

Parameters:

  • infile (String)

    the name of the file to load from

Returns:

  • (Void)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/tbag/game.rb', line 158

def load_game(infile)
  begin
    if File.exist? infile
      in_file = File.open(infile, 'r+')
      state = in_file.gets.chomp.intern
      #print state
      next_scene state
      puts 'Load Successful!'
    else
      puts 'File ' + String(infile) + ' not found.'
      reset
    end
  rescue
    puts 'Load Failed!'
  end
end

Add a main menu scene to the scene queue.

Parameters:

  • block (Block)

Returns:

  • (Void)


68
69
70
71
72
# File 'lib/tbag/game.rb', line 68

def main_menu(&block)
  main_menu = T_BAG::Main_Menu.new
  @scenes[:menu] = main_menu
  Docile.dsl_eval(main_menu, &block)
end

Save the given scene as the menu scene.

Parameters:

  • scene (Symbol)

    a scene

Returns:

  • (Void)


90
91
92
# File 'lib/tbag/game.rb', line 90

def menu(scene)
  @menu_scene = scene
end

#next_scene(scene) ⇒ Void

Set the given scene to the next scene in the game.

Parameters:

  • scene (Symbol)

    a scene

Returns:

  • (Void)


110
111
112
# File 'lib/tbag/game.rb', line 110

def next_scene(scene)
  @next_scene = scene
end

#quitVoid

Quit the game.

Returns:

  • (Void)


128
129
130
131
# File 'lib/tbag/game.rb', line 128

def quit
  puts 'Goodbye!'
  exit
end

#resetVoid

Reset the game by going back to the starting scene.

Returns:

  • (Void)


116
117
118
# File 'lib/tbag/game.rb', line 116

def reset
  @next_scene = @start_scene
end

#runVoid

Run the scenes in chronological order until it finds a nil scene.

Returns:

  • (Void)


96
97
98
99
100
101
102
103
104
105
# File 'lib/tbag/game.rb', line 96

def run
  until @current_scene.nil?
    scene = @scenes[@current_scene]
    if scene.nil?
      $stderr.puts "[GAME] Scene '#{@current_scene}' does not exist"
    end
    scene.run
    @current_scene = @next_scene
  end
end

#save_game(outfile) ⇒ Void

Save the current game state as outfile.

Parameters:

  • outfile (String)

    the name of the file to save to

Returns:

  • (Void)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tbag/game.rb', line 136

def save_game(outfile)
  begin
  if File.exist? outfile
    out_file = File.open(outfile, 'r+')
    out_file.puts @current_scene
    out_file.close
    puts 'Save Successful!'
  else
    out_file = File.new(outfile, 'w')
    puts 'Created New Save File...'
    out_file.puts @current_scene
    out_file.close
    puts 'Save Successful!'
  end
  rescue Exception => ex
    puts 'Save Failed!'
  end
end

#scene(name, title, &block) ⇒ Void

Add a scene to the scene queue.

Parameters:

  • name (String)

    the name of the scene

  • title (String)

    the title of the scene

  • block (Block)

Returns:

  • (Void)


50
51
52
53
54
# File 'lib/tbag/game.rb', line 50

def scene(name, title, &block)
  scene = T_BAG::Scene.new(name, title)
  @scenes[name] = scene
  Docile.dsl_eval(scene, &block)
end

#start(scene) ⇒ Void

Save the given scene as the starting scene and start the scene.

Parameters:

  • scene (Symbol)

    a scene

Returns:

  • (Void)


77
78
79
80
81
82
83
84
85
# File 'lib/tbag/game.rb', line 77

def start(scene)
  if @called == false
    @current_scene = scene
    @start_scene = scene
    @called = true
  else
    @start_scene = scene
  end
end

#tomenuVoid

Reset the game by going back to the menu scene.

Returns:

  • (Void)


122
123
124
# File 'lib/tbag/game.rb', line 122

def tomenu
  @next_scene = @menu_scene
end