Began working on context-sensitive dialogues
[butaba-adventures.git] / maingame.py
index 02527db..66f28b8 100644 (file)
@@ -9,6 +9,8 @@ import butaba
 import utility
 import gameobjects
 import constants
+import npcs
+import gamestate
 
 class MainGame:
 
@@ -27,6 +29,9 @@ class MainGame:
 
                self.img_chestbg = pygame.image.load (os.path.join ("background", "chestcontent.png")).convert ()
 
+               self.img_dialogue = pygame.image.load (os.path.join ("background", "dialog_screen.png")).convert ()
+               self.img_dialogue.set_colorkey (pygame.Color (0, 255, 0))
+
 
                # initialize object graphics
                self.img_redpotion = pygame.image.load (os.path.join ("objects", "red-potion.png")).convert ()
@@ -56,6 +61,14 @@ class MainGame:
                self.img_butabaright = pygame.image.load (os.path.join ("sprite", "butaba-right.png")).convert ()
                self.img_butabaright.set_colorkey (pygame.Color (0, 255, 0))
 
+               # initialize NPC graphics
+               self.img_bulisa = pygame.image.load (os.path.join ("sprite", "bulisa.png")).convert ()
+               self.img_bulisa.set_colorkey (pygame.Color (0, 255, 0))
+
+               # initialize portraits
+               self.img_butaba_portrait = pygame.image.load (os.path.join ("portraits", "butaba.png")).convert ()
+               self.img_bulisa_portrait = pygame.image.load (os.path.join ("portraits", "bulisa.png")).convert ()
+
                # set level data
                self.setup_levels ()
                # set current level and position of our character
@@ -80,6 +93,11 @@ class MainGame:
                potion2 = gameobjects.HealthPotion (5, 2, self.img_redpotion)
                potion3 = gameobjects.HealthPotion (5, 2, self.img_redpotion)
 
+               npc_bulisa = npcs.Bulisa (4, 3, self.img_bulisa, self.img_bulisa_portrait,
+                                                                               [ os.path.join ("dialogues", "bulisa1.dlg"),
+                                                                                 os.path.join ("dialogues", "bulisa2.dlg"),
+                                                                                 os.path.join ("dialogues", "bulisa3.dlg") ] )
+
                chest1.objects = [ gold50, gold25, potion2, potion3, key2, gold10 ]
 
                # create the levels
@@ -89,7 +107,7 @@ class MainGame:
                self.level1w = level.Level (cPickle.load (file ("levels/level1w.dat")))
 
                self.level1e = level.Level (cPickle.load (file ("levels/level1e.dat")),
-                               objects = [ key1, potion, chest2 ])
+                               objects = [ key1, potion, chest2 ], npcs = [ npc_bulisa ])
 
                # set up the interaction between levels
                self.level1.levelright = self.level1e
@@ -106,6 +124,8 @@ class MainGame:
                        self.draw_level_background (self.currentlevel)
                        # draw level objects
                        self.draw_level_objects (self.currentlevel)
+                       # draw the NPCs in the level
+                       self.draw_level_npcs (self.currentlevel)
                        # draw our character
                        self.draw_butaba ()
                        # display the character's inventory
@@ -221,8 +241,62 @@ class MainGame:
 
                notblock = self.interact_objects (level, objs)
 
+               # get npc at current location
+               current_npc = None
+               for npc in level.npcs:
+                       if npc.row == row and npc.col == col:
+                               current_npc = npc
+                               break
+
+               # npcs always block the tile. So return false if there is an NPC
+               # at the location
+               if current_npc is not None:
+                       self.interact_npc (current_npc)
+                       return False
+
                return notblock
 
+       # interaction with npcs
+       def interact_npc (self, npc):
+               # interact with NPC and get the response ID
+               # if the NPC is Bulisa
+               if isinstance (npc, npcs.Bulisa):
+                       self.interact_npc_bulisa (npc)
+
+       # interact with NPC Bulisa
+       def interact_npc_bulisa (self, npc):
+               # set initial response ID to none
+               resp_id = None
+               print (gamestate.mission_bulisa_water_from_well, gamestate.mission_bulisa_water_from_well_complete)
+               # not yet started mission drawing water from well and not refused it
+               if (gamestate.mission_bulisa_water_from_well is False
+                       and gamestate.mission_bulisa_water_from_well_refused is False):
+                       # set the current dialogue
+                       npc.currentdialog = 0
+                       # get the response ID
+                       resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.img_butaba_portrait, 0, 90)
+               if (gamestate.mission_bulisa_water_from_well_refused is True and
+                               gamestate.mission_bulisa_water_from_well is False):
+                       # set the current dialog
+                       npc.currentdialog = 2
+                       resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.img_butaba_portrait, 0, 90)
+               # mission accepted but not completed
+               elif (gamestate.mission_bulisa_water_from_well is True
+                       and gamestate.mission_bulisa_water_from_well_complete is False):
+                       npc.currentdialog = 1
+                       # get the response ID
+                       resp_id = utility.dialogue_play (self.screen, self.img_dialogue, npc, self.img_butaba_portrait, 0, 90)
+
+               print resp_id
+               # if response ID is 12, then drawing water from well mission is refused
+               if resp_id == "12" or resp_id == "18":
+                       gamestate.mission_bulisa_water_from_well_refused = True
+               # if response ID is 13: that is accepted the drawing water from well mission begins
+               if resp_id == "13" or resp_id == "17":
+                       gamestate.mission_bulisa_water_from_well = True
+               # if response ID is none
+               elif resp_id is None:
+                       self.status_message = "You cannot initiate a conversation with %s" % npc.charname
 
        # interaction with objects
        def interact_objects (self, container, objs):
@@ -236,7 +310,7 @@ class MainGame:
                                notblock = False
                        # if object can be picked up ask
                        if obj.can_pickup is True:
-                               ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Pick up", "Use", "Ignore"], self.img_menu)
+                               ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Pick up", obj.use_str, "Ignore"], self.img_menu)
                                # if the answer is "pick up"
                                if ans == 1:
                                        self.pickup_object (container, obj)
@@ -245,12 +319,20 @@ class MainGame:
                                        self.use_object (container, obj)
                        # if it cannot be picked up, try to use it anyway
                        else:
-                               ans = utility.ask_question (self.screen, "Found %s." % obj.text, ["Use", "Ignore"], self.img_menu)
+                               ans = utility.ask_question (self.screen, "Found %s." % obj.text, [obj.use_str, "Ignore"], self.img_menu)
                                if ans == 1:
                                        self.use_object (container, obj)
 
                return notblock
 
+       # transfer an object from one container to another
+       # container must have an objects list
+       def transfer_object (self, source, obj, dest):
+               # remove object from source
+               source.objects.remove (obj)
+               # add object to destination
+               dest.objects.append (obj)
+
        # picking up an object
        def pickup_object (self, container, obj):
                # only if object can be picked up, pick it up or use it
@@ -260,9 +342,7 @@ class MainGame:
                                self.status_message = "Cannot pick up item. Inventory full"
                        else:
                                # add item to inventory
-                               self.butaba.objects.append (obj)
-                               # remove from container
-                               container.objects.remove (obj)
+                               self.transfer_object (container, obj, self.butaba)
 
                                self.status_message = "You picked up %s" % obj.text
 
@@ -452,3 +532,10 @@ class MainGame:
                for obj in level.objects:
                        if obj.image is not None:
                                self.screen.blit (obj.image, (obj.col*48, obj.row*48))
+
+
+       # Draw the NPCs in the level
+       def draw_level_npcs (self, level):
+               for npc in level.npcs:
+                       if npc.image is not None:
+                               self.screen.blit (npc.image, (npc.col*48, npc.row*48))