Created
December 30, 2010 03:44
-
-
Save JulesWang/759427 to your computer and use it in GitHub Desktop.
A ruby-warrior GUI based on Shoes, under development
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#run: shoes this-file.rb | |
require 'stringio' | |
require "./ruby_warrior/lib/ruby_warrior.rb" | |
$wait_input = false | |
# Name Show | |
$game_name = "Ruby Warrior!" | |
# Absolut coordinate of the Map | |
$map_coord = [80,120] | |
# Size of grids in the map | |
$grid_size = 30 | |
# Constant in the game | |
HealthMe = 20 | |
HealthFoe = {"Sludge"=>12, "Thick Sludge"=>24, "Archer"=>7} | |
class MyStringIO < StringIO | |
# Make the gets waiting for input | |
def gets | |
$wait_input = true | |
begin | |
str = super | |
end while (str == nil) | |
self.string = "" | |
$wait_input = false | |
return str | |
end | |
end | |
def run | |
runner = RubyWarrior::Runner.new(ARGV, @input, @output) | |
runner.run | |
end | |
Shoes.app(:title => $game_name) do | |
background azure | |
# init | |
@output = StringIO.new | |
@input = MyStringIO.new | |
@status = 0 | |
@game = nil | |
@map = nil | |
@name = nil | |
@difficulty = nil | |
@health_me = HealthMe | |
@health_foe = 0 | |
@foe_name = nil | |
# Button Ctrls at the top | |
@ctl = flow do | |
@start_btn = button "Start/Restart" do | |
if (@game && @game.alive?) | |
@game.exit | |
@status = 0 | |
@output.string = "" | |
@name.string = "" | |
@map.remove if @map | |
@body.clear | |
@head.clear{title $game_name} | |
end | |
# Start Game Engine Thread | |
@game = Thread.new {run} | |
end | |
@edit_btn = button "Edit Script" do | |
return if @name == nil | |
Thread.new { system "gvim .\\rubywarrior\\#{@name}-#{@difficulty}\\player.rb" } | |
end | |
@readme_btn = button "ReadMe" do | |
return if @name == nil | |
Thread.new { system "gvim .\\rubywarrior\\#{@name}-#{@difficulty}\\readme" } | |
end | |
end | |
# Level Caption | |
@head = stack :margin => 10 do | |
title $game_name | |
end | |
# Map | |
@body = stack :margin => 10 do | |
end | |
@health_bar = flow do | |
@para_name = para @name | |
@prog_me = progress :width => 100 | |
@para_health_me = para @health_me.to_i | |
para " " | |
@para_foe_name = para @foe_name | |
@prog_foe = progress :width => 100 | |
@para_health_foe = para @health_foe.to_i | |
end | |
# Fight log | |
@info = stack :margin => 10, :height => 100, :scroll => true do | |
end | |
@info.height = 0; | |
@user_input = stack :margin => 10 do | |
@input_ctl = edit_line "1" | |
button "Send" do | |
@input.string = @input_ctl.text | |
@body.clear | |
@status += 1; | |
@health_bar.show | |
if @options | |
@name = @options[@input_ctl.text.to_i-1].split("-")[0][3..-1].strip | |
@difficulty = @options[@input_ctl.text.to_i-1].split("-")[1].strip | |
@para_name.text = @name.to_s | |
end | |
end | |
end | |
@user_input.hide | |
@health_bar.hide | |
animate do |i| | |
@prog_me.fraction = @health_me/HealthMe.to_f | |
@prog_foe.fraction = @health_foe/(HealthFoe[@foe_name].to_f) | |
end | |
# Update | |
every do | |
@output.rewind | |
if (@output.string != "") | |
@options = nil | |
if @status == 1 | |
parseInfo(@output.string) | |
else | |
@body.append{para @output.string} | |
@options = @output.string.split("\n") | |
@options.shift | |
end | |
@output.string = "" | |
end | |
if $wait_input | |
@user_input.show | |
@info.height = 0 | |
else | |
@user_input.hide | |
@info.height = 100 | |
end | |
end | |
def parseInfo(str) | |
lines = str.split("\n") | |
if (lines[0] =~ /^Starting Level \d+$/) == 0 | |
level = lines[0].split(' ')[2].to_i | |
@head.clear{title ("Level " + level.to_s + @name)} | |
lines.shift | |
end | |
map = [] | |
log = [] | |
lines.each do |line| | |
if ( (line =~ /^- turn \d+ -$/) == 0 || | |
(line =~ /^ -/) == 0 || | |
(line =~ /^\|/) == 0 ) | |
map << line | |
else | |
log << line | |
words = line.split(" ") | |
if words[0] == @name | |
i = words.reverse.index("health") | |
@health_me = words.reverse[i+1].to_i if i | |
@para_health_me.text = @health_me.to_s | |
elsif words[0] && words[0][0] < 'a' # the first char is capitalize | |
@old_foe_name = @foe_name | |
@foe_name = words[0] | |
if words[1][0] < 'a' | |
@foe_name << " " << words[1] | |
end | |
if words[1] == "dies" | |
@old_foe_name = nil | |
end | |
if @old_foe_name && @old_foe_name != @foe_name | |
@health_foe = HealthFoe[@foe_name] | |
end | |
i = words.reverse.index("health") | |
@health_foe = words.reverse[i+1].to_i if i | |
@para_foe_name.text = @foe_name | |
@para_health_foe.text = @health_foe | |
end | |
end | |
end | |
@body.clear{para map.join("\n")} if map.size > 0 | |
@info.append{para log.join("\n")} if log.size > 0 | |
@info.scroll_top = @info.scroll_max | |
drawMap(map) if map.size > 0 | |
end | |
def drawMap(map) | |
nostroke | |
if @map | |
@map.remove | |
end | |
map.shift | |
@map = image :top => 0, :left => 0 do | |
map.each_with_index do |line, i| | |
debug line | |
j = 0 | |
line.each_char do |char| | |
left = $map_coord[0] + ($grid_size * j) | |
top = $map_coord[1] + ($grid_size * i) | |
case char | |
when '-', '|' | |
fill gray | |
rect left, top, $grid_size, $grid_size | |
when '@' | |
fill blue | |
star left+$grid_size/2, top+$grid_size/2, 5, $grid_size/4 , $grid_size/2 | |
when '>' | |
fill green | |
rect left, top, $grid_size, $grid_size | |
when 's', 'S' | |
fill red | |
oval left, top, $grid_size, $grid_size | |
when 'a' | |
fill green | |
oval left, top, $grid_size, $grid_size | |
else | |
fill white | |
rect left, top, $grid_size, $grid_size | |
end | |
j = j+1; | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment