diff options
author | Nat Lasseter <Nat Lasseter nathan@bytemark.co.uk> | 2018-04-04 21:54:03 +0100 |
---|---|---|
committer | Nat Lasseter <Nat Lasseter nathan@bytemark.co.uk> | 2018-04-04 21:54:03 +0100 |
commit | c7f7a60a7f1175696b8239355dc7737532c1f1ce (patch) | |
tree | fe5759a74286560e0da63208fdb0424098fe810c /proto |
Diffstat (limited to 'proto')
-rw-r--r-- | proto/game.rb | 109 | ||||
-rw-r--r-- | proto/rooms.json | 25 | ||||
-rw-r--r-- | proto/stuff.json | 9 |
3 files changed, 143 insertions, 0 deletions
diff --git a/proto/game.rb b/proto/game.rb new file mode 100644 index 0000000..68f1198 --- /dev/null +++ b/proto/game.rb @@ -0,0 +1,109 @@ +require 'json' + +class Array + def second; return self[1]; end + def third; return self[2]; end + def rest; return self[1..-1]; end +end + +$stuff = JSON.parse(File.read("stuff.json")) +$rooms = JSON.parse(File.read("rooms.json")) +$inventory = [] +$room = "start" +$energy = 10 +$light = false + +def look + if $rooms[$room]["dark"] and !$light then + puts "It's too dark, you can't see anything." + else + puts $rooms[$room]["summary"] + things = $rooms[$room]["inventory"] + if things.length.zero? then + puts "You see nothing." + else + puts "You see:" + things.each do |thing| + puts "\t#{thing}" + end + end + end +end + +def handle(line) + a = line.split + case a.first + when "look" + if a.second == "me" || a.second == "self" then + puts "A daring adventurer." + puts "HP: #{$energy}" + handle "inventory" + else + look + end + when "take", "get" + if $rooms[$room]["inventory"].include?(a.second) then + $inventory << a.second + $rooms[$room]["inventory"].delete(a.second) + puts "You take the #{a.second}." + else + puts "You can't see a(n) #{a.second}." + end + when "north", "south", "east", "west" + handle "go #{a.first}" + when "go" + $room = $rooms[$room][a.second] + look + when "inventory" + if $inventory.length.zero? then + puts "You have nothing." + else + puts "You have:" + $inventory.each do |thing| + puts "\t#{thing}" + end + end + when "light" + if $inventory.include?(a.second) then + if $stuff[a.second]["light"] then + puts "You light the #{a.second}." + $light = true + else + puts "You can't light that." + end + else + puts "You don't have a(n) #{a.second} to light." + end + when "snuff" + if $inventory.include?(a.second) then + if $stuff[a.second]["light"] then + puts "You snuff the #{a.second}." + $light = false + else + puts "You can't light that, let alone snuff it." + end + else + puts "You don't have a(n) #{a.second} to snuff." + end + when "eat" + if $inventory.include?(a.second) then + if $stuff[a.second]["edible"] then + $energy += $stuff[a.second]["energy"] + puts "A tasty #{a.second}" + $inventory.delete(a.second) + else + puts "You can't eat that." + end + else + puts "You don't have a(n) #{a.second} to eat." + end + end +end + +look + +loop do + print ">: " + line = gets.strip.downcase + handle(line) +end diff --git a/proto/rooms.json b/proto/rooms.json new file mode 100644 index 0000000..a6d0cd7 --- /dev/null +++ b/proto/rooms.json @@ -0,0 +1,25 @@ +{ + "start": { + "summary": "A bland, starting room.", + "inventory": [ + "apple", + "lamp" + ], + "north": "another", + "south": "another", + "east": "start", + "west": "start" + }, + "another": { + "summary": "Another room.", + "dark": true, + "inventory": [ + "apple", + "apple" + ], + "north": "start", + "south": "start", + "east": "another", + "west": "another" + } +} diff --git a/proto/stuff.json b/proto/stuff.json new file mode 100644 index 0000000..e665b6f --- /dev/null +++ b/proto/stuff.json @@ -0,0 +1,9 @@ +{ + "apple": { + "edible": true, + "energy": 2 + }, + "lamp": { + "light": true + } +} |