Commits

rablador committed f34a515

Added support for alternative story messages. Added support for storing and getting current/previous room objects. Lots of refactoring needed.

Comments (0)

Files changed (5)

   def go(self, direction):
     for exit in self.exits:
       if direction in exit:
-        return self.exits[direction]
+        if type(self.exits[direction]) == type(dict()):
+          return self.exits[direction]['room']
+        else:
+          return self.exits[direction]
 
   def interact(self, keyword, action):
     for item in self.items:
     
     types = {
       'direction' : None,
+      'directional_action' : None,
       'action' : None,
       'keyword' : None
     }
       if (direction):
         types['direction'] = direction
 
+      directional_action = self.get_directional_action(word)
+      if (directional_action):
+        types['directional_action'] = directional_action
+
       action = self.get_action(word)
       if (action):
         types['action'] = action
       if (word in dir):
         return dir[0]
 
+  def get_directional_action(self, word):
+    if (word in self.keywords['actions']['directional']):
+      return {'word' : word, 'type' : 'directional'}
+
   def get_action(self, word):
     if (word in self.keywords['actions']['where']):
-      return {'word' : word, 'kind' : 'where'}
+      return {'word' : word, 'type' : 'where'}
     elif (word in self.keywords['actions']['what']):
-      return {'word' : word, 'kind' : 'what'}
+      return {'word' : word, 'type' : 'what'}
 
   def get_keyword(self, word):
     if (word in self.keywords['keywords']):
 from libs.player import Player
 from libs.room import Room
 from libs.util import Util
+from libs.visited_rooms import VisitedRooms
 
 util = Util()
+visited_rooms = VisitedRooms()
 
 # clear = lambda: os.system('cls')
 # clear()
 
-current_room = Room('room1')
+current_room = Room('step1')
 player = Player('Mika')
 
 print('\n' + player.name + ', welcome to Text Adventure!')
   command = input('')
   types = util.get_command_types(command)
 
-  if (types['keyword'] and types['action']):
+  if (types['directional_action'] and types['direction']):
+    room_name = current_room.go(types['direction'])
+    if (room_name):
+      current_room = Room(room_name)
+      print('\n' + current_room.story)
+    else:
+      print('\n' + 'You can\'t go there.')
+
+  if (types['action'] and types['keyword']):
     interaction = current_room.interact(types['keyword'], types['action']['word'])
     if (interaction):
       print('\n' + interaction)
+    else:
+      print('\n' + 'You can\'t do that.')
 
   else:
     if (types['action']):
-      print('\n' + types['action']['word'].capitalize() + ' ' + types['action']['kind'] + '?')
+      print('\n' + types['action']['word'].capitalize() + ' ' + types['action']['type'] + '?')
       
       command = input('')
       additional_types = util.get_command_types(command)
         if (interaction):
           print('\n' + interaction)
 
-    elif (types['direction']):
+    elif types['direction']:      
+      try:
+        alternative_story = current_room.exits[types['direction']]['story']
+      except KeyError:
+        alternative_story = None
+
       room_name = current_room.go(types['direction'])
       if (room_name):
-        current_room = Room(room_name)
-        print('\n' + current_room.story)
+        current_room = visited_rooms.get(room_name)
+        if (current_room == None):
+          current_room = Room(room_name)
+        visited_rooms.add(room_name, current_room)
+
+        story = alternative_story if alternative_story else current_room.story
+        print(story)

resources/keywords.json

       "smell",
       "open",
       "knock"
+    ],
+    "directional" : [
+      "go",
+      "walk",
+      "run",
+      "crawl",
+      "jump",
+      "jog",
+      "sprint"
     ]
   },
 

resources/rooms.json

 {
-  "room1" : {
+  "step1" : {
     "exits" : {
-      "e" : "room2"
+      "e" : {
+        "room" : "step2",
+        "story" : "This is an alternative story."
+      }
     },
     "story" : "You stand in a large room with some flowers beside you and a door to the right.",
     "items" : {
     }
   },
 
-  "room2" : {
+  "step2" : {
     "exits" : {
-      "w" : "room1"
+      "w" : {
+        "room" : "step1"
+      }
     },
     "story" : "You stand in a large room with some flowers beside you and a door to the left.",
     "items" : {