Advertisement
Guest User

A Game of Rooms 0.7

a guest
Mar 27th, 2012
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.46 KB | None | 0 0
  1. #A Game of Rooms revision 0.7
  2. #Biggest known glitch: using an inventory item deletes all identical items.
  3.  
  4.  
  5. #declarations
  6.  
  7. # Each room is an array like this: [x, y, z, description, items, monster, traps]
  8. Facility = [
  9.   [0,0,0, "The sign says this is room one. There's a ladder leading to the ceiling.", "bottle", "troll", nil],
  10.   [0,0,1, "This is room two. There's a trap door in the floor.", nil, "dragon", "endportal"],
  11.   [0,0,2, "This is room three. There's a wolf head on the wall.", "can", nil, nil]
  12.   ]
  13.  
  14. #facilityMap is a hash containing all the rooms. Their xyz locations are
  15. #associated with an array containing the rest.
  16. $facilityMap = {}
  17.  
  18. #Probably a really dumb way to do this, but these are whitelists of
  19. #allowed commands.  
  20. $movementVars = ["up", "down", "left", "right", "forward", "back"]
  21. $roomCommands = ["use", "eat", "sleep", "fight", "look", "say", "take", "get",
  22.                  "remember", "where", "weapon", "life", "equip", "points"]
  23.  
  24. #Attaches the coordinates of each room to their other attributes through the
  25. #easily accessible $facilityMap hash.
  26. class Room
  27.   def initialize( x, y, z, description, items, monster, fixtures )
  28.     coords = [x, y, z]
  29.     $facilityMap[coords] = [description, items, monster, fixtures]
  30.   end
  31. end
  32.  
  33. #All players are Inmates. This class is where I put things players have or do,
  34. #like movement, commands, life, coordinates, inventory, points, and your weapon.
  35. class Inmate
  36.  
  37.   attr_accessor :weapon, :life, :coordinates, :inventory, :points
  38.  
  39.   #Sets starting conditions for the player.
  40.   def initialize
  41.     @points = 0
  42.     @zCoord = 0
  43.     @yCoord = 0
  44.     @xCoord = 0
  45.     @coordinates = [@xCoord, @yCoord, @zCoord]
  46.     @inventory = []
  47.     @weapon = [ "a gun" , 10 ]
  48.     @life = 100
  49.   end
  50.  
  51.   #The Inmate.doCommand method executes the player's non-movement commands.
  52.   def doCommand( command )
  53.     #The main loop sends an array here composed of the player's input broken
  54.     #into strings for each word. First, doCommand checks to see if it's a movement
  55.     #variable, and if it is, sends it to the player movement method.
  56.     if $movementVars.include? command[0]
  57.       return move(command[0])
  58.     else
  59.       #I've heard nesting conditionals is bad form. Whoops. Mark this one
  60.       #as a prime location for cleanup.
  61.      
  62.       case command[0]        
  63.       when "help"
  64.         puts "You can only use the following commands."
  65.         puts $movementVars, $roomCommands  
  66.       when "points"
  67.         puts "You have #{ @points } points."
  68.       when "look"
  69.         puts $facilityMap[@coordinates][0]
  70.       when "where"
  71.         puts @coordinates.to_s
  72.       when "use"
  73.        
  74.         #This part is buggy.
  75.         if @inventory.include? command[1]
  76.           puts "You decide to use the #{ command[1] }."
  77.           #Having checked to make sure the item is in the player's inventory,
  78.           #the Inmate.use method will execute whatever process that inventory
  79.           #object can do.
  80.           return use( command[1] )
  81.         else
  82.           #Ain't I a stinker.
  83.           puts "Use what?"
  84.         end
  85.        
  86.         #Proof of concept for synonyms. I'll implement more later.
  87.       when "take"
  88.         return doCommand(["get", command[1]])
  89.        
  90.         #Remember that commands enter the hash as an array [command[1], command[2]]
  91.       when "get"
  92.         item = command[1]
  93.         if $facilityMap[@coordinates][1] == item
  94.           puts "You got the #{ item }!"
  95.           @inventory << $facilityMap[@coordinates][1]
  96.           $facilityMap[@coordinates][1] = nil
  97.         else
  98.            puts "Get what?"
  99.         end
  100.        
  101.         #Prints player's life
  102.       when "life"
  103.         puts @life
  104.        
  105.        
  106.        #Prints inventory
  107.       when "inventory"
  108.         if @inventory == []
  109.           puts "Your pockets are empty."
  110.         else
  111.           print "You have: \n"
  112.           @inventory.each { |item| print item + " "; }
  113.           puts " "
  114.         end      
  115.        
  116.        
  117.       #The weapon array contains two objects: a string (the weapon's name) and an integer (how much damage it does.)
  118.       when "weapon"
  119.         puts "You have #{ @weapon[0]} which does #{ @weapon[1] } damage."
  120.      
  121.       else
  122.         puts "I don't know what you're trying to say."
  123.       end
  124.     end
  125.   end
  126.    
  127.   #player movement system.checks to see if there's a room and if there is, alters coordinates accordingly.
  128.   #TODO prettify if possible.
  129.  
  130.   def move( direction )
  131.     disposablexCoord = @xCoord
  132.     disposableyCoord = @yCoord
  133.     disposablezCoord = @zCoord
  134.     axes = {"up" => "disposablezCoord += 1", "down" => "disposablezCoord -= 1",
  135.             "right" => "disposablexCoord += 1", "left" => "disposablexCoord -= 1",
  136.             "forward" => "disposableyCoord += 1", "back" => "disposableyCoord -= 1"}
  137.     eval axes[direction]  
  138.     disposableCoords = [disposablexCoord, disposableyCoord, disposablezCoord]
  139.  
  140.         if $facilityMap.key?(disposableCoords)
  141.           @xCoord = disposablexCoord
  142.           @yCoord = disposableyCoord
  143.           @zCoord = disposablezCoord
  144.           @coordinates = [@xCoord, @yCoord, @zCoord]
  145.           puts "The door opens easily."
  146.         else
  147.           puts "You can't go #{ direction } here."
  148.         end
  149.     end
  150.  
  151.     def use( item )
  152.       case item
  153.       when "can"
  154.         @inventory.delete "can"
  155.         puts "You eat the beans and feel refreshed."
  156.         puts "Life = 100"
  157.         @life = 100
  158.       when "laser"
  159.         @inventory.delete "laser"
  160.         puts "You equip the laser. It does lots more damage."
  161.         @weapon = ["a laser", 20]
  162.         @laserVersion = 1
  163.       when "charger"
  164.         if @weapon[0] == "a laser"
  165.           puts "You increase the power of your laser by 10!"
  166.           @weapon[1] += 10
  167.           @laserVersion += 1
  168.           puts "Laser level #{ @laserVersion}!"
  169.           @inventory.delete "charger"
  170.         else
  171.           puts "You need a laser first!"
  172.       end
  173.       when "bottle"
  174.         if $facilityMap[coordinates][3] == "endportal"
  175.           puts "You explode into a shower of #{ @points } points!"
  176.           abort "You win!"
  177.         else
  178.           puts "You can't use the bottle yet!"
  179.         end
  180.       end
  181.     end
  182.   end
  183.  
  184.  
  185. class Monster
  186.  
  187.   attr_accessor :coordinates, :weapon, :life, :points
  188.  
  189.   def initialize( player )
  190.     @monsterKind = $facilityMap[player.coordinates][2]
  191.       if @monsterKind
  192.         case @monsterKind
  193.         when "dragon"
  194.           @monsterLife = 30
  195.           @monsterAttack = 10
  196.           roll = rand(2)
  197.           if roll == 0
  198.             @drop = "can"
  199.           else
  200.             @drop = "charger"
  201.           spawnProb = 3
  202.           end
  203.         when "troll"
  204.           @monsterLife = 15
  205.           @monsterAttack = 20
  206.           roll = rand(2)
  207.           if roll == 0
  208.             @drop = "gold"
  209.           else
  210.             @drop = "laser"
  211.           spawnProb = 10
  212.           end
  213.         else
  214.           raise Error "Nonexistent monster!"
  215.         end
  216.       if @monsterKind && rand(spawnProb) == 0
  217.         puts "A terrible #{ @monsterKind } appears!"
  218.         puts "Will you fight him or run?!"
  219.         puts "> "
  220.         input = gets.chomp
  221.           if $movementVars.include? input
  222.           puts "You escaped!"
  223.           player.move(input)
  224.         else
  225.           return fight player
  226.         end
  227.       end
  228.     else
  229.       return true
  230.     end
  231.   end
  232.  
  233.   def fight( player )
  234.     #The fight is a loop that runs till one of you is dead.
  235.     while @monsterLife > 0
  236.       puts "You duke it out and stuff."
  237.       player.life -= @monsterAttack
  238.       puts "The #{ @monsterKind } attacks for #{ @monsterAttack } damage!"
  239.       if player.life > 0
  240.         @monsterLife =- player.weapon[1]
  241.         puts "You attack the #{ @monsterKind } with #{ player.weapon[0] } for #{ player.weapon[1]} damage!"
  242.       else
  243.         abort "You died like an asshole."
  244.         puts "You had #{ player.points } points."
  245.         break
  246.       end
  247.      
  248.       #Here's the points formula, open to change.
  249.     points = -10 * ( @monsterLife * @monsterAttack ) / player.life
  250.     puts "You killed the #{ @monsterKind }!"
  251.     puts "You got #{ points } points!"
  252.     player.points += points
  253.     player.inventory << @drop
  254.     puts "You got a #{ @drop }!"
  255.     end
  256.   end
  257. end  
  258.  
  259. #Controls item spawning in rooms.
  260. class Item
  261.   attr_accessor :coordinates, :life
  262.   def initialize( player )
  263.     item = $facilityMap[player.coordinates][1]
  264.     if item
  265.       puts "There is a #{ item } here."
  266.     else
  267.       return true
  268.     end
  269.   end
  270. end
  271.  
  272.  
  273. #Eventual fixtures will include traps, buttons, machines, and other stuff built into rooms.
  274. #Winning conditions will involve fixtures.
  275. class Fixture
  276.   #TODO your mom  
  277. end
  278.  
  279. #Setting up for the main loop. Gotta have a player, and the Facility constant must be loaded
  280. #into memory as its constituent rooms.
  281. player = Inmate.new
  282. Facility.each{ |x, y, z, desc, items, monsters, traps|
  283. Room.new(x, y, z, desc, items, monsters, traps)}
  284. puts "You wake up in a cold white room."
  285.  
  286. #The main loop. Pretty simple: First it checks to see if there's an item. Then it rolls to spawn
  287. #a monster. Finally, it takes input from the player, which it splits up and parcels into the
  288. #Inmate.doCommand method.
  289. while true
  290.   Item.new(player)
  291.   Monster.new( player )
  292.   print "> "
  293.   input = gets.chomp.downcase.split(" ").to_a  
  294.   player.doCommand(input)
  295. end
  296.  
  297. #This crime against code was written by Robert Gryfft.
  298. #CC BY-SA 3.0
  299. #http://creativecommons.org/licenses/by-sa/3.0/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement