Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #A Game of Rooms revision 0.7
- #Biggest known glitch: using an inventory item deletes all identical items.
- #declarations
- # Each room is an array like this: [x, y, z, description, items, monster, traps]
- Facility = [
- [0,0,0, "The sign says this is room one. There's a ladder leading to the ceiling.", "bottle", "troll", nil],
- [0,0,1, "This is room two. There's a trap door in the floor.", nil, "dragon", "endportal"],
- [0,0,2, "This is room three. There's a wolf head on the wall.", "can", nil, nil]
- ]
- #facilityMap is a hash containing all the rooms. Their xyz locations are
- #associated with an array containing the rest.
- $facilityMap = {}
- #Probably a really dumb way to do this, but these are whitelists of
- #allowed commands.
- $movementVars = ["up", "down", "left", "right", "forward", "back"]
- $roomCommands = ["use", "eat", "sleep", "fight", "look", "say", "take", "get",
- "remember", "where", "weapon", "life", "equip", "points"]
- #Attaches the coordinates of each room to their other attributes through the
- #easily accessible $facilityMap hash.
- class Room
- def initialize( x, y, z, description, items, monster, fixtures )
- coords = [x, y, z]
- $facilityMap[coords] = [description, items, monster, fixtures]
- end
- end
- #All players are Inmates. This class is where I put things players have or do,
- #like movement, commands, life, coordinates, inventory, points, and your weapon.
- class Inmate
- attr_accessor :weapon, :life, :coordinates, :inventory, :points
- #Sets starting conditions for the player.
- def initialize
- @points = 0
- @zCoord = 0
- @yCoord = 0
- @xCoord = 0
- @coordinates = [@xCoord, @yCoord, @zCoord]
- @inventory = []
- @weapon = [ "a gun" , 10 ]
- @life = 100
- end
- #The Inmate.doCommand method executes the player's non-movement commands.
- def doCommand( command )
- #The main loop sends an array here composed of the player's input broken
- #into strings for each word. First, doCommand checks to see if it's a movement
- #variable, and if it is, sends it to the player movement method.
- if $movementVars.include? command[0]
- return move(command[0])
- else
- #I've heard nesting conditionals is bad form. Whoops. Mark this one
- #as a prime location for cleanup.
- case command[0]
- when "help"
- puts "You can only use the following commands."
- puts $movementVars, $roomCommands
- when "points"
- puts "You have #{ @points } points."
- when "look"
- puts $facilityMap[@coordinates][0]
- when "where"
- puts @coordinates.to_s
- when "use"
- #This part is buggy.
- if @inventory.include? command[1]
- puts "You decide to use the #{ command[1] }."
- #Having checked to make sure the item is in the player's inventory,
- #the Inmate.use method will execute whatever process that inventory
- #object can do.
- return use( command[1] )
- else
- #Ain't I a stinker.
- puts "Use what?"
- end
- #Proof of concept for synonyms. I'll implement more later.
- when "take"
- return doCommand(["get", command[1]])
- #Remember that commands enter the hash as an array [command[1], command[2]]
- when "get"
- item = command[1]
- if $facilityMap[@coordinates][1] == item
- puts "You got the #{ item }!"
- @inventory << $facilityMap[@coordinates][1]
- $facilityMap[@coordinates][1] = nil
- else
- puts "Get what?"
- end
- #Prints player's life
- when "life"
- puts @life
- #Prints inventory
- when "inventory"
- if @inventory == []
- puts "Your pockets are empty."
- else
- print "You have: \n"
- @inventory.each { |item| print item + " "; }
- puts " "
- end
- #The weapon array contains two objects: a string (the weapon's name) and an integer (how much damage it does.)
- when "weapon"
- puts "You have #{ @weapon[0]} which does #{ @weapon[1] } damage."
- else
- puts "I don't know what you're trying to say."
- end
- end
- end
- #player movement system.checks to see if there's a room and if there is, alters coordinates accordingly.
- #TODO prettify if possible.
- def move( direction )
- disposablexCoord = @xCoord
- disposableyCoord = @yCoord
- disposablezCoord = @zCoord
- axes = {"up" => "disposablezCoord += 1", "down" => "disposablezCoord -= 1",
- "right" => "disposablexCoord += 1", "left" => "disposablexCoord -= 1",
- "forward" => "disposableyCoord += 1", "back" => "disposableyCoord -= 1"}
- eval axes[direction]
- disposableCoords = [disposablexCoord, disposableyCoord, disposablezCoord]
- if $facilityMap.key?(disposableCoords)
- @xCoord = disposablexCoord
- @yCoord = disposableyCoord
- @zCoord = disposablezCoord
- @coordinates = [@xCoord, @yCoord, @zCoord]
- puts "The door opens easily."
- else
- puts "You can't go #{ direction } here."
- end
- end
- def use( item )
- case item
- when "can"
- @inventory.delete "can"
- puts "You eat the beans and feel refreshed."
- puts "Life = 100"
- @life = 100
- when "laser"
- @inventory.delete "laser"
- puts "You equip the laser. It does lots more damage."
- @weapon = ["a laser", 20]
- @laserVersion = 1
- when "charger"
- if @weapon[0] == "a laser"
- puts "You increase the power of your laser by 10!"
- @weapon[1] += 10
- @laserVersion += 1
- puts "Laser level #{ @laserVersion}!"
- @inventory.delete "charger"
- else
- puts "You need a laser first!"
- end
- when "bottle"
- if $facilityMap[coordinates][3] == "endportal"
- puts "You explode into a shower of #{ @points } points!"
- abort "You win!"
- else
- puts "You can't use the bottle yet!"
- end
- end
- end
- end
- class Monster
- attr_accessor :coordinates, :weapon, :life, :points
- def initialize( player )
- @monsterKind = $facilityMap[player.coordinates][2]
- if @monsterKind
- case @monsterKind
- when "dragon"
- @monsterLife = 30
- @monsterAttack = 10
- roll = rand(2)
- if roll == 0
- @drop = "can"
- else
- @drop = "charger"
- spawnProb = 3
- end
- when "troll"
- @monsterLife = 15
- @monsterAttack = 20
- roll = rand(2)
- if roll == 0
- @drop = "gold"
- else
- @drop = "laser"
- spawnProb = 10
- end
- else
- raise Error "Nonexistent monster!"
- end
- if @monsterKind && rand(spawnProb) == 0
- puts "A terrible #{ @monsterKind } appears!"
- puts "Will you fight him or run?!"
- puts "> "
- input = gets.chomp
- if $movementVars.include? input
- puts "You escaped!"
- player.move(input)
- else
- return fight player
- end
- end
- else
- return true
- end
- end
- def fight( player )
- #The fight is a loop that runs till one of you is dead.
- while @monsterLife > 0
- puts "You duke it out and stuff."
- player.life -= @monsterAttack
- puts "The #{ @monsterKind } attacks for #{ @monsterAttack } damage!"
- if player.life > 0
- @monsterLife =- player.weapon[1]
- puts "You attack the #{ @monsterKind } with #{ player.weapon[0] } for #{ player.weapon[1]} damage!"
- else
- abort "You died like an asshole."
- puts "You had #{ player.points } points."
- break
- end
- #Here's the points formula, open to change.
- points = -10 * ( @monsterLife * @monsterAttack ) / player.life
- puts "You killed the #{ @monsterKind }!"
- puts "You got #{ points } points!"
- player.points += points
- player.inventory << @drop
- puts "You got a #{ @drop }!"
- end
- end
- end
- #Controls item spawning in rooms.
- class Item
- attr_accessor :coordinates, :life
- def initialize( player )
- item = $facilityMap[player.coordinates][1]
- if item
- puts "There is a #{ item } here."
- else
- return true
- end
- end
- end
- #Eventual fixtures will include traps, buttons, machines, and other stuff built into rooms.
- #Winning conditions will involve fixtures.
- class Fixture
- #TODO your mom
- end
- #Setting up for the main loop. Gotta have a player, and the Facility constant must be loaded
- #into memory as its constituent rooms.
- player = Inmate.new
- Facility.each{ |x, y, z, desc, items, monsters, traps|
- Room.new(x, y, z, desc, items, monsters, traps)}
- puts "You wake up in a cold white room."
- #The main loop. Pretty simple: First it checks to see if there's an item. Then it rolls to spawn
- #a monster. Finally, it takes input from the player, which it splits up and parcels into the
- #Inmate.doCommand method.
- while true
- Item.new(player)
- Monster.new( player )
- print "> "
- input = gets.chomp.downcase.split(" ").to_a
- player.doCommand(input)
- end
- #This crime against code was written by Robert Gryfft.
- #CC BY-SA 3.0
- #http://creativecommons.org/licenses/by-sa/3.0/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement