Package arcmap :: Module tilemap
[hide private]
[frames] | no frames]

Source Code for Module arcmap.tilemap

  1  ################################################################################ 
  2  # Authors: Brian Schott (Sir Alaran) 
  3  # Copyright: Brian Schott (Sir Alaran) 
  4  # Date: Sep 29 2009 
  5  # License: 
  6  # 
  7  # This program is free software: you can redistribute it and/or modify 
  8  # it under the terms of the GNU General Public License as published by 
  9  # the Free Software Foundation, either version 3 of the License, or 
 10  # (at your option) any later version. 
 11  # 
 12  # This program is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 15  # GNU General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU General Public License 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 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") 
37 38 -class TileMap:
39 - def __init__(self):
40 # Measured in tiles 41 self.width = 0 42 # Measured in tiles 43 self.height = 0 44 # Measured in pixels 45 self.tileSize = 32 46 # Map's layers 47 self.layers = [] 48 # Map's shapes 49 self.shapes = [] 50 # Parallax backgrounds 51 self.backgrounds = [] 52 # Background fill color 53 self.bgColor = graphics.RGBA(0.0, 0.0, 0.0, 1.0) 54 # Image files used 55 self.images = [] 56 # Lights 57 self.lights = [] 58 # Name 59 self.name = "Unnamed Map" 60 # Description 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
94 - def addTile(self, t, x, y, z):
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 # Don't issue a warning here because the size of the tilegrid 112 # window is often greater than that of the map 113 return 114 while z > len(self.layers) - 1: 115 self.layers.append(Layer()) 116 return self.layers[z].addTile(t, x, y)
117
118 - def removeTile(self, x, y, z):
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
158 - def removeLayer(self, index):
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
169 - def swapLayers(self, index1, index2):
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
184 - def getParallaxes(self):
185 return copy.deepcopy(self.backgrounds)
186
187 - def setParallaxes(self, parallaxes):
188 self.backgrounds = parallaxes
189
190 - def getBGColor(self):
191 """ 192 @rtype: graphics.RGBA 193 @return: the map's background color 194 """ 195 return copy.copy(self.bgColor)
196
197 - def setBGColor(self, color):
198 self.bgColor = color
199
200 - def addShape(self, s):
201 """ 202 Brief Description 203 @type s: Shape 204 @param s: Placeholder 205 """ 206 self.shapes.append(s)
207
208 - def delShape(self, s):
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
216 - def getShapes(self):
217 return self.shapes
218
219 - def addLight(self, l):
220 """ 221 Brief Description 222 @type l: Light 223 @param l: Placeholder 224 """ 225 self.lights.append(l)
226
227 - def addImage(self, fileName, index):
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
239 - def createMap(tileSize, width, height):
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
256 - def readFromFile(fileName):
257 """ 258 Brief Description 259 @type fileName: string 260 @param fileName: Placeholder 261 """ 262 reader = mapio.MapReader.getReader(fileName) 263 return reader.getMap()
264
265 - def writeToFile(self, fileName):
266 """ 267 Brief Description 268 @type fileName: string 269 @param fileName: Placeholder 270 """ 271 writer = mapio.MapWriter.getWriter(fileName) 272 writer.writeInfo(self.width, self.height, self.tileSize, 273 self.name, self.description) 274 writer.writeLayers(self.layers) 275 writer.writeLights(self.lights) 276 writer.writeShapes(self.shapes) 277 writer.writeBackgrounds(self.backgrounds, self.bgColor) 278 writer.writeImages(self.images) 279 writer.finish()
280
281 282 -class Light(object):
283 - def __init__(self, r, sr, x, y):
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
299 300 -class Parallax(object):
301 - def __init__(self):
302 # Name of the file for the parallax background 303 self.fileName = None 304 # True if the background should be vertically tiled 305 self.vTile = False 306 # True if the background should be horizontally tiled 307 self.hTile = False 308 # True if the background should scroll vertically 309 self.vScroll = False 310 # True if the background should scroll horizontally 311 self.hScroll = False 312 # Rate at which the background scrolls vertically 313 self.vScrollSpeed = 1.0 314 # Rate at which the background scrolls horizontally 315 self.hScrollSpeed = 1.0 316 # Layer visibilty - can be turned off 317 self.visible = True
318
319 320 -class Layer:
321 - def __init__(self):
322 self.tiles = {} 323 self.name = "New Layer" 324 self.visible = True
325
326 - def addTile(self, tile, x, y):
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
348 - def removeTile(self, x, y):
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):
369 - def __init__(self, index, ix, iy):
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
383 - def getImageInfo(self):
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
390 - def setImageInfo(self, index, ix, iy):
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