changeset 30:ec5a55266f6a

Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
author KarstenBock@gmx.net
date Mon, 05 Sep 2011 13:02:05 +0200
parents 86799b61e5dc
children 2b51d0010e1c
files behaviours/__init__.py behaviours/base.py behaviours/npc.py behaviours/player.py
diffstat 4 files changed, 226 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/behaviours/__init__.py	Mon Sep 05 13:02:05 2011 +0200
@@ -0,0 +1,18 @@
+#   This file is part of PARPG.
+
+#   PARPG is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+
+#   PARPG is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+
+#   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 BaseBehaviour
+from npc import NPCBehaviour
+from player import PlayerBehaviour
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/behaviours/base.py	Mon Sep 05 13:02:05 2011 +0200
@@ -0,0 +1,67 @@
+#   This file is part of PARPG.
+
+#   PARPG is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+
+#   PARPG is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+
+#   You should have received a copy of the GNU General Public License
+#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
+
+from fife import fife
+
+_AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6)
+
+class BaseBehaviour (fife.InstanceActionListener):
+    """Fife agent listener"""
+    def __init__(self, layer):
+        fife.InstanceActionListener.__init__(self)
+        self.layer = layer
+        self.agent = None
+        self.state = None
+        self.speed = 0
+        self.idle_counter = 1
+    
+    def attachToLayer(self, agent_ID):
+        """Attaches to a certain layer
+           @type agent_ID: String
+           @param agent_ID: ID of the layer to attach to.
+           @return: None"""
+        self.agent = self.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.ID)
+        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 onInstanceActionFinished(self, instance, action):
+        pass
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/behaviours/npc.py	Mon Sep 05 13:02:05 2011 +0200
@@ -0,0 +1,97 @@
+#   This file is part of PARPG.
+
+#   PARPG is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+
+#   PARPG is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+
+#   You should have received a copy of the GNU General Public License
+#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
+
+from random import randrange
+
+from base import *
+
+class NPCBehaviour(BaseBehaviour):
+    """This is a basic NPC behaviour"""
+    def __init__(self, Parent=None, Layer=None):
+        super(NPCBehaviour, self).__init__(Layer)
+        
+        self.parent = Parent
+        self.state = _AGENT_STATE_NONE
+        self.pc = None
+        self.target_loc = None
+        self.nextAction = None
+        
+        # hard code these for now
+        self.distRange = (2, 4)
+        # these are parameters to lower the rate of wandering
+        # wander rate is the number of "IDLEs" before a wander step
+        # this could be set for individual NPCs at load time
+        # or thrown out altogether.
+        self.wanderCounter = 0
+        self.wanderRate = 9
+        
+    def getTargetLocation(self):
+        """@rtype: fife.Location
+           @return: NPC's position"""
+        x = self.getX()
+        y = self.getY()
+        if self.state == _AGENT_STATE_WANDER:
+            """ Random Target Location """
+            l = [0, 0]
+            for i in range(len(l)):
+                sign = randrange(0, 2)
+                dist = randrange(self.distRange[0], self.distRange[1])
+                if sign == 0:
+                    dist *= -1
+                l[i] = dist
+            x += l[0]
+            y += l[1]
+            # Random walk is
+            # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud
+        l = fife.Location(self.agent.getLocation())
+        l.setLayerCoordinates(fife.ModelCoordinate(x, y))
+        return l
+
+    def onInstanceActionFinished(self, instance, action):
+        """What the NPC does when it has finished an action.
+           Called by the engine and required for InstanceActionListeners.
+           @type instance: fife.Instance
+           @param instance: self.agent (the NPC listener is listening for this
+                                        instance)
+           @type action: ???
+           @param action: ???
+           @return: None"""
+        if self.state == _AGENT_STATE_WANDER:
+            self.target_loc = self.getTargetLocation()
+        self.idle()
+        
+    
+    def idle(self):
+        """Controls the NPC when it is idling. Different actions
+           based on the NPC's state.
+           @return: None"""
+        if self.state == _AGENT_STATE_NONE:
+            self.state = _AGENT_STATE_IDLE
+            self.agent.act('stand', self.agent.getFacingLocation())
+        elif self.state == _AGENT_STATE_IDLE:
+            if self.wanderCounter > self.wanderRate:
+                self.wanderCounter = 0
+                self.state = _AGENT_STATE_WANDER
+            else:
+                self.wanderCounter += 1
+                self.state = _AGENT_STATE_NONE
+            
+            self.target_loc = self.getTargetLocation()
+            self.agent.act('stand', self.agent.getFacingLocation())
+        elif self.state == _AGENT_STATE_WANDER:
+            self.parent.wander(self.target_loc)
+            self.state = _AGENT_STATE_NONE
+        elif self.state == _AGENT_STATE_TALK:
+            self.agent.act('stand', self.pc.getLocation())
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/behaviours/player.py	Mon Sep 05 13:02:05 2011 +0200
@@ -0,0 +1,44 @@
+#   This file is part of PARPG.
+
+#   PARPG is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+
+#   PARPG is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+
+#   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 *
+
+class PlayerBehaviour (BaseBehaviour):
+    def __init__(self, parent=None, layer=None):
+        super(PlayerBehaviour, self).__init__(layer)        
+        self.parent = parent
+        self.idle_counter = 1
+        self.speed = 0
+        self.nextAction = None
+        self.agent = None
+        
+    def onInstanceActionFinished(self, instance, action):
+        """@type instance: ???
+           @param instance: ???
+           @type action: ???
+           @param action: ???
+           @return: None"""
+        # First we reset the next behavior 
+        act = self.nextAction
+        self.nextAction = None 
+        self.idle()
+        
+        if act:
+            act.execute()
+            
+        if(action.getId() != 'stand'):
+            self.idle_counter = 1
+        else:
+            self.idle_counter += 1  
\ No newline at end of file