Ver Fonte

created EntityCreator classes

Darren Ranalli há 22 anos atrás
pai
commit
5a06879434

+ 5 - 1
direct/src/level/DistributedLevel.py

@@ -138,13 +138,17 @@ class DistributedLevel(DistributedObject.DistributedObject,
         # currently-visible zones?
         self.localEntities = {}
         for entId, spec in self.entId2Spec.iteritems():
-            entity = EntityCreator.createEntity(spec['type'], self, entId)
+            entity = self.entityCreator.createEntity(spec['type'], self, entId)
             if entity is not None:
                 self.localEntities[entId] = entity
 
         # there should not be any pending reparents left
         assert len(self.parent2ChildIds) == 0
 
+    def makeEntityCreator(self):
+        """inheritors, override if desired"""
+        return EntityCreator.EntityCreator()
+
     def announceGenerate(self):
         self.notify.debug('announceGenerate')
         DistributedObject.DistributedObject.announceGenerate(self)

+ 5 - 1
direct/src/level/DistributedLevelAI.py

@@ -38,6 +38,10 @@ class DistributedLevelAI(DistributedObjectAI.DistributedObjectAI,
         self.startTimestamp = globalClockDelta.localToNetworkTime(
             self.startTime, bits=32)
 
+    def makeEntityCreator(self):
+        """inheritors, override if desired"""
+        return EntityCreatorAI.EntityCreatorAI()
+
     # required-field getters
     def getZoneIds(self):
         return self.zoneIds
@@ -58,7 +62,7 @@ class DistributedLevelAI(DistributedObjectAI.DistributedObjectAI,
         self.aiEntities = {}
         for entId, spec in self.entId2Spec.iteritems():
             self.notify.debug('creating %s %s' % (spec['type'], entId))
-            entity = EntityCreatorAI.createEntity(
+            entity = self.entityCreator.createEntity(
                 spec['type'], self.air, self.doId, entId,
                 self.getZoneId(spec['zone']))
             if entity is not None:

+ 28 - 27
direct/src/level/EntityCreator.py

@@ -1,36 +1,37 @@
-"""EntityCreator.py: contains methods for creation of Entities"""
+"""EntityCreator module: contains the EntityCreator class"""
 
-from PythonUtil import Functor
-import PlatformEntity
 import BasicEntities
+import DirectNotifyGlobal
 
+# some useful constructor functions
+# ctor functions must take (level, entId)
 def nothing(level, entId):
     """For entities that don't need to be created by the client"""
     return None
 
-# Client-side entity ctors:
-EntityType2Ctor = {
-    # Map entity type name to constructor function that takes
-    # (level, entId)
-    'beanBarrel': nothing,
-    'door': nothing,
-    'gagBarrel': nothing,
-    'lift': nothing,
-    'nodepath': BasicEntities.NodePathEntity,
-    'platform': PlatformEntity.PlatformEntity,
-    'stomper': nothing,
-    'stomperPair': nothing,
-    'switch': nothing,
+class EntityCreator:
+    """This class is responsible for creating instances of Entities on the
+    client. It can be subclassed to handle more Entity types."""
+    notify = DirectNotifyGlobal.directNotify.newCategory('EntityCreator')
+    
+    def __init__(self):
+        self.entType2Ctor = {}
+        self.privRegisterTypes({
+            'logicGate': nothing,
+            'nodepath': BasicEntities.NodePathEntity,
+            })
 
-    'andLogicObject': nothing,
-    'orLogicObject': nothing,
-    'xorLogicObject': nothing, #XorLoEntity.XorLoEntity,
-    }
+    def privRegisterType(self, entType, ctor):
+        assert(not self.entType2Ctor.has_key(entType))
+        self.entType2Ctor[entType] = ctor
 
-# TODO: what other args will be required?
-def createEntity(entType, level, entId):
-    if not EntityType2Ctor.has_key(entType):
-        print "createEntity(entType=%s, entId=%s) not found"%(
-                entType, entId)
-        return None
-    return EntityType2Ctor[entType](level, entId)
+    def privRegisterTypes(self, type2ctor):
+        for entType, ctor in type2ctor.items():
+            self.privRegisterType(entType, ctor)
+
+    def createEntity(self, entType, level, entId):
+        if not self.entType2Ctor.has_key(entType):
+            EntityCreator.notify.warning(
+                'createEntity(entType=%s) not found' % entType)
+            return None
+        return self.entType2Ctor[entType](level, entId)

+ 35 - 36
direct/src/level/EntityCreatorAI.py

@@ -1,18 +1,12 @@
-"""EntityCreatorAI.py: contains methods for creation of Entities"""
-
-from PythonUtil import Functor
-import DistributedBeanBarrelAI
-import DistributedLiftAI
-import DistributedDoorEntityAI
-import DistributedGagBarrelAI
-import DistributedStomperPairAI
-import DistributedSwitchAI
-import DistributedStomperAI
-import LogicGateAI
+"""EntityCreatorAI module: contains the EntityCreatorAI class"""
 
+import DirectNotifyGlobal
+import LogicGateAI
 
-def cDE(AIclass, air, levelDoId, entId, zoneId):
-    """create a distributed entity"""
+# some useful constructor functions
+# ctor functions must take (air, level doId, entId, zoneId)
+def createDistributedEntity(AIclass, air, levelDoId, entId, zoneId):
+    """create a distributed entity and call generate"""
     ent = AIclass(air, levelDoId, entId)
     ent.generateWithRequired(zoneId)
     return ent
@@ -21,26 +15,31 @@ def nothing(air, levelDoId, entId, zoneId):
     """Create entity that doesn't have a server side representation."""
     return None
 
-# Server (AI) side factory functions:
-EntityType2Ctor = {
-    # Map entity type name to constructor function that takes
-    # (air, level doId, entId, zoneId)
-    'beanBarrel': Functor(cDE, DistributedBeanBarrelAI.DistributedBeanBarrelAI),
-    'door': DistributedDoorEntityAI.DistributedDoorEntityAI,
-    'gagBarrel': Functor(cDE, DistributedGagBarrelAI.DistributedGagBarrelAI),
-    'lift': Functor(cDE, DistributedLiftAI.DistributedLiftAI),
-    'nodepath': nothing,
-    'platform': nothing,
-    'stomper': Functor(cDE, DistributedStomperAI.DistributedStomperAI),
-    'stomperPair': Functor(cDE, DistributedStomperPairAI.DistributedStomperPairAI),
-    'switch': DistributedSwitchAI.DistributedSwitchAI,
-
-    'logicGate': LogicGateAI.LogicGateAI,
-    }
-
-def createEntity(entType, air, levelDoId, entId, zoneId):
-    if not EntityType2Ctor.has_key(entType):
-        print "createEntity(entType=%s, air=%s, levelDoId=%s, entId=%s, zoneId=%s) not found"%(
-                entType, "the air", levelDoId, entId, zoneId)
-        return None
-    return EntityType2Ctor[entType](air, levelDoId, entId, zoneId)
+class EntityCreatorAI:
+    """This class is responsible for creating instances of Entities on the AI.
+    It can be subclassed to handle more Entity types."""
+    notify = DirectNotifyGlobal.directNotify.newCategory('EntityCreatorAI')
+
+    def __init__(self):
+        self.entType2Ctor = {}
+        self.privRegisterTypes({
+            'logicGate': LogicGateAI.LogicGateAI,
+            'nodepath': nothing,
+            })
+
+    def privRegisterType(self, entType, ctor):
+        assert(not self.entType2Ctor.has_key(entType))
+        self.entType2Ctor[entType] = ctor
+
+    def privRegisterTypes(self, type2ctor):
+        for entType, ctor in type2ctor.items():
+            self.privRegisterType(entType, ctor)
+
+    def createEntity(self, entType, air, levelDoId, entId, zoneId):
+        if not self.entType2Ctor.has_key(entType):
+            EntityCreatorAI.notify.warning(
+                'createEntity(entType=%s, levelDoId=%s, '
+                'entId=%s, zoneId=%s) not found' %
+                (entType, levelDoId, entId, zoneId))
+            return None
+        return self.entType2Ctor[entType](air, levelDoId, entId, zoneId)

+ 8 - 0
direct/src/level/LevelBase.py

@@ -2,6 +2,7 @@
 
 import DirectNotifyGlobal
 import string
+from PythonUtil import lineInfo
 
 class LevelBase:
     """LevelBase: shared client and AI code
@@ -33,6 +34,13 @@ class LevelBase:
         # this will be filled in as the entities are created and report in
         self.entities = {}
 
+        # get an entity creator object
+        self.entityCreator = self.makeEntityCreator()
+
+    def makeEntityCreator(self):
+        self.notify.error(
+            'concrete Level class must override %s' % lineInfo()[2])
+
     def destroyLevel(self):
         del self.entities
         del self.entId2Spec