GameState.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. if(MIDAS == undefined) {
  2. var MIDAS = {};
  3. }
  4. MIDAS.GameState = function(gameConsole) {
  5. var self = this;
  6. var commands = ["say", "look", "exits", "go"];
  7. var commandsDescription = [
  8. "Your character speaks whatever you type"
  9. , "Gathers information about your current surroundings"
  10. , "Lists all of the ways out of the room"
  11. , "Moves in the specified direction"
  12. ];
  13. var rooms = [ {"id" : 0, "name" : "The Void" , "description" : "Nothing but swirling mist in the blackness.", "exits" : []},
  14. {"id" : 1, "name" : "Entry Hall", "description" : "The ceiling of the hall is vaulted, with light streaming in through the stained glass windows on either side.", "exits" : [{"command" : "north", "target" : 2, "action" : "You move north."}]}
  15. ,{"id" : 2, "name" : "Grand Ballroom", "description" : "The cool marble floors glisten with the candlight of the crystal chandeliers above.", "exits" : [{"command" : "south", "target" : 1, "action" : "You waltz south."}]}
  16. ];
  17. var playerPosition = 1;
  18. this.getCommandList = function() {
  19. return commands;
  20. };
  21. this.getCommandDescriptions = function() {
  22. return commandsDescription;
  23. };
  24. this.handleCommandInput = function(commandObject) {
  25. switch(commandObject.command) {
  26. case commands[0]:
  27. gameConsole.output("You said \"" + commandObject.data.join(" ") + "\"");
  28. break;
  29. case commands[1]:
  30. gameConsole.output(rooms[playerPosition].name);
  31. gameConsole.output(rooms[playerPosition].description);
  32. break;
  33. case commands[2]:
  34. var exitsList = [];
  35. if(rooms[playerPosition].exits.length == 0) {
  36. gameConsole.output("There are no obvious exits.");
  37. } else {
  38. for(var i = 0; i < rooms[playerPosition].exits.length; i++) {
  39. exitsList.push(rooms[playerPosition].exits[i].command);
  40. }
  41. gameConsole.output("There are exits to the " + exitsList.join(", "));
  42. }
  43. break;
  44. case commands[3]:
  45. var exit = {};
  46. for(var i = 0; i < rooms[playerPosition].exits.length; i++) {
  47. if(commandObject.data[0] == rooms[playerPosition].exits[i].command){
  48. exit = rooms[playerPosition].exits[i];
  49. }
  50. }
  51. console.log(exit);
  52. if (exit.target) {
  53. gameConsole.output(exit.action);
  54. playerPosition = exit.target;
  55. gameConsole.output(rooms[playerPosition].name);
  56. gameConsole.output(rooms[playerPosition].description);
  57. } else {
  58. gameConsole.output("I don't see that exit.");
  59. }
  60. break;
  61. }
  62. };
  63. };