-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMap.py
394 lines (311 loc) · 14.1 KB
/
Map.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import pygame
import ImageData
import DisplayInfo
import random
import GameObject
import string
import pygame.time
import GlobalData
import Item
import ItemWrapper
def loadTileSet(mapName, xNum, yNum, alpha = None):
GlobalData.textureManager.loadTexture(mapName,"images/" + mapName, alpha)
for y in range(0,xNum):
for x in range(0,yNum):
GlobalData.textureManager.spriteRects[mapName].append(pygame.rect.Rect(x*24,y*24,24,24))
def loadBattleBG(mapName, alpha = None):
GlobalData.textureManager.loadTexture("bg", "images/battle/bg/" + mapName +str(".png"), alpha)
class MapTile:
def __init__(self):
self.text = ""
self.name = ""
self.collision = 0
self.rect = None
self.backRect = None
self.tileSet = None
self.tileSetName = ""
self.contents = []
self.visible = 0
self.portal = ""
def __str__(self):
if self.rect is not None:
if self.rect.top == 0:
return "[]"
else:
return "[!]"
return "{}"
def displayTile(self, x, y):
if self.tileSetName != "":
#print self.name
if self.backRect is not None:
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures[self.tileSetName][0], (x,y), self.backRect)
#GlobalData.display.getScreen().fill((0,0,0), pygame.rect.Rect(x,y,24,24))
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures[self.tileSetName][0], (x, y), self.rect)
def setRect(self, rect):
self.rect = rect
def setTileSet(self, tileSet):
self.tileSet = tileSet
def getRect(self):
return self.rect
def getTileSet(self):
return self.tileSet
def setTileSetName(self, name):
self.tileSetName = name
def getName(self):
return self.name
def setName(self, name):
self.name = name
def getCollision(self):
return self.collision
def setCollision(self, collision):
self.collision = collision
def setText(self, text):
self.text = text
def getText(self):
return self.text
def getContents(self):
return self.contents
def setContents(self, contents):
self.contents = contents
def isVisible(self):
return self.visible == 1
class MapPiece:
def __init__(self, piece, name):
self.array = piece
self.name = name
#print self.collisions
def __str__(self):
for y in range(19):
for x in range(25):
if x != 24:
print str(self.array[x][y]) + "\t",
else:
print str(self.array[x][y])
return ""
def drawRow(self, row, xOff, yOff):
for y in range(19):
self.array[row][y].displayTile(xOff + row*24, yOff + y*24)
def draw(self, Xoff, Yoff):
#print Xoff
#print Yoff
for x in range(-1,26):
for y in range(-1,20):
self.array[x][y].displayTile(x*24 + Xoff, y*24 + Yoff)
def getName(self):
return self.name
class Map:
def __init__(self, mapName, startPieceName, startPieceOffsetX, startPieceOffsetY):
self.name = mapName
self.mapPieceWidth = None
self.mapPieceLength = None
self.hasMonsters = None
self.monsters = None
self.numMonsters = None
self.freq = None
self.getMapData()
self.objectManager = ObjectManager()
self.collisionRects = [[pygame.Rect(0,0,0,0) for i in range(19)] for j in range(25)]
self.mapPieces = [[MapTile() for i in range(self.mapPieceWidth)] for j in range(self.mapPieceLength)]
self.currentPiece = None
self.startPieceOffsetX = startPieceOffsetX
self.startPieceOffsetY = startPieceOffsetY
self.startPieceName = startPieceName
self.prevXoff = 0
self.prevYoff = 0
self.Xoff = 0
self.Yoff = 0
self.loadMap(mapName)
self.allTiles = [[MapTile() for i in range(30*self.mapPieceWidth)] for j in range(30*self.mapPieceLength)]
self.getAllTiles()
self.getCollisionRects()
if self.hasMonsters == True:
loadBattleBG(mapName)
#print GlobalData.textureManager.textures["bg"]
self.battleBG = GlobalData.textureManager.textures["bg"]
#self.monsters = ["creep", "goblin"]
def getMapData(self):
self.mapPieceWidth = int(GlobalData.mapsData[self.name][0])
self.mapPieceLength = int(GlobalData.mapsData[self.name][1])
self.hasMonsters = GlobalData.mapsData[self.name][2] == "True"
self.monsters = string.split(GlobalData.mapsData[self.name][3], ',')
#self.monsters[len(self.monsters)-1] = self.monsters[len(self.monsters)-1] Useless?
self.numMonsters = int(GlobalData.mapsData[self.name][4])
self.freq = int(GlobalData.mapsData[self.name][5])
def getCollisionRects(self):
for x in range(25):
for y in range(19):
if self.currentPiece.array[x][y].getCollision() == 1:
self.collisionRects[x][y] = (pygame.Rect(x*24 - 1, y*24 - 1, 26, 26))
else:
self.collisionRects[x][y] = (pygame.Rect(0, 0, 0, 0))
def printCollisionRects(self):
for y in range(19):
for x in range(25):
if x != 24:
print str(self.collisionRects[x][y].top) + "\t",
else:
print str(self.collisionRects[x][y].top)
return ""
def getAllTiles(self):
self.xCount = 0
self.yCount = 0
self.yBase = -1
self.xBase = -1
#self.count = 0
for y in range(self.mapPieceLength):
self.yBase = -1
for x in range(self.mapPieceWidth):
self.xCount = self.xBase + 1
for v in range(30):
self.yCount = self.yBase + 1
for z in range(30):
#print "V:" + str(v)
#print "X:" + str(x)
#print "Y:" + str(y)
#print "xCount:" + str(self.xCount)
#print "yCount:" + str(self.yCount)
#print "xBase:" + str(self.xBase)
#print "yBase:" + str(self.yBase)
#print self.mapPieces[x][y].array[v][z].getName()
self.allTiles[self.xCount][self.yCount] = self.mapPieces[x][y].array[v][z]
self.yCount += 1
#self.count += 1
self.xCount += 1
self.yBase += 30
self.xBase += 30
#print self.count
def loadMapPiece(self, name, xNum, yNum, mapPieceName):
self.file = "data/" + name[:-2] + "/" + name
self.workingMap = [[MapTile() for i in range(30)] for j in range(30)]
self.tmpList = []
self.fileToLoad = open(self.file, 'r')
for x in range(30):
for y in range(30):
self.tmpList = string.split(self.fileToLoad.readline(), ',')
self.workingMap[x][y].text = self.tmpList[0]
self.workingMap[x][y].name = self.tmpList[1]
self.workingMap[x][y].collision = int(self.tmpList[2])
if self.tmpList[3] == "None":
self.workingMap[x][y].rect = None
continue
elif self.tmpList[3] != "None":
self.workingMap[x][y].rect = pygame.Rect(int(self.tmpList[3]), int(self.tmpList[4]),24,24)
if self.tmpList[5] != "":
self.workingMap[x][y].tileSetName = self.tmpList[5]
self.workingMap[x][y].tileSet = GlobalData.textureManager.textures[self.tmpList[5]][0]
if self.tmpList[6] != '':
self.splitList = self.tmpList[6].split(';')
#print self.splitList
for z in self.splitList:
self.workingMap[x][y].contents.append(ItemWrapper.ItemWrapper(z, 1))
#print self.workingMap[x][y].contents
self.workingMap[x][y].portal = self.tmpList[7]
if self.tmpList[8][:-1] == "None":
self.workingMap[x][y].backRect = None
continue
else:
self.workingMap[x][y].backRect = pygame.Rect(int(self.tmpList[8]), int(self.tmpList[9]), 24, 24)
self.fileToLoad.close()
self.mapPieces[xNum][yNum] = MapPiece(self.workingMap, mapPieceName)
def drawMap(self):
if self.Xoff != self.prevXoff:
if self.Xoff % 24 == 0:
for x in range(-1,26):
for y in range(-1,20):
self.currentPiece.array[x][y] = self.allTiles[-self.Xoff/24 + x + self.startPieceOffsetX][-self.Yoff/24 + y + self.startPieceOffsetY]
if self.Yoff != self.prevYoff:
if self.Yoff % 24 == 0:
for x in range(-1,26):
for y in range(-1,20):
self.currentPiece.array[x][y] = self.allTiles[-self.Xoff/24 + x + self.startPieceOffsetX][-self.Yoff/24 + y + self.startPieceOffsetY]
#print "Y+1"
#self.currentPiece.drawRow(24, self.Xoff, self.Yoff)
#self.currentPiece.drawRow(25, self.Xoff, self.Yoff)
#self.printCollisionRects()
#print "X:" + str(self.Xoff)
#print "PrevX:" + str(self.prevXoff)
if self.Xoff > self.prevXoff:
GlobalData.display.getScreen().fill((0,0,0))
self.currentPiece.draw((self.Xoff%24), self.Yoff%24)
#print "Left"
elif self.Xoff < self.prevXoff:
#print 24 - (self.Xoff%24)
GlobalData.display.getScreen().fill((0,0,0))
self.currentPiece.draw(-(24-(self.Xoff%24)), self.Yoff%24)
#print "Right"
elif self.Yoff < self.prevYoff:
GlobalData.display.getScreen().fill((0,0,0))
self.currentPiece.draw(self.Xoff%24, -(24 - (self.Yoff%24)))
#print "Up"
elif self.Yoff > self.prevYoff:
GlobalData.display.getScreen().fill((0,0,0))
self.currentPiece.draw(self.Xoff%24, (self.Yoff%24))
#print "Down"
elif self.Xoff%24 == 0 and self.Yoff%24 == 0:
GlobalData.display.getScreen().fill((0,0,0))
self.currentPiece.draw(self.Xoff%24, self.Yoff%24)
#print "Stop"
self.prevYoff = self.Yoff
self.prevXoff = self.Xoff
self.getCollisionRects()
#print str(self.currentPiece)
#def drawRects(self):
# for x in self.collisionRects:
# for y in x:
def loadMap(self, mapName):
self.count = 0
for x in range(self.mapPieceWidth):
#self.count = 0
for y in range(self.mapPieceLength):
if self.count == 7:
self.count = 0
self.loadMapPiece(mapName + str("-") + str(self.count), x, y, mapName + str("-") + str(self.count))
if mapName + str("-") + str(self.count) == self.startPieceName:
self.currentPiece = self.mapPieces[x][y]
self.count += 1
#print self.count
#print str (self.currentPiece)
def setXYoff(self, x, y):
self.prevXoff = self.Xoff
self.prevYoff = self.Yoff
self.Xoff = x
self.Yoff = y
def getXoff(self):
return self.Xoff
def getYoff(self):
return self.Yoff
class Object(GameObject.GameObject):
def __init__(self, graphicsData, name, surf, objectID, collision):
GameObject.GameObject.__init__(self)
self.name = name
self.surf = surf
self.tileSurface = graphicsData.textures[name + str(".png")][0]
self.tileSprite = graphicsData.spriteRects[name + str(".png")][objectID]
self.collision = collision
def render(self,x,y):
rect = self.surf.blit(self.tileSurface,(x,y), self.tileSprite)
class ObjectManager:
def __init__(self):
self.surf = GlobalData.display.getScreen()
self.objects = []
self.collisionRects = []
self.Xoff = 0
self.Yoff = 0
def makeObjects(self, low, high, collision, name):
for x in range(low, high):
self.objects.append(Object(name, self.surf, x, collision))
def drawObjects(self):
self.tmpCollisions = []
#for x in range(1, 50):
# self.objects[x].render(32*x + self.Xoff, 32*x + self.Yoff)
# self.objects[x].setPosition(32*x + self.Xoff, 32*x + self.Yoff)
# if self.objects[x].collision == 1:
# self.tmpCollisions.append(pygame.Rect(32*x + self.Xoff - 1, 32*x + self.Yoff -1, 32 + 2, 32 + 2))
self.collisionRects = self.tmpCollisions
def setXYoff(self, x, y):
self.Xoff = x
self.Yoff = y
def getXoff(self):
return self.Xoff
def getYoff(self):
return self.Yoff