comparison demos/rpg/scripts/actors/baseactor.py @ 517:c3a026cdd91b

Added the BaseGameObject class and put it in it's own file. All game object will inherit it. Added instance action listeners.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 26 May 2010 15:28:25 +0000
parents d70fc46c8aa5
children 14f777be6b94
comparison
equal deleted inserted replaced
516:d70fc46c8aa5 517:c3a026cdd91b
27 import sys, os, re, math, random, shutil 27 import sys, os, re, math, random, shutil
28 28
29 from fife import fife 29 from fife import fife
30 from fife.extensions.loaders import loadMapFile 30 from fife.extensions.loaders import loadMapFile
31 31
32 ActorStates = {'IDLE':0, 32 from scripts.objects.baseobject import ObjectActionListener, BaseGameObject
33
34 ActorStates = {'STAND':0,
33 'WALK':1, 35 'WALK':1,
34 'ATTACK':2} 36 'ATTACK':2}
35 37
36 class Actor(object): 38 class ActorActionListener(ObjectActionListener):
39 def __init__(self, gamecontroller, obj):
40 super(ActorActionListener, self).__init__(gamecontroller, obj)
41
42 def onInstanceActionFinished(self, instance, action):
43 if action.getId() == 'walk':
44 self._object.stand()
45
46 class Actor(BaseGameObject):
37 def __init__(self, gamecontroller, instancename, instanceid=None, createInstance=False): 47 def __init__(self, gamecontroller, instancename, instanceid=None, createInstance=False):
38 """ 48 super(Actor, self).__init__(gamecontroller, instancename, instanceid, createInstance)
39 @param gamecontroller: A reference to the master game controller 49
40 @param instancename: The name of the object to load. The object's XML file must 50 self._walkspeed = self._gamecontroller.settings.get("RPG", "DefaultActorWalkSpeed", 4.0)
41 be part of the map file or added with fife.extensions.loaders.loadImportFile
42 @param instanceid: used if you want to give a specific ID to the instance to
43 differenciate it from other instances
44 @param createInstance: If this is True it will attempt to be loaded from disk and not
45 use one that has already been loaded from the map file. See the note about the
46 instancename paramater above.
47 """
48 self._gamecontroller = gamecontroller
49 self._fifeobject = None
50 self._name = instancename
51 if instanceid:
52 self._id = instanceid
53 else:
54 self._id = self._name
55
56 self._instance = None
57 51
58 if createInstance: 52 self._actionlistener = ActorActionListener(self._gamecontroller, self)
59 self._createFIFEInstance()
60 else:
61 self._instance = self._gamecontroller.scene.actorlayer.getInstance(self._id)
62 self._instance.thisown = 0
63
64 self._walkspeed = self._gamecontroller.settings.get("RPG", "DefaultActorWalkSpeed", 4.0)
65 self._state = ActorStates["IDLE"]
66 53
67 def destroy(self): 54 self.stand()
68 """ 55
69 Deletes the FIFE instance from the actor layer on the map. 56 def stand(self):
70 """ 57 self._state = ActorStates["STAND"]
71 if self._instance : 58 self._instance.act('stand', self._instance.getFacingLocation())
72 self._gamecontroller.scene.actorlayer.deleteInstance(self._instance) 59
73 self._instance = None
74
75 def walk(self, location): 60 def walk(self, location):
76 self._state = ActorStates["WALK"] 61 self._state = ActorStates["WALK"]
77 self._instance.move('walk', location, self._walkspeed) 62 self._instance.move('walk', location, self._walkspeed)
63
64 def _getState(self):
65 return self._state
66
67 def _setState(self, state):
68 self._state = state
78 69
79 def _createFIFEInstance(self): 70 state = property(_getState, _setState)
80 """
81 Should not be called directly. Use the constructor!
82 """
83 mapmodel = self._gamecontroller.engine.getModel()
84 self._fifeobject = mapmodel.getObject(self._name, self._gamecontroller.settings.get("RPG", "ObjectNamespace", "http://www.fifengine.de/xml/rpg"))
85
86 self._instance = self._gamecontroller.scene.actorlayer.createInstance(self._fifeobject, fife.ModelCoordinate(0,0), self._id)
87 fife.InstanceVisual.create(self._instance)
88 self._instance.thisown = 0
89
90 def _getLocation(self):
91 return self._instance.getLocation()
92
93 def _setLocation(self, loc):
94 self._instance.setLocation(loc)
95
96 location = property(_getLocation,_setLocation)