-
Notifications
You must be signed in to change notification settings - Fork 6
/
run.rb
101 lines (80 loc) · 2.81 KB
/
run.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
92
93
94
95
96
97
98
99
100
101
require 'engine/setup.rb'
Shoes.app(
:title => '',
:width => 300,
:height => 170,
:resizable => true
) {
# Main loop of game
def main(game_selector_window, app_base_path, game_path, config)
Shoes.app(
:title => config['title'],
:width => config['width'],
:height => config['height'],
:resizable => config['resizable']
) {
# Close game selector
game_selector_window.close
if (config['background'])
background config['background']
end
# Initialize image display area
@image_stack = stack :background => white,
:width => config['width'],
:height => config['image_height']
# Initialize output area
@output_stack = stack :scroll => true,
:width => config['width'],
:height => (config['height'] - config['image_height'])
# Initialize game
@game = Game.new(config, app_base_path, game_path)
# Initialize CLI (showing/hiding the output stack fixes a platform-specific issue)
@cli = Cli.new :output_stack => @output_stack,
:image_stack => @image_stack,
:game => @game,
:initial_text => config['startup_message'],
:standard_commands => config['standard_commands'],
:command_abbreviations => config['command_abbreviations'],
:garbage_words => config['garbage_words'],
:global_synonyms => config['global_synonyms']
if config['command_condition']
@cli.command_condition = config['command_condition']
end
# Allow startup logic to be supplied as raw logic and/or a file
custom_startup_logic = false
# Include start logic from file, if one exists
path_to_startup_logic = game_path + '/startup_logic.rb'
if File.file?(path_to_startup_logic)
instance_eval(File.read(path_to_startup_logic))
custom_startup_logic = true
end
# Include startup logic form config, if specified
if config['startup_logic']
instance_eval(config['startup_logic'])
custom_startup_logic = true
end
# if no startup logic has been specified, issue 'look' command
if !custom_startup_logic
@cli.issue_command('look', false)
end
# Display CLI prompt
@cli.display_prompt
# Process keystrokes
keypress do |k|
@cli.keystroke(k)
if @game.over
if @game.score
@cli.output_add(@game.score_total(@game.turns))
@cli.display_prompt
end
@game.restart_or_exit
@cli.reset
end
end
}
end
# Application path is the directory in which this file lives
app_base_path = File.expand_path(File.dirname(__FILE__))
# Allow player to select game, if applicable, before proceeding to main loop
game_selector(app, app_base_path)
}