-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instructions print via game #32
base: master
Are you sure you want to change the base?
Changes from all commits
e0eb026
c42cd47
ebe2aed
3f02d02
73fbb03
7dba848
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,12 @@ class Game | |
attr_reader :flags, :counters, :saved_rooms, :noun #:nodoc: | ||
attr_accessor :loc, :counter, :saved_room, :lampleft #:nodoc: | ||
|
||
private | ||
attr_reader :input, :output | ||
|
||
# Creates a new game, with no room, items or actions -- load must | ||
# be called to make the game ready for playing, or | ||
# compile_to_stdout can be called to generate a new game-file. | ||
# be called to make the game ready for playing, or compile can be | ||
# called to generate a new game-file. | ||
# | ||
# The options hash affects various aspects of how the game will be | ||
# loaded, played and compiled. The following symbols are | ||
# recognised as keys in the options hash: | ||
|
@@ -44,10 +45,18 @@ class Game | |
# file to restore before starting to play. | ||
# | ||
# [+read_file+] If specified, the name of a file of game | ||
# commands to be run after restoring s saved | ||
# commands to be run after restoring a saved | ||
# game (if any) and before starting to read | ||
# commands from the user. | ||
# | ||
# [+output+] If specified, a writable IO stream for | ||
# capturing the game's output. Defaults to | ||
# <tt>$stdout</tt> if nothing is provided. | ||
# | ||
# [+input+] If specified, a readable IO stream for | ||
# capturing the game's input. Defaults to | ||
# <tt>$stdin</tt> if nothing is provided. | ||
# | ||
# [+echo_input+] If true, then game commands are echoed | ||
# before being executed. This is useful | ||
# primarily if input is being redirected | ||
|
@@ -106,8 +115,25 @@ def initialize(options) | |
@options = options | ||
@rooms, @items, @actions, @nouns, @verbs, @messages = | ||
[], [], [], [], [], [] | ||
@input = options[:input] || $stdin | ||
@output = options[:output] || $stdout | ||
end | ||
|
||
def puts *args | ||
output.puts(*args) | ||
end | ||
|
||
def print *args | ||
output.print(*args) | ||
end | ||
|
||
# Print debugging output | ||
def dputs(level, *args) #:nodoc: | ||
super.puts args.map { |x| "# #{x}" } if @options[level] | ||
end | ||
|
||
private | ||
|
||
# Virtual accessor | ||
def dark_flag #:nodoc: | ||
@flags[FLAG_DARK] | ||
|
@@ -261,10 +287,6 @@ def restore(name) | |
@items.each { |item| item.loc = f.gets.to_i } | ||
end | ||
|
||
def dputs(level, *args) #:nodoc: | ||
puts args.map { |x| "# #{x}" } if @options[level] | ||
end | ||
|
||
# Compiles the specified game-source file, writing the resulting | ||
# object file to stdout, whence it should be redirected into a | ||
# file so that it can be played. Yes, this API is sucky: it would | ||
|
@@ -283,14 +305,12 @@ def dputs(level, *args) #:nodoc: | |
# function is that its behaviour is influenced by the game's | ||
# options.) | ||
# | ||
def compile_to_stdout(filename, fh = nil) | ||
compiler = ScottKit::Game::Compiler.new(self, filename, fh) | ||
compiler.compile_to_stdout | ||
def compile(out, filename, fh = nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not in love with this argument ordering but it felt like the least bad option considering the optional There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not following. What would that look like? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll respond on the main thread to keep things centrally visible. |
||
ScottKit::Game::Compiler.new(self, filename, fh).compile_to(out) | ||
end | ||
|
||
public :load, :compile_to_stdout # Must be visible to driver program | ||
public :load, :compile # Must be visible to driver program | ||
public :roomname, :itemname # Needed by Condition.render() | ||
public :dputs # Needed for contained classes' debugging output | ||
public :dirname # Needed by compiler | ||
public :dark_flag= # Invoked from Instruction.execute() | ||
|
||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about overriding
print
ingame.rb
. Isn't the more honest approach you used in the previous commit also suitable here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ... but you're not overriding
print
, are you? You're just defining a method with the same name as the underlying primitive that it wraps -- so the invocation changes fromprint(x)
to@game.print(x)
. OK, that makes perfect sense. Carry on :-)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah kind of funky to have to do this but it's either this or passing the output object down to the instructions. This seemed cleaner and having it centralized would give you one place to hook in a driver.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, this is good.