1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """
23 Contains classes to represent the tile map.
24 """
25
26
27 __docformat__ = "epytext"
28
29 import copy
30 import logging
31
32 import mapio
33
34 import graphics
35
36 log = logging.getLogger("tilemap")
40
41 self.width = 0
42
43 self.height = 0
44
45 self.tileSize = 32
46
47 self.layers = []
48
49 self.shapes = []
50
51 self.backgrounds = []
52
53 self.bgColor = graphics.RGBA(0.0, 0.0, 0.0, 1.0)
54
55 self.images = []
56
57 self.lights = []
58
59 self.name = "Unnamed Map"
60
61 self.description = "Description of " + self.name
62
63 - def resize(self, width, height, xoffset, yoffset):
64 """
65 Resizes the map
66 @type width: int
67 @param width: The new width
68 @type height: int
69 @param height: The new height
70 @type xoffset: int
71 @param xoffset: the number of tiles that the existing tiles sholuld be
72 shifted right. This number can be negative.
73 @type yoffset: int
74 @param yoffset: the number of tiles that the existing tiles should be
75 shifted down. This number can be negative.
76 """
77 if width == self.width and height == self.height:
78 return
79 for l in self.layers:
80 newTiles = {}
81 for key, value in l.tiles.iteritems():
82 oldX = key[0]
83 oldY = key[1]
84 newX = oldX + xoffset
85 newY = oldY + yoffset
86 if newX < width and newY < height and newX >= 0 and newY >= 0:
87 newTiles[(newX, newY)] = value
88 l.tiles = newTiles
89 self.width = width
90 self.height = height
91 for s in self.shapes:
92 s.shift(xoffset * self.tileSize, yoffset * self.tileSize)
93
95 """
96 Adds a tile to the map
97 @type t: Tile
98 @param t: The tile
99 @type x: int
100 @param x: x-coordinate
101 @type y: int
102 @param y: y-coordinate
103 @type z: int
104 @param z: layer index
105 @rtype: (int, int, int)
106 @return: The x-coordinate, y-coordinate, and index of the image of the
107 tile that used to be at the coordinates (x, y), or None if there
108 was no tile there before
109 """
110 if x > self.width or y > self.height:
111
112
113 return
114 while z > len(self.layers) - 1:
115 self.layers.append(Layer())
116 return self.layers[z].addTile(t, x, y)
117
119 """
120 Removes a tile from the map
121 @type x: int
122 @param x: x-coordinate
123 @type y: int
124 @param y: y-coordinate
125 @type z: int
126 @param z: layer index
127 @rtype: (int, int, int)
128 @return: The x-coordinate, y-coordinate, and index of the image of the
129 tile that used to be at the coordinates (x, y, z), or None if there
130 was no tile there before
131 """
132 if z > len(self.layers):
133 return None
134 else:
135 return self.layers[z].removeTile(x, y)
136
137 - def addLayer(self, name, visible, z = -1):
138 """
139 Brief Description
140 @type name: string
141 @param name: Placeholder
142 @type visible: bool
143 @param visible: whether or not the layer is visible
144 @type z: int
145 @param z: index of the new layer
146 """
147 if z != -1:
148 while z > len(self.layers) - 1:
149 self.layers.append(Layer())
150 self.layers[z].name = name
151 self.layers[z].visible = visible
152 else:
153 self.layers.append(Layer())
154 self.layers[-1].name = name
155 self.layers[-1].visible = visible
156 z = -1
157
159 """
160 Brief Description
161 @type index: int
162 @param index: Placeholder
163 """
164 if index > len(self.layers) - 1:
165 loge("Tried to remove a non-existant layer")
166 else:
167 self.layers.remove(self.layers[index])
168
170 """
171 Brief Description
172 @type index1: int
173 @param index1: Placeholder
174 @type index2: int
175 @param index2: Placeholder
176 """
177 if index1 > len(self.layers) -1 or index2 > len(self.layers) - 1:
178 loge("TileMap.swapLayers: Swap indicies out of range.")
179 else:
180 temp = self.layers[index1]
181 self.layers[index1] = self.layers[index2]
182 self.layers[index2] = temp
183
185 return copy.deepcopy(self.backgrounds)
186
188 self.backgrounds = parallaxes
189
191 """
192 @rtype: graphics.RGBA
193 @return: the map's background color
194 """
195 return copy.copy(self.bgColor)
196
199
201 """
202 Brief Description
203 @type s: Shape
204 @param s: Placeholder
205 """
206 self.shapes.append(s)
207
209 """
210 @type s: shapes.Shape
211 @param s: the shape to remove
212 """
213 if s in self.shapes:
214 self.shapes.remove(s)
215
218
220 """
221 Brief Description
222 @type l: Light
223 @param l: Placeholder
224 """
225 self.lights.append(l)
226
228 """
229 @type fileName: string
230 @param fileName: file name of the image to add
231 @type index: int
232 @param index: index of the image
233 """
234 while index > len(self.images) - 1:
235 self.images.append(None)
236 self.images[index] = fileName
237
238 @staticmethod
240 """
241 @type tileSize: placeholder
242 @param tileSize: placeholder
243 @type width: placeholder
244 @param width: placeholder
245 @type height: placeholder
246 @param height: placeholder
247 """
248 m = TileMap()
249 m.width = width
250 m.height = height
251 m.tileSize = tileSize
252 m.addLayer("New Layer", True)
253 return m
254
255 @staticmethod
257 """
258 Brief Description
259 @type fileName: string
260 @param fileName: Placeholder
261 """
262 reader = mapio.MapReader.getReader(fileName)
263 return reader.getMap()
264
280
284 """
285 @type r: number
286 @param r: the outer radius of the light
287 @type sr: number
288 @param sr: the radius of the light source itself
289 @type x: number
290 @param x: the x coordinate of the light
291 @type y: number
292 @param y: the y coordinate of the light
293 """
294 self.outerradius = r
295 self.sourceradius = sr
296 self.center = Point(x, y)
297 self.color = color.RGBA(1.0, 1.0, 0.0, 1.0)
298
302
303 self.fileName = None
304
305 self.vTile = False
306
307 self.hTile = False
308
309 self.vScroll = False
310
311 self.hScroll = False
312
313 self.vScrollSpeed = 1.0
314
315 self.hScrollSpeed = 1.0
316
317 self.visible = True
318
322 self.tiles = {}
323 self.name = "New Layer"
324 self.visible = True
325
327 """
328 Brief Description
329 @type tile: Tile
330 @param tile: Placeholder
331 @type x: int
332 @param x: Placeholder
333 @type y: int
334 @param y: Placeholder
335 @rtype: (int, int, int)
336 @return: The x-coordinate, y-coordinate, and index of the image of the
337 tile that used to be at the coordinates (x, y), or None if there
338 was no tile there before
339 """
340 if (x, y) in self.tiles:
341 r = self.tiles[(x, y)].getImageInfo()
342 self.tiles[(x, y)] = tile
343 return r
344 else:
345 self.tiles[(x, y)] = tile
346 return None
347
349 """
350 Brief Description
351 @type x: int
352 @param x: Placeholder
353 @type y: int
354 @param y: Placeholder
355 @rtype: (int, int, int)
356 @return: The x-coordinate, y-coordinate, and index of the image of the
357 tile that used to be at the coordinates (x, y), or None if there
358 was no tile there before
359 """
360 if (x, y) in self.tiles:
361 r = self.tiles[(x, y)].getImageInfo()
362 del self.tiles[(x, y)]
363 return r
364 else:
365 return None
366
367
368 -class Tile(object):
370 """
371 Brief Description
372 @type index: int
373 @param index: Image index
374 @type ix: int
375 @param ix: Image x-coordinate
376 @type iy: int
377 @param iy: Image y-coordinate
378 """
379 self.__ix = ix
380 self.__iy = iy
381 self.__index = index
382
384 """
385 @rtype: (int, int, int)
386 @return: the image x-coordinate, y-coordinate, and index
387 """
388 return self.__ix, self.__iy, self.__index
389
391 """
392 @type index: int
393 @param index: image index
394 @type ix: int
395 @param ix: x-coordinate of image
396 @type iy: int
397 @param iy: y-coordinate of image
398 """
399 self._ix = ix
400 self._iy = iy
401 self._index = index
402