def buildDistributionRects(tmxmap, layer, gid=None): """ generate a set of non-overlapping rects that represents the distribution of the specfied gid. if gid is not passed, then will choose one. useful for collision detection """ import maputils if gid == None: gid = tmxmap.gidmap[tmxmap.tilesets[layer].firstgid] layer_data = tmxmap.getLayerData(layer) p = product(xrange(tmxmap.width), xrange(tmxmap.height)) points = [(x, y) for (x, y) in p if layer_data[y][x] == gid] rects = maputils.simplify(points, tmxmap.tilewidth, tmxmap.tileheight) return rects
def buildDistributionRects(tmxmap, layer, gid=None): """ generate a set of non-overlapping rects that represents the distribution of the specfied gid. if gid is not passed, then will choose one. useful for collision detection """ import maputils if gid == None: gid = tmxmap.gidmap[tmxmap.tilesets[layer].firstgid] layer_data = tmxmap.getLayerData(layer) p = product(xrange(tmxmap.width), xrange(tmxmap.height)) points = [ (x,y) for (x,y) in p if layer_data[y][x] == gid ] rects = maputils.simplify(points, tmxmap.tilewidth, tmxmap.tileheight) return rects
def makeCollisionLayer(self, layer, gid=None): """ Builds a Quadtree for collision detection using tiles from the specified layer and tile. The quadtree can be queried for collisions between a rect and the layer, but not between objects in the layer. The quad tree reduces the number of checks that must be made and is overall better than a brute force collision check. """ import quadtree import maputils if gid == None: gid = self.data.gidmap[self.data.tilesets[-1].firstgid] layer_data = self.data.getLayerData(layer) p = product(range(self.data.width),range(self.data.height)) points = [ (x,y) for (x,y) in p if layer_data[y][x] == gid ] rects=maputils.simplify(points,self.data.tilewidth,self.data.tileheight) self.collisionQuadtree = quadtree.QuadTree(rects, 8)
msg = "Tileset must be either a int or string. got: {}" raise ValueError, msg.format(type(tileset)) gid = None if real_gid: try: gid, flags = tmxmap.mapGID(real_gid)[0] except KeyError, IndexError: msg = "GID #{} not found" raise ValueError, msg.format(real_gid) if isinstance(layer, int): layer_data = tmxmap.getLayerData(layer).data elif isinstance(layer, str): try: layer = [ l for l in tmxmap.layers if l.name == layer ].pop() layer_data = layer.data except IndexError: msg = "Layer \"{}\" not found in map {}." raise ValueError, msg.format(layer, tmxmap) p = product(xrange(tmxmap.width), xrange(tmxmap.height)) if gid: points = [ (x,y) for (x,y) in p if layer_data[y][x] == gid ] else: points = [ (x,y) for (x,y) in p if layer_data[y][x] ] rects = maputils.simplify(points, tmxmap.tilewidth, tmxmap.tileheight) return rects