diff src/parpg/behaviours/npc.py @ 55:4311a0a5378c

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
children 097cceb06b92
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parpg/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