changeset 104:c9afad46091b

Added actions for lockable components (Open, Close, Lock and Unlock).
author KarstenBock@gmx.net
date Fri, 30 Sep 2011 14:05:16 +0200
parents 57f1cff9a75d
children dc573ac08355
files gamescenecontroller.py objects/action.py
diffstat 2 files changed, 80 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/gamescenecontroller.py	Fri Sep 30 14:04:29 2011 +0200
+++ b/gamescenecontroller.py	Fri Sep 30 14:05:16 2011 +0200
@@ -28,9 +28,10 @@
 from controllerbase import ControllerBase
 from parpg.gui.hud import Hud
 from parpg.gui import drag_drop_data as data_drag
-from objects.action import ChangeMapAction, ExamineAction, OpenBoxAction, \
-                           UnlockBoxAction, LockBoxAction, TalkAction, \
-                           PickUpAction, DropItemAction
+from objects.action import (ChangeMapAction, ExamineAction, TalkAction,
+                            OpenAction, CloseAction, UnlockAction, LockAction, 
+                            PickUpAction, DropItemAction,
+                            )
 
 from parpg.world import World
 
@@ -465,18 +466,26 @@
                                    obj.change_map.target_position)])
             
             if obj.lockable:
-                actions.append(["Open", "Open", 
-                                player.fifeagent.behaviour.approach,
-                                [obj_pos.x, obj_pos.y],
-                                OpenBoxAction(self, obj)])
-                actions.append(["Unlock", "Unlock", 
-                                player.fifeagent.behaviour.approach,
-                                [obj_pos.x, obj_pos.y],
-                                UnlockBoxAction(self, obj)])
-                actions.append(["Lock", "Lock", 
-                                player.fifeagent.behaviour.approach,
-                                [obj_pos.x, obj_pos.y],
-                                LockBoxAction(self, obj)])
+                if obj.lockable.closed:
+                    actions.append(["Open", "Open", 
+                                    player.fifeagent.behaviour.approach,
+                                    [obj_pos.x, obj_pos.y],
+                                    OpenAction(self, obj)])
+                else:
+                    actions.append(["Close", "Close", 
+                                    player.fifeagent.behaviour.approach,
+                                    [obj_pos.x, obj_pos.y],
+                                    CloseAction(self, obj)])
+                if obj.lockable.locked:
+                    actions.append(["Unlock", "Unlock", 
+                                    player.fifeagent.behaviour.approach,
+                                    [obj_pos.x, obj_pos.y],
+                                    UnlockAction(self, obj)])
+                else:
+                    actions.append(["Lock", "Lock", 
+                                    player.fifeagent.behaviour.approach,
+                                    [obj_pos.x, obj_pos.y],
+                                    LockAction(self, obj)])
             if obj.containable:
                 actions.append(["Pick Up", "Pick Up", 
                                 player.fifeagent.behaviour.approach,
--- a/objects/action.py	Fri Sep 30 14:04:29 2011 +0200
+++ b/objects/action.py	Fri Sep 30 14:05:16 2011 +0200
@@ -21,7 +21,7 @@
 
 from parpg.gui import drag_drop_data as data_drag
 from parpg.dialoguecontroller import DialogueController
-from parpg.components import container
+from parpg.components import container, lockable
 
 
 class NoSuchQuestException(Exception):
@@ -92,8 +92,8 @@
         super(ChangeMapAction, self).execute()
 
 class OpenAction(Action):
-    """Open a container"""
-    def __init__(self, controller, container, commands=None):
+    """Open an lockable"""
+    def __init__(self, controller, lockable, commands=None):
         """
         @param controller: A reference to the GameSceneController.
         @type controller: parpg.GameSceneController
@@ -101,21 +101,27 @@
         @type commands: Dictionary 
         @type view: class derived from parpg.ViewBase
         @param view: The view
-        @param container: A reference to the container
+        @param lockable: A reference to the lockable
         """
-        super(OpenAction, self).__init__(controller, commands)
+        Action.__init__(self, controller, commands)
         self.view = controller.view
-        self.container = container
+        self.lockable = lockable
+    
     def execute(self):
-        """Open the box."""
-        self.view.hud.createBoxGUI(self.container.name, \
-                                              self.container)
-        super(OpenAction, self).execute()
-       
-       
-class OpenBoxAction(OpenAction):
-    """Open a box. Needs to be more generic, but will do for now."""
-    def __init__(self, controller, container, commands = None):
+        """Open the lockable."""
+        try:
+            lockable.open(self.lockable.lockable)
+            self.lockable.fifeagent.behaviour.animate("open")
+            self.lockable.fifeagent.behaviour.queue_animation("opened", 
+                                                              repeating=True)
+        except lockable.LockedError:
+            self.view.hud.createExamineBox(self.lockable.description.view_name,
+                                           "Locked")            
+        Action.execute(self)
+
+class CloseAction(Action):
+    """Close an lockable"""
+    def __init__(self, controller, lockable, commands=None):
         """
         @param controller: A reference to the GameSceneController.
         @type controller: parpg.GameSceneController
@@ -123,57 +129,60 @@
         @type commands: Dictionary 
         @type view: class derived from parpg.ViewBase
         @param view: The view
-        @param container: A reference to the container
+        @param lockable: A reference to the lockable
         """
-        super(OpenBoxAction, self).__init__(controller, commands)
-        self.view = controller.view
-        self.container = container
-        
+        Action.__init__(self, controller, commands)
+        self.lockable = lockable
+    
     def execute(self):
-        """Open the box."""
-        try:
-            self.container.open()
-            super(OpenBoxAction, self).execute()
-
-        except ValueError:
-            self.view.hud.createExamineBox(self.container.name, \
-                                                  "The container is locked")
+        """Close the lockable."""
+        lockable.close(self.lockable.lockable)
+        self.lockable.fifeagent.behaviour.animate("close")
+        self.lockable.fifeagent.behaviour.queue_animation("closed", 
+                                                          repeating=True)
+        Action.execute(self)
         
-class UnlockBoxAction(Action):
-    """Unlocks a box. Needs to be more generic, but will do for now."""
-    def __init__(self, controller, container, commands = None):
+class UnlockAction(Action):
+    """Unlocks a lockable."""
+    def __init__(self, controller, lockable, commands = None):
         """
         @param controller: A reference to the GameSceneController.
         @type controller: parpg.GameSceneController
         @param commands: Special commands that are executed
         @type commands: Dictionary 
-        @param container: A reference to the container
+        @param lockable: A reference to the lockable
         """
-        super(UnlockBoxAction, self).__init__(controller, commands)
-        self.container = container
+        Action.__init__(self, controller, commands)
+        self.lockable = lockable
     
     def execute(self):
         """Open the box."""
-        self.container.unlock()
-        super(UnlockBoxAction, self).execute()
+        lockable.unlock(self.lockable.lockable)
+        Action.execute(self)
         
-class LockBoxAction(Action):
-    """Locks a box. Needs to be more generic, but will do for now."""
-    def __init__(self, controller, container, commands = None):
+class LockAction(Action):
+    """Locks a lockable."""
+    def __init__(self, controller, lockable, commands = None):
         """
         @param controller: A reference to the GameSceneController.
         @type controller: parpg.GameSceneController
         @param commands: Special commands that are executed
         @type commands: Dictionary 
-        @param container: A reference to the container
+        @param lockable: A reference to the lockable
         """
-        super(LockBoxAction, self).__init__(controller, commands)
-        self.container = container
+        Action.__init__(self, controller, commands)
+        self.lockable = lockable
+        self.view = controller.view
         
     def execute(self):
         """Lock the box."""
-        self.container.lock()
-        super(LockBoxAction, self).execute()
+        try:
+            lockable.lock(self.lockable.lockable)
+        except lockable.OpenError:
+            self.view.hud.createExamineBox(self.lockable.description.view_name,
+                                           "Is open")            
+            
+        Action.execute(self)
 
 
 class ExamineAction(Action):
@@ -547,9 +556,9 @@
 
 ACTIONS = {"ChangeMap":ChangeMapAction, 
            "Open":OpenAction,
-           "OpenBox":OpenBoxAction, 
-           "Unlock":UnlockBoxAction,
-           "Lock":LockBoxAction,
+           "Close":CloseAction,
+           "Unlock":UnlockAction,
+           "Lock":LockAction,
            "ExamineItem":ExamineItemAction,
            "Examine":ExamineAction,
            "Look":ExamineItemAction,