Mercurial > parpg-core
view src/parpg/behaviours/npc.py @ 64:32faacaf6f28
Added funcionality to load Entities from file
author | KarstenBock@gmx.net |
---|---|
date | Mon, 05 Sep 2011 14:47:21 +0200 |
parents | 2915dbb60940 |
children | 58661f5b2f6b |
line wrap: on
line source
# 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 import base from base import BaseBehaviour 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 = base._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())