# HG changeset patch # User KarstenBock@gmx.net # Date 1317384269 -7200 # Node ID ecac92680beff14e34a0fdb6da7732e5db423749 # Parent ccf4d6cffcf169558e25eeb4da24eed73357f361 Added animation queue and method the base behaviour class. diff -r ccf4d6cffcf1 -r ecac92680bef src/parpg/behaviours/__init__.py --- a/src/parpg/behaviours/__init__.py Thu Sep 29 18:15:12 2011 +0200 +++ b/src/parpg/behaviours/__init__.py Fri Sep 30 14:04:29 2011 +0200 @@ -1,19 +1,19 @@ -# 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 . - -from base import BaseBehaviour as Base -from moving import MovingAgentBehaviour as Moving -from npc import NPCBehaviour as NonPlayer +# 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 . + +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 diff -r ccf4d6cffcf1 -r ecac92680bef src/parpg/behaviours/base.py --- a/src/parpg/behaviours/base.py Thu Sep 29 18:15:12 2011 +0200 +++ b/src/parpg/behaviours/base.py Fri Sep 30 14:04:29 2011 +0200 @@ -13,6 +13,8 @@ # You should have received a copy of the GNU General Public License # along with PARPG. If not, see . +from collections import deque + from fife import fife _AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6) @@ -23,6 +25,8 @@ fife.InstanceActionListener.__init__(self) self.agent = None self.state = None + self.animation_queue = deque() + self.nextAction = None def attachToLayer(self, agent_ID, layer): """Attaches to a certain layer @@ -58,7 +62,7 @@ def idle(self): """@return: None""" - self.state = _AGENT_STATE_IDLE + self.state = _AGENT_STATE_IDLE def onInstanceActionFinished(self, instance, action): """@type instance: ??? @@ -73,6 +77,11 @@ if act: act.execute() + try: + animtion = self.animation_queue.popleft() + self.animate(**animtion) + except IndexError: + self.idle() def getLocation(self): @@ -87,5 +96,19 @@ @return: None""" self.state = _AGENT_STATE_TALK self.pc = pc.behaviour.agent + self.clear_animations() self.idle() - \ No newline at end of file + + def animate(self, action, direction = None, repeating = False): + """Perform an animation""" + direction = direction or self.agent.getFacingLocation() + self.agent.act(action, direction, repeating) + + def queue_animation(self, action, direction = None, repeating = False): + """Add an action to the queue""" + self.animation_queue.append({"action": action, "direction": direction, + "repeating": repeating}) + + def clear_animations(self): + """Remove all actions from the queue""" + self.animation_queue.clear() diff -r ccf4d6cffcf1 -r ecac92680bef src/parpg/behaviours/moving.py --- a/src/parpg/behaviours/moving.py Thu Sep 29 18:15:12 2011 +0200 +++ b/src/parpg/behaviours/moving.py Fri Sep 30 14:04:29 2011 +0200 @@ -1,82 +1,84 @@ -# 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 . - -from fife import fife -import base -from base import BaseBehaviour - -class MovingAgentBehaviour (BaseBehaviour): - """Fife agent listener""" - def __init__(self): - BaseBehaviour.__init__(self) - self.speed = 0 - self.idle_counter = 1 - - def onNewMap(self, layer): - """Sets the agent onto the new layer.""" - BaseBehaviour.onNewMap(self, layer) - self.idle_counter = 1 - - - 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 = base._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) - - def onInstanceActionFinished(self, instance, action): - """@type instance: ??? - @param instance: ??? - @type action: ??? - @param action: ??? - @return: None""" - BaseBehaviour.onInstanceActionFinished(self, instance, action) - - if(action.getId() != 'stand'): - self.idle_counter = 1 - else: - self.idle_counter += 1 - - def idle(self): - """@return: None""" - BaseBehaviour.idle(self) - self.agent.act('stand', self.agent.getFacingLocation()) - - 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 = base._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 = base._AGENT_STATE_RUN - self.nextAction = None - self.agent.move('walk', location, self.speed - 1) +# 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 . + +from fife import fife +import base +from base import BaseBehaviour + +class MovingAgentBehaviour (BaseBehaviour): + """Fife agent listener""" + def __init__(self): + BaseBehaviour.__init__(self) + self.speed = 0 + self.idle_counter = 1 + + def onNewMap(self, layer): + """Sets the agent onto the new layer.""" + BaseBehaviour.onNewMap(self, layer) + self.idle_counter = 1 + + + 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 = base._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) + + def onInstanceActionFinished(self, instance, action): + """@type instance: ??? + @param instance: ??? + @type action: ??? + @param action: ??? + @return: None""" + BaseBehaviour.onInstanceActionFinished(self, instance, action) + + if(action.getId() != 'stand'): + self.idle_counter = 1 + else: + self.idle_counter += 1 + + def idle(self): + """@return: None""" + BaseBehaviour.idle(self) + self.animate('stand') + + 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 = base._AGENT_STATE_RUN + self.clear_animations() + 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 = base._AGENT_STATE_RUN + self.clear_animations() + self.nextAction = None + self.agent.move('walk', location, self.speed - 1) \ No newline at end of file diff -r ccf4d6cffcf1 -r ecac92680bef src/parpg/behaviours/npc.py --- a/src/parpg/behaviours/npc.py Thu Sep 29 18:15:12 2011 +0200 +++ b/src/parpg/behaviours/npc.py Fri Sep 30 14:04:29 2011 +0200 @@ -1,106 +1,105 @@ -# 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 . - -from random import randrange - -from fife import fife - -import base -from moving import MovingAgentBehaviour - -class NPCBehaviour(MovingAgentBehaviour): - """This is a basic NPC behaviour""" - def __init__(self, parent=None): - super(NPCBehaviour, self).__init__() - - 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 == base._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 - @type action: ??? - @param action: ??? - @return: None""" - if self.state == base._AGENT_STATE_WANDER: - self.target_loc = self.getTargetLocation() - MovingAgentBehaviour.onInstanceActionFinished(self, instance, action) - - - def idle(self): - """Controls the NPC when it is idling. Different actions - based on the NPC's state. - @return: None""" - if self.state == base._AGENT_STATE_NONE: - self.state = base._AGENT_STATE_IDLE - self.agent.act('stand', self.agent.getFacingLocation()) - elif self.state == base._AGENT_STATE_IDLE: - if self.wanderCounter > self.wanderRate: - self.wanderCounter = 0 - self.state = base._AGENT_STATE_WANDER - else: - self.wanderCounter += 1 - self.state = base._AGENT_STATE_NONE - - self.target_loc = self.getTargetLocation() - self.agent.act('stand', self.agent.getFacingLocation()) - elif self.state == base._AGENT_STATE_WANDER: - self.wander(self.target_loc) - self.state = base._AGENT_STATE_NONE - elif self.state == base._AGENT_STATE_TALK: - self.agent.act('stand', self.pc.getLocation()) - - def wander(self, location): - """Nice slow movement for random walking. - @type location: fife.Location - @param location: Where the NPC will walk to. - @return: None""" +# 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 . + +from random import randrange + +from fife import fife + +import base +from moving import MovingAgentBehaviour + +class NPCBehaviour(MovingAgentBehaviour): + """This is a basic NPC behaviour""" + def __init__(self, parent=None): + super(NPCBehaviour, self).__init__() + + self.parent = parent + self.state = base._AGENT_STATE_NONE + self.pc = None + self.target_loc = 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 == base._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 + @type action: ??? + @param action: ??? + @return: None""" + if self.state == base._AGENT_STATE_WANDER: + self.target_loc = self.getTargetLocation() + MovingAgentBehaviour.onInstanceActionFinished(self, instance, action) + + + def idle(self): + """Controls the NPC when it is idling. Different actions + based on the NPC's state. + @return: None""" + if self.state == base._AGENT_STATE_NONE: + self.state = base._AGENT_STATE_IDLE + self.animate('stand') + elif self.state == base._AGENT_STATE_IDLE: + if self.wanderCounter > self.wanderRate: + self.wanderCounter = 0 + self.state = base._AGENT_STATE_WANDER + else: + self.wanderCounter += 1 + self.state = base._AGENT_STATE_NONE + + self.target_loc = self.getTargetLocation() + self.animate('stand') + elif self.state == base._AGENT_STATE_WANDER: + self.wander(self.target_loc) + self.state = base._AGENT_STATE_NONE + elif self.state == base._AGENT_STATE_TALK: + self.animate('stand', self.pc.getLocation()) + + def wander(self, location): + """Nice slow movement for random walking. + @type location: fife.Location + @param location: Where the NPC will walk to. + @return: None""" self.agent.move('walk', location, self.speed - 1) \ No newline at end of file diff -r ccf4d6cffcf1 -r ecac92680bef src/parpg/behaviours/player.py --- a/src/parpg/behaviours/player.py Thu Sep 29 18:15:12 2011 +0200 +++ b/src/parpg/behaviours/player.py Fri Sep 30 14:04:29 2011 +0200 @@ -1,26 +1,25 @@ -# 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 . - -import moving -from moving import MovingAgentBehaviour - -class PlayerBehaviour (MovingAgentBehaviour): - def __init__(self, parent=None): - super(PlayerBehaviour, self).__init__() - self.parent = parent - self.idle_counter = 1 - self.speed = 0 - self.nextAction = None +# 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 . + +import moving +from moving import MovingAgentBehaviour + +class PlayerBehaviour (MovingAgentBehaviour): + def __init__(self, parent=None): + super(PlayerBehaviour, self).__init__() + self.parent = parent + self.idle_counter = 1 + self.speed = 0 self.agent = None \ No newline at end of file