t-bag 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/examples/sample.rb +71 -0
- data/lib/tbag.rb +53 -0
- data/lib/tbag/game.rb +175 -0
- data/lib/tbag/game_over.rb +43 -0
- data/lib/tbag/main_menu.rb +42 -0
- data/lib/tbag/prompt.rb +79 -0
- data/lib/tbag/scene.rb +114 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cf302fa7c2f18d90737044c46ba9ec78e06fd45d
|
4
|
+
data.tar.gz: eb61e19872f242cd77f6a0043c252ff4ddde4e1d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: de4cd7eb3eda6c6d8eeeda67b219b428d00169957fd849dbfb4217bc097486056565b41fb9efef113d8f52cad1414da4ca163147fca9d1ece96e4a3a62687dfc
|
7
|
+
data.tar.gz: 8586e0060c3fc2a5f3eebda88dc9c3987aaa21806d7b0e8252531c3efa84bea507e6775234cfef95deac16e16975286c5926b660b852bcf9d2e239aa8391db9f
|
data/examples/sample.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
=begin
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2015 Benjamin Meyers as Lion Logic <lion.logic.org@gmail.com>
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
23
|
+
=end
|
24
|
+
require_relative '../lib/tbag'
|
25
|
+
include T_BAG
|
26
|
+
|
27
|
+
game 'Zork', 'Infocom' do
|
28
|
+
start :scene1
|
29
|
+
scene :scene1, 'West of House' do
|
30
|
+
text 'You are standing in an open field west of a white house, with a boarded front door.'
|
31
|
+
pause
|
32
|
+
text 'There is a small mailbox here.'
|
33
|
+
pause
|
34
|
+
prompt 'What would you like to do?', String, 'I don\'t know that word.' do
|
35
|
+
choice 'open' do
|
36
|
+
puts 'The door cannot be opened.'
|
37
|
+
end
|
38
|
+
choice 'open mailbox' do
|
39
|
+
puts 'Opening the small mailbox reveals a leaflet.'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
prompt 'What would you like to do?', String, 'I don\'t know that word.' do
|
43
|
+
choice 'read leaflet' do
|
44
|
+
scene_change :scene2
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
scene :scene2, 'WELCOME TO ZORK!' do
|
49
|
+
text 'WELCOME TO ZORK!'
|
50
|
+
pause
|
51
|
+
text 'ZORK is a game of adventure, danger, and low cunning.'
|
52
|
+
text 'In it you will explore some of the most amazing territory ever seen by mortals.'
|
53
|
+
text 'No computer should be without one!'
|
54
|
+
pause
|
55
|
+
scene_change :endgame
|
56
|
+
end
|
57
|
+
game_over do
|
58
|
+
text 'You were eaten by a grue.'
|
59
|
+
pause
|
60
|
+
prompt 'Would you like to start over?', String, 'I\'m afraid I don\'t like that answer...' do
|
61
|
+
choice 'yes' do
|
62
|
+
$game.reset
|
63
|
+
end
|
64
|
+
choice 'no' do
|
65
|
+
$game.quit
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
run
|
data/lib/tbag.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
=begin
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2015 Benjamin Meyers as Lion Logic <lion.logic.org@gmail.com>
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
23
|
+
=end
|
24
|
+
|
25
|
+
require_relative 'tbag/game'
|
26
|
+
require_relative 'tbag/prompt'
|
27
|
+
require_relative 'tbag/scene'
|
28
|
+
require_relative 'tbag/game_over'
|
29
|
+
require_relative 'tbag/main_menu'
|
30
|
+
|
31
|
+
require 'docile'
|
32
|
+
|
33
|
+
# A framework for writing text-based adventure games.
|
34
|
+
# @author Benjamin S. Meyers
|
35
|
+
module T_BAG
|
36
|
+
# The version number.
|
37
|
+
VERSION = '0.0.4'
|
38
|
+
|
39
|
+
# @param [String] title the game title
|
40
|
+
# @param [String] author the game author
|
41
|
+
# @param [Block] block
|
42
|
+
def game(title, author, &block)
|
43
|
+
$game = T_BAG::Game.new(title, author)
|
44
|
+
|
45
|
+
Docile.dsl_eval($game, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Run the game.
|
49
|
+
# @return [Void]
|
50
|
+
def run
|
51
|
+
$game.run
|
52
|
+
end
|
53
|
+
end
|
data/lib/tbag/game.rb
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
=begin
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2015 Benjamin Meyers as Lion Logic <lion.logic.org@gmail.com>
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
23
|
+
=end
|
24
|
+
|
25
|
+
require_relative '../tbag'
|
26
|
+
|
27
|
+
require 'docile'
|
28
|
+
|
29
|
+
module T_BAG
|
30
|
+
# Defines a game and its run behavior.
|
31
|
+
class Game
|
32
|
+
# @param [String] title the game title
|
33
|
+
# @param [String] author the game author
|
34
|
+
def initialize(title, author)
|
35
|
+
@title = title
|
36
|
+
@author = author
|
37
|
+
@scenes = {}
|
38
|
+
@current_scene = nil
|
39
|
+
@start_scene = nil
|
40
|
+
@next_scene = nil
|
41
|
+
@menu_scene = nil
|
42
|
+
@called = false
|
43
|
+
end
|
44
|
+
|
45
|
+
# Add a scene to the scene queue.
|
46
|
+
# @param [String] name the name of the scene
|
47
|
+
# @param [String] title the title of the scene
|
48
|
+
# @param [Block] block
|
49
|
+
# @return [Void]
|
50
|
+
def scene(name, title, &block)
|
51
|
+
scene = T_BAG::Scene.new(name, title)
|
52
|
+
@scenes[name] = scene
|
53
|
+
Docile.dsl_eval(scene, &block)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Add a game over scene to the scene queue.
|
57
|
+
# @param [Block] block
|
58
|
+
# @return [Void]
|
59
|
+
def game_over(&block)
|
60
|
+
game_over = T_BAG::Game_Over.new
|
61
|
+
@scenes[:endgame] = game_over
|
62
|
+
Docile.dsl_eval(game_over, &block)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Add a main menu scene to the scene queue.
|
66
|
+
# @param [Block] block
|
67
|
+
# @return [Void]
|
68
|
+
def main_menu(&block)
|
69
|
+
main_menu = T_BAG::Main_Menu.new
|
70
|
+
@scenes[:menu] = main_menu
|
71
|
+
Docile.dsl_eval(main_menu, &block)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Save the given scene as the starting scene and start the scene.
|
75
|
+
# @param [Symbol] scene a scene
|
76
|
+
# @return [Void]
|
77
|
+
def start(scene)
|
78
|
+
if @called == false
|
79
|
+
@current_scene = scene
|
80
|
+
@start_scene = scene
|
81
|
+
@called = true
|
82
|
+
else
|
83
|
+
@start_scene = scene
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Save the given scene as the menu scene.
|
88
|
+
# @param [Symbol] scene a scene
|
89
|
+
# @return [Void]
|
90
|
+
def menu(scene)
|
91
|
+
@menu_scene = scene
|
92
|
+
end
|
93
|
+
|
94
|
+
# Run the scenes in chronological order until it finds a nil scene.
|
95
|
+
# @return [Void]
|
96
|
+
def run
|
97
|
+
until @current_scene.nil?
|
98
|
+
scene = @scenes[@current_scene]
|
99
|
+
if scene.nil?
|
100
|
+
$stderr.puts "[GAME] Scene '#{@current_scene}' does not exist"
|
101
|
+
end
|
102
|
+
scene.run
|
103
|
+
@current_scene = @next_scene
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Set the given scene to the next scene in the game.
|
108
|
+
# @param [Symbol] scene a scene
|
109
|
+
# @return [Void]
|
110
|
+
def next_scene(scene)
|
111
|
+
@next_scene = scene
|
112
|
+
end
|
113
|
+
|
114
|
+
# Reset the game by going back to the starting scene.
|
115
|
+
# @return [Void]
|
116
|
+
def reset
|
117
|
+
@next_scene = @start_scene
|
118
|
+
end
|
119
|
+
|
120
|
+
# Reset the game by going back to the menu scene.
|
121
|
+
# @return [Void]
|
122
|
+
def tomenu
|
123
|
+
@next_scene = @menu_scene
|
124
|
+
end
|
125
|
+
|
126
|
+
# Quit the game.
|
127
|
+
# @return [Void]
|
128
|
+
def quit
|
129
|
+
puts 'Goodbye!'
|
130
|
+
exit
|
131
|
+
end
|
132
|
+
|
133
|
+
# Save the current game state as outfile.
|
134
|
+
# @param [String] outfile the name of the file to save to
|
135
|
+
# @return [Void]
|
136
|
+
def save_game(outfile)
|
137
|
+
begin
|
138
|
+
if File.exist? outfile
|
139
|
+
out_file = File.open(outfile, 'r+')
|
140
|
+
out_file.puts @current_scene
|
141
|
+
out_file.close
|
142
|
+
puts 'Save Successful!'
|
143
|
+
else
|
144
|
+
out_file = File.new(outfile, 'w')
|
145
|
+
puts 'Created New Save File...'
|
146
|
+
out_file.puts @current_scene
|
147
|
+
out_file.close
|
148
|
+
puts 'Save Successful!'
|
149
|
+
end
|
150
|
+
rescue Exception => ex
|
151
|
+
puts 'Save Failed!'
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
# Load a game state from infile.
|
156
|
+
# @param [String] infile the name of the file to load from
|
157
|
+
# @return [Void]
|
158
|
+
def load_game(infile)
|
159
|
+
begin
|
160
|
+
if File.exist? infile
|
161
|
+
in_file = File.open(infile, 'r+')
|
162
|
+
state = in_file.gets.chomp.intern
|
163
|
+
#print state
|
164
|
+
next_scene state
|
165
|
+
puts 'Load Successful!'
|
166
|
+
else
|
167
|
+
puts 'File ' + String(infile) + ' not found.'
|
168
|
+
reset
|
169
|
+
end
|
170
|
+
rescue
|
171
|
+
puts 'Load Failed!'
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
=begin
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2015 Benjamin Meyers as Lion Logic <lion.logic.org@gmail.com>
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
23
|
+
=end
|
24
|
+
|
25
|
+
require_relative '../tbag'
|
26
|
+
|
27
|
+
require 'docile'
|
28
|
+
|
29
|
+
module T_BAG
|
30
|
+
# Defines a special Game Over scene and its run behavior.
|
31
|
+
class Game_Over < T_BAG::Scene
|
32
|
+
# Returns an instance of a special Game Over scene.
|
33
|
+
def initialize
|
34
|
+
super(:endgame, 'Game Over')
|
35
|
+
end
|
36
|
+
|
37
|
+
# Run with the same behavior as a scene.
|
38
|
+
# @return [Void]
|
39
|
+
def run
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
=begin
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2015 Benjamin Meyers as Lion Logic <lion.logic.org@gmail.com>
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
23
|
+
=end
|
24
|
+
|
25
|
+
require_relative '../tbag'
|
26
|
+
|
27
|
+
require 'docile'
|
28
|
+
|
29
|
+
module T_BAG
|
30
|
+
# Defines a special Main Menu scene and its run behavior.
|
31
|
+
class Main_Menu < T_BAG::Scene
|
32
|
+
# Returns an instance of a special Main Menu scene.
|
33
|
+
def initialize
|
34
|
+
super(:menu, 'Main Menu')
|
35
|
+
end
|
36
|
+
|
37
|
+
# Run with the same behavior as a scene.
|
38
|
+
def run
|
39
|
+
super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/tbag/prompt.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
=begin
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2015 Benjamin Meyers as Lion Logic <lion.logic.org@gmail.com>
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
23
|
+
=end
|
24
|
+
|
25
|
+
require_relative '../tbag'
|
26
|
+
|
27
|
+
require 'docile'
|
28
|
+
|
29
|
+
module T_BAG
|
30
|
+
# Defines a prompt and its run behavior.
|
31
|
+
class Prompt
|
32
|
+
# @param [String] question the prompt for the User
|
33
|
+
# @param [ANY] type the type of the expected User input
|
34
|
+
# @param [String] error the error to display if User input is invalid
|
35
|
+
def initialize(question, type, error)
|
36
|
+
@question = question
|
37
|
+
@type = type
|
38
|
+
@choices = {}
|
39
|
+
@error = error
|
40
|
+
end
|
41
|
+
|
42
|
+
# Capture the User's answer to a prompt
|
43
|
+
# @param [String] value the User input
|
44
|
+
# @param [Block] block
|
45
|
+
def choice(value, &block)
|
46
|
+
if value.is_a? String
|
47
|
+
value.downcase!
|
48
|
+
end
|
49
|
+
@choices[value] = Proc.new &block
|
50
|
+
end
|
51
|
+
|
52
|
+
# Define how to interpret prompt input.
|
53
|
+
def run
|
54
|
+
puts @question
|
55
|
+
done = false
|
56
|
+
until done
|
57
|
+
answer = gets.strip
|
58
|
+
|
59
|
+
if @type == Integer
|
60
|
+
unless answer.to_i == 0
|
61
|
+
answer = answer.to_i
|
62
|
+
end
|
63
|
+
elsif @type == String
|
64
|
+
answer.downcase!
|
65
|
+
end
|
66
|
+
if answer.is_a? @type and (@choices[answer] or @choices[:any])
|
67
|
+
if @choices[answer]
|
68
|
+
@choices[answer].call(answer)
|
69
|
+
else
|
70
|
+
@choices[:any].call(answer)
|
71
|
+
end
|
72
|
+
done = true
|
73
|
+
else
|
74
|
+
puts @error
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/lib/tbag/scene.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
=begin
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2015 Benjamin Meyers as Lion Logic <lion.logic.org@gmail.com>
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
23
|
+
=end
|
24
|
+
|
25
|
+
module T_BAG
|
26
|
+
# Defines a scene and its run behavior.
|
27
|
+
class Scene
|
28
|
+
# @param [String] name the name of the scene
|
29
|
+
# @param [String] title the title of the scene
|
30
|
+
def initialize(name, title)
|
31
|
+
@name = name
|
32
|
+
@title = title
|
33
|
+
@actions = []
|
34
|
+
end
|
35
|
+
|
36
|
+
# Add a pause to the action queue.
|
37
|
+
def pause
|
38
|
+
@actions << {type: :pause}
|
39
|
+
end
|
40
|
+
|
41
|
+
# Add a scene change to the action queue.
|
42
|
+
# @param [Symbol] scene a scene
|
43
|
+
def scene_change( scene )
|
44
|
+
@actions << {type: :scene_change, scene: scene}
|
45
|
+
end
|
46
|
+
|
47
|
+
# Add a set of text to the action queue.
|
48
|
+
# @param [String] dialog the text to be output to the User
|
49
|
+
def text(dialog)
|
50
|
+
@actions << {type: :text, dialog: dialog}
|
51
|
+
end
|
52
|
+
|
53
|
+
# Add a prompt to the action queue.
|
54
|
+
# @param [String] question the prompt for the User
|
55
|
+
# @param [ANY] type the type of the expected User input
|
56
|
+
# @param [String] error the error to display if User input is invalid
|
57
|
+
# @param [Block] block
|
58
|
+
def prompt(question, type, error, &block)
|
59
|
+
prompt = T_BAG::Prompt.new(question, type, error)
|
60
|
+
@actions << {type: :prompt, prompt: prompt}
|
61
|
+
Docile.dsl_eval(prompt, &block)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Add a save to the action queue.
|
65
|
+
# @param [String] outfile the name of the file to save to
|
66
|
+
def save_game(outfile)
|
67
|
+
@actions << {type: :save, filename: outfile}
|
68
|
+
end
|
69
|
+
|
70
|
+
# Add a load to the action queue.
|
71
|
+
# @param [String] infile the name of the file to load from
|
72
|
+
def load_game(infile)
|
73
|
+
@actions << {type: :load, filename: infile}
|
74
|
+
end
|
75
|
+
|
76
|
+
# Create a scene change for the game over scene.
|
77
|
+
def game_over
|
78
|
+
scene_change :endgame
|
79
|
+
end
|
80
|
+
|
81
|
+
# Create a scene change for the main menu scene.
|
82
|
+
def main_menu
|
83
|
+
scene_change :menu
|
84
|
+
end
|
85
|
+
|
86
|
+
# Define how a scene runs.
|
87
|
+
def run
|
88
|
+
@actions.each do |a|
|
89
|
+
case a[:type]
|
90
|
+
when :pause
|
91
|
+
print 'Continue...'
|
92
|
+
gets
|
93
|
+
puts
|
94
|
+
when :scene_change
|
95
|
+
$game.next_scene a[:scene]
|
96
|
+
when :text
|
97
|
+
a[:dialog].bytes.each do |c|
|
98
|
+
putc c
|
99
|
+
sleep(0.05)
|
100
|
+
end
|
101
|
+
putc "\n"
|
102
|
+
when :prompt
|
103
|
+
a[:prompt].run
|
104
|
+
when :save
|
105
|
+
$game.save_game a[:filename]
|
106
|
+
when :load
|
107
|
+
$game.load_game a[:filename]
|
108
|
+
else
|
109
|
+
$stderr.puts '[SCENE] Action not allowed'
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: t-bag
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Benjamin S. Meyers
|
8
|
+
- Bryan T. Meyers
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-08-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: docile
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.1'
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.1.5
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - "~>"
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '1.1'
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.5
|
34
|
+
description: A framework for implementing domain-specific languages for text-based
|
35
|
+
adventure games.
|
36
|
+
email:
|
37
|
+
- lion.logic.org@gmail.com
|
38
|
+
- bmeyers@datadrake.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- examples/sample.rb
|
44
|
+
- lib/tbag.rb
|
45
|
+
- lib/tbag/game.rb
|
46
|
+
- lib/tbag/game_over.rb
|
47
|
+
- lib/tbag/main_menu.rb
|
48
|
+
- lib/tbag/prompt.rb
|
49
|
+
- lib/tbag/scene.rb
|
50
|
+
homepage: https://github.com/meyersbs/t-bag
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.2.2
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Text-Based Adventure Game Framework
|
74
|
+
test_files: []
|
75
|
+
has_rdoc:
|