Class: T_BAG::Game
- Inherits:
-
Object
- Object
- T_BAG::Game
- Defined in:
- lib/tbag/game.rb
Overview
Defines a game and its run behavior.
Instance Method Summary collapse
-
#game_over(&block) ⇒ Void
Add a game over scene to the scene queue.
-
#initialize(title, author) ⇒ Game
constructor
A new instance of Game.
-
#load_game(infile) ⇒ Void
Load a game state from infile.
-
#main_menu(&block) ⇒ Void
Add a main menu scene to the scene queue.
-
#menu(scene) ⇒ Void
Save the given scene as the menu scene.
-
#next_scene(scene) ⇒ Void
Set the given scene to the next scene in the game.
-
#quit ⇒ Void
Quit the game.
-
#reset ⇒ Void
Reset the game by going back to the starting scene.
-
#run ⇒ Void
Run the scenes in chronological order until it finds a nil scene.
-
#save_game(outfile) ⇒ Void
Save the current game state as outfile.
-
#scene(name, title, &block) ⇒ Void
Add a scene to the scene queue.
-
#start(scene) ⇒ Void
Save the given scene as the starting scene and start the scene.
-
#tomenu ⇒ Void
Reset the game by going back to the menu scene.
Constructor Details
#initialize(title, author) ⇒ Game
Returns a new instance of Game.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tbag/game.rb', line 34 def initialize(title, ) @title = title @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.
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.
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 |
#main_menu(&block) ⇒ Void
Add a main menu scene to the scene queue.
68 69 70 71 72 |
# File 'lib/tbag/game.rb', line 68 def (&block) = T_BAG::Main_Menu.new @scenes[:menu] = Docile.dsl_eval(, &block) end |
#menu(scene) ⇒ Void
Save the given scene as the menu scene.
90 91 92 |
# File 'lib/tbag/game.rb', line 90 def (scene) @menu_scene = scene end |
#next_scene(scene) ⇒ Void
Set the given scene to the next scene in the game.
110 111 112 |
# File 'lib/tbag/game.rb', line 110 def next_scene(scene) @next_scene = scene end |
#quit ⇒ Void
Quit the game.
128 129 130 131 |
# File 'lib/tbag/game.rb', line 128 def quit puts 'Goodbye!' exit end |
#reset ⇒ Void
Reset the game by going back to the starting scene.
116 117 118 |
# File 'lib/tbag/game.rb', line 116 def reset @next_scene = @start_scene end |
#run ⇒ Void
Run the scenes in chronological order until it finds a nil scene.
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.
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.
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.
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 |
#tomenu ⇒ Void
Reset the game by going back to the menu scene.
122 123 124 |
# File 'lib/tbag/game.rb', line 122 def @next_scene = @menu_scene end |