Added LevelLoader to help users load Level Editor data in game

This commit is contained in:
Gyedo Jeon 2010-02-02 01:43:49 +00:00
parent 461b684243
commit 8d085f039b
3 changed files with 69 additions and 6 deletions

View File

@ -45,12 +45,6 @@ class FileMgr:
moduleName = moduleName[:-3]
file, pathname, description = imp.find_module(moduleName, [dirname])
try:
if self.editor is None: # when loaded outside of LE
base.objectPalette = ObjectPalette()
base.protoPalette = ProtoPalette()
base.objectHandler = ObjectHandler(None)
base.objectMgr = ObjectMgr(None)
module = imp.load_module(moduleName, file, pathname, description)
except:
print 'failed to load %s'%fileName

View File

@ -0,0 +1,32 @@
"""
This is just a sample code.
LevelLoader should be rewritten
to be game specific.
You need to define which ObjectMgr, ObjectHandler,
ObjectPalette, ProtoPalette would be used by importing section.
Then declare them in initLoader function.
You also need to define defaultPath in initLoader function, too.
"""
import os
from direct.leveleditor.LevelLoaderBase import LevelLoaderBase
from direct.leveleditor.ObjectMgr import ObjectMgr
from direct.leveleditor.ProtoPalette import ProtoPalette
from direct.leveleditor import ObjectGlobals as OG
from ObjectHandler import ObjectHandler
from ObjectPalette import ObjectPalette
class LevelLoader(LevelLoaderBase):
def __init__(self):
LevelLoaderBase.__init__(self)
def initLoader(self):
self.defaultPath = os.path.dirname(__file__)
base.objectPalette = ObjectPalette()
base.protoPalette = ProtoPalette()
base.objectHandler = ObjectHandler(None)
base.objectMgr = ObjectMgr(None)

View File

@ -0,0 +1,37 @@
import imp
class LevelLoaderBase:
"""
Base calss for LevelLoader
which you will use to load level editor data in your game.
Refer LevelLoader.py for example.
"""
def __init__(self):
self.defaultPath = None # this should be set in your LevelLoader.py
self.initLoader()
def initLoader(self):
# You should implement this in subclass
raise NotImplementedError('populate() must be implemented in your LevelLoader.py')
def cleanUp(self):
# When you don't need to load any more data, you can call clean up
del base.objectPalette
del base.protoPalette
del base.objectHandler
del base.objectMgr
def loadFromFile(self, fileName, filePath=None):
if filePath is None:
filePath = self.defaultPath
if fileName.endswith('.py'):
fileName = fileName[:-3]
file, pathname, description = imp.find_module(fileName, [filePath])
try:
module = imp.load_module(fileName, file, pathname, description)
return True
except:
print 'failed to load %s'%fileName
return None