|
|
@@ -1,10 +1,13 @@
|
|
|
"""Entity.py: contains the Entity class"""
|
|
|
|
|
|
import string
|
|
|
+import DirectNotifyGlobal
|
|
|
+from PythonUtil import lineInfo
|
|
|
|
|
|
class Entity:
|
|
|
"""Entity is the base class for all objects that exist in a Level
|
|
|
and can be edited with the LevelEditor."""
|
|
|
+ notify = DirectNotifyGlobal.directNotify.newCategory('Entity')
|
|
|
|
|
|
# these are values that can be changed in the level editor
|
|
|
Attribs = (
|
|
|
@@ -22,6 +25,9 @@ class Entity:
|
|
|
if attribs is not None:
|
|
|
self.attribs.update(attribs)
|
|
|
|
|
|
+ def __str__(self):
|
|
|
+ return 'ent%s(%s)' % (self.entId, self.level.getEntityType(self.entId))
|
|
|
+
|
|
|
def initializeEntity(self):
|
|
|
"""Call this once on initialization to set this entity's
|
|
|
spec data"""
|
|
|
@@ -36,33 +42,62 @@ class Entity:
|
|
|
return getattr(self, setFuncName)
|
|
|
return None
|
|
|
|
|
|
- def callSetters(self, *attribs):
|
|
|
- """call this with a list of attribs, and any that exist on the
|
|
|
- entity and have setters will be passed to their setter"""
|
|
|
+ def privCallSetters(self, doDelete, *attribs):
|
|
|
+ """common implementation of callSetters and callSettersAndDelete"""
|
|
|
for attrib in attribs:
|
|
|
if hasattr(self, attrib):
|
|
|
setter = self.privGetSetter(attrib)
|
|
|
if setter is not None:
|
|
|
- setter(getattr(self, attrib))
|
|
|
+ value = getattr(self, attrib)
|
|
|
+ if doDelete:
|
|
|
+ delattr(self, attrib)
|
|
|
+ setter(value)
|
|
|
+
|
|
|
+ def callSetters(self, *attribs):
|
|
|
+ """call this with a list of attribs, and any that exist on the
|
|
|
+ entity and have setters will be passed to their setter"""
|
|
|
+ self.privCallSetters(0, *attribs)
|
|
|
|
|
|
- def attribChanged(self):
|
|
|
- """This is called when a parameter is tweaked and no setter
|
|
|
- is called; i.e. the value is set directly on the object.
|
|
|
- Some Entities might want to completely reset every time anything
|
|
|
- is tweaked; this is the place to do it; override this func in your
|
|
|
- derived class
|
|
|
- """
|
|
|
- pass
|
|
|
+ def callSettersAndDelete(self, *attribs):
|
|
|
+ """same as callSetters, but also removes attribs from entity"""
|
|
|
+ self.privCallSetters(1, *attribs)
|
|
|
|
|
|
- def getAttribInfo(self):
|
|
|
- return self.attribs
|
|
|
+ # this will be called with each item of our spec data on initialization
|
|
|
+ def setAttribInit(self, attrib, value):
|
|
|
+ if hasattr(self, attrib):
|
|
|
+ Entity.notify.warning('%s already has member %s in %s' %
|
|
|
+ (self, attrib, lineInfo()[2]))
|
|
|
+ # TODO: we should probably put this crep in a dictionary
|
|
|
+ # rather than dump it into the entity's namespace
|
|
|
+ self.__dict__[attrib] = value
|
|
|
|
|
|
- def privTweak(self, name, value):
|
|
|
- self.__dict__[name] = value
|
|
|
+ if __debug__:
|
|
|
+ # support for level editing
|
|
|
+ def handleAttribChange(self, attrib, value):
|
|
|
+ # call callback function if it exists
|
|
|
+ # otherwise set attrib directly and call notify func
|
|
|
+ setter = self.privGetSetter(attrib)
|
|
|
+ if setter is not None:
|
|
|
+ # call the setter
|
|
|
+ setter(value)
|
|
|
+ else:
|
|
|
+ # set the attrib directly
|
|
|
+ self.__dict__[attrib] = value
|
|
|
+ # and call the notify func
|
|
|
+ self.attribChanged(attrib)
|
|
|
+
|
|
|
+ def attribChanged(self, attrib):
|
|
|
+ """This is called when a parameter is tweaked and no setter
|
|
|
+ is called; i.e. the value is set directly on the object.
|
|
|
+ Some Entities might want to completely reset every time anything
|
|
|
+ is tweaked; this is the place to do it, just override this func
|
|
|
+ in your derived class
|
|
|
+ """
|
|
|
+ pass
|
|
|
+
|
|
|
+ def getAttribInfo(self):
|
|
|
+ return self.attribs
|
|
|
|
|
|
- def __str__(self):
|
|
|
- return 'ent%s(%s)' % (self.entId, self.level.getEntityType(self.entId))
|
|
|
-
|
|
|
if __debug__:
|
|
|
def debugPrint(self, message):
|
|
|
"""for debugging"""
|