changeset 56:3f6299f975fe

Added BaseBehaviour. Moved methods from MovingAgentBehaviour to BaseBehaviour.
author KarstenBock@gmx.net
date Fri, 09 Sep 2011 15:18:17 +0200
parents 8b1ad2d342d8
children ba85e5aff370
files behaviours/__init__.py behaviours/base.py behaviours/moving.py behaviours/npc.py
diffstat 4 files changed, 15 insertions(+), 107 deletions(-) [+]
line wrap: on
line diff
--- a/behaviours/__init__.py	Fri Sep 09 15:05:23 2011 +0200
+++ b/behaviours/__init__.py	Fri Sep 09 15:18:17 2011 +0200
@@ -13,6 +13,7 @@
 #   You should have received a copy of the GNU General Public License
 #   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
 
-from base import MovingAgentBehaviour as Moving
+from base import BaseBehaviour as Base
+from moving import MovingAgentBehaviour as Moving
 from npc import NPCBehaviour as NonPlayer
 from player import PlayerBehaviour as Player
\ No newline at end of file
--- a/behaviours/base.py	Fri Sep 09 15:05:23 2011 +0200
+++ b/behaviours/base.py	Fri Sep 09 15:18:17 2011 +0200
@@ -23,8 +23,6 @@
         fife.InstanceActionListener.__init__(self)
         self.agent = None
         self.state = None
-        self.speed = 0
-        self.idle_counter = 1
     
     def attachToLayer(self, agent_ID, layer):
         """Attaches to a certain layer
@@ -57,26 +55,11 @@
         self.agent = layer.getInstance(self.parent.fifeagent.identifier)
         self.agent.addActionListener(self)
         self.state = _AGENT_STATE_NONE
-        self.idle_counter = 1
     
     def idle(self):
         """@return: None"""
         self.state = _AGENT_STATE_IDLE
-        self.agent.act('stand', self.agent.getFacingLocation())
-
-    def approach(self, location, action=None):
-        """Approaches a location and then perform an action (if set).
-           @type loc: fife.Location
-           @param loc: the location to approach
-           @type action: Action
-           @param action: The action to schedule for execution after the approach.
-           @return: None"""
-        self.state = _AGENT_STATE_APPROACH
-        self.nextAction = action
-        boxLocation = tuple([int(float(i)) for i in location])
-        l = fife.Location(self.agent.getLocation())
-        l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
-        self.agent.move('run', l, self.speed + 1)        
+        self.agent.act('stand', self.agent.getFacingLocation())       
         
     def onInstanceActionFinished(self, instance, action):
         """@type instance: ???
@@ -91,11 +74,7 @@
         
         if act:
             act.execute()
-            
-        if(action.getId() != 'stand'):
-            self.idle_counter = 1
-        else:
-            self.idle_counter += 1 
+          
             
     def getLocation(self):
         """Get the agents position as a fife.Location object. 
@@ -110,22 +89,4 @@
         self.state = _AGENT_STATE_TALK
         self.pc = pc.behaviour.agent
         self.idle()
-        
-    def run(self, location):
-        """Makes the PC run to a certain location
-           @type location: fife.ScreenPoint
-           @param location: Screen position to run to.
-           @return: None"""
-        self.state = _AGENT_STATE_RUN
-        self.nextAction = None
-        self.agent.move('run', location, self.speed + 1)
-
-    def walk(self, location):
-        """Makes the PC walk to a certain location.
-           @type location: fife.ScreenPoint
-           @param location: Screen position to walk to.
-           @return: None"""
-        self.state = _AGENT_STATE_RUN
-        self.nextAction = None 
-        self.agent.move('walk', location, self.speed - 1)
         
\ No newline at end of file
--- a/behaviours/moving.py	Fri Sep 09 15:05:23 2011 +0200
+++ b/behaviours/moving.py	Fri Sep 09 15:18:17 2011 +0200
@@ -14,55 +14,21 @@
 #   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
 
 from fife import fife
+import base
+from base import BaseBehaviour
 
-_AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6)
-
-class MovingAgentBehaviour (fife.InstanceActionListener):
+class MovingAgentBehaviour (BaseBehaviour):
     """Fife agent listener"""
     def __init__(self):
-        fife.InstanceActionListener.__init__(self)
-        self.agent = None
-        self.state = None
+        BaseBehaviour.__init__(self)
         self.speed = 0
         self.idle_counter = 1
-    
-    def attachToLayer(self, agent_ID, layer):
-        """Attaches to a certain layer
-           @type agent_ID: String
-           @param agent_ID: ID of the layer to attach to.
-           @type layer: Fife layer
-           @param layer: Layer of the agent to attach the behaviour to
-           @return: None"""
-        self.agent = layer.getInstance(agent_ID)
-        self.agent.addActionListener(self)
-        self.state = _AGENT_STATE_NONE
-        
-    def getX(self):
-        """Get the NPC's x position on the map.
-           @rtype: integer"
-           @return: the x coordinate of the NPC's location"""
-        return self.agent.getLocation().getLayerCoordinates().x
-
-    def getY(self):
-        """Get the NPC's y position on the map.
-           @rtype: integer
-           @return: the y coordinate of the NPC's location"""
-        return self.agent.getLocation().getLayerCoordinates().y
         
     def onNewMap(self, layer):
         """Sets the agent onto the new layer."""
-        if self.agent is not None:
-            self.agent.removeActionListener(self)
-            
-        self.agent = layer.getInstance(self.parent.fifeagent.identifier)
-        self.agent.addActionListener(self)
-        self.state = _AGENT_STATE_NONE
+        BaseBehaviour.onNewMap(self, layer)
         self.idle_counter = 1
-    
-    def idle(self):
-        """@return: None"""
-        self.state = _AGENT_STATE_IDLE
-        self.agent.act('stand', self.agent.getFacingLocation())
+
 
     def approach(self, location, action=None):
         """Approaches a location and then perform an action (if set).
@@ -71,7 +37,7 @@
            @type action: Action
            @param action: The action to schedule for execution after the approach.
            @return: None"""
-        self.state = _AGENT_STATE_APPROACH
+        self.state = base._AGENT_STATE_APPROACH
         self.nextAction = action
         boxLocation = tuple([int(float(i)) for i in location])
         l = fife.Location(self.agent.getLocation())
@@ -84,39 +50,19 @@
            @type action: ???
            @param action: ???
            @return: None"""
-        # First we reset the next behavior 
-        act = self.nextAction
-        self.nextAction = None 
-        self.idle()
-        
-        if act:
-            act.execute()
+        BaseBehaviour.onInstanceActionFinished(self, instance, action)
             
         if(action.getId() != 'stand'):
             self.idle_counter = 1
         else:
             self.idle_counter += 1 
-            
-    def getLocation(self):
-        """Get the agents position as a fife.Location object. 
-           @rtype: fife.Location
-           @return: the location of the agent"""
-        return self.agent.getLocation()
-    
-        
-    def talk(self, pc):
-        """Makes the agent ready to talk to the PC
-           @return: None"""
-        self.state = _AGENT_STATE_TALK
-        self.pc = pc.behaviour.agent
-        self.idle()
         
     def run(self, location):
         """Makes the PC run to a certain location
            @type location: fife.ScreenPoint
            @param location: Screen position to run to.
            @return: None"""
-        self.state = _AGENT_STATE_RUN
+        self.state = base._AGENT_STATE_RUN
         self.nextAction = None
         self.agent.move('run', location, self.speed + 1)
 
@@ -125,7 +71,7 @@
            @type location: fife.ScreenPoint
            @param location: Screen position to walk to.
            @return: None"""
-        self.state = _AGENT_STATE_RUN
+        self.state = base._AGENT_STATE_RUN
         self.nextAction = None 
         self.agent.move('walk', location, self.speed - 1)
         
\ No newline at end of file
--- a/behaviours/npc.py	Fri Sep 09 15:05:23 2011 +0200
+++ b/behaviours/npc.py	Fri Sep 09 15:18:17 2011 +0200
@@ -17,7 +17,7 @@
 
 from fife import fife
 
-import moving
+import base
 from moving import MovingAgentBehaviour
 
 class NPCBehaviour(MovingAgentBehaviour):