-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLevel.py
140 lines (110 loc) · 3.68 KB
/
Level.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import TerrainClass
import TerrainManager
from panda3d.core import PandaNode, NodePath
from math import sqrt
class Level ( object ):
def __init__(self, levelFile ):
'''load the levelFile'''
'''class variables'''
self.terrainMgr = TerrainManager.TerrainManager()
self.modelList = []
self.levelRoot=None
try:
'''test for file'''
temp = levelFile.readline
del temp
except:
'''level file is a string'''
self.levelFile = open( levelFile )
self.readFile()
temp = 16
self.levelRoot.setScale( (temp,temp,temp) )
del temp
def getRoot(self):
return self.levelRoot
def nextNonComment(self, levelFile ):
line = "#"
while line[0] is '#' or line[0] is '\n' :
line = levelFile.readline()
line = line.strip()
if len ( line ) is 0:
return line
return line
def parseNumMapTiles(self,line):
where = line.find("mapTiles=")
if where is -1:
return -1
return int (line[where+len("mapTiles="):] )
def parseNumModels( self,line ):
partList = line.partition( "=")
if partList[2] is "":
string = "parsing error on line expected model=x recieved : "+line
raise Exception(string )
else :
return int ( partList[2] )
def parseModelLine( self,line ):
'''take a line as input and output a dictionary
containing all the models data as output'''
toRet = {}
split = line.split()
for part in split :
pieces = part.partition('=')
toRet[pieces[0]] = pieces[2]
if __debug__ is True or True:
listing = toRet.keys()
listing.sort()
expected = ['h','model','p','x','y','r','z','sx','sy','sz']
expected.sort()
if str(listing) is str(expected) :
print "bad keys in level file's model listing"
print " expected:",expected
print " recieved:",listing
return toRet
def readFile(self):
self.terrainList = []
'''get version of encoding'''
line = self.nextNonComment( self.levelFile )
if line != "version=0.01" :
print "Level.py: Level.readFile(): bad version on",self.levelFile
'''get number of mapTiles'''
line = self.nextNonComment( self.levelFile)
numMapTiles = self.parseNumMapTiles(line)
'''make sure we can make a square out of the images'''
if numMapTiles is not int( sqrt( numMapTiles) )**2:
raise Exception( " can't make level with non square image set" )
for i in xrange( numMapTiles ):
line = self.nextNonComment( self.levelFile )
parts = line.split() # split the line on the space if any
'''first part is the height map, second is the texture(if there)'''
heightMapName=parts[0]
textureName=parts[0]
if len( parts) == 2 :
textureName=parts[1]
elif len(parts)>2:
raise Exception("Error, to many parts in: "+line)
self.terrainMgr.newTerrainPart( heightMapName, textureName )
base.taskMgr.add( self.terrainMgr.update,"1" )
'''base.taskMgr.add( self.terrainList[i].asyncUpdate,
str(self.terrainList[i] ))i'''
'''make our root the terrainMgr root'''
self.levelRoot = self.terrainMgr.getRoot()
'''read in the models and their locations'''
numModels = self.parseNumModels( self.nextNonComment( self.levelFile ) )
for item in xrange(numModels):
line = self.nextNonComment( self.levelFile )
specs = self.parseModelLine( line )
node = loader.loadModel(specs["model"] )
node.reparentTo( self.levelRoot )
node.setX( float( specs["x"] ) )
node.setY( float( specs["y"] ) )
node.setZ( float( specs["z"] ) )
node.setH( float( specs["h"] ) )
node.setR( float( specs["r"] ) )
node.setP( float( specs["p"] ) )
node.setSx( float (specs["sx"] ))
node.setSy( float (specs["sy"] ))
node.setSz( float (specs["sz"] ))
self.modelList.append( item )
self.levelFile.close()
print "levelLoaded"
return # finished loading the level