This is a simple creator-defined displayable that creates a tilemap by tiling other displayables.
An example of how to use this displayable will be written on the next post.
Code: Select all
## Tilemap displayable
init -2 python:
class Tilemap(renpy.Displayable):
"""
This creates a displayable by tiling other displayables. It has the following field values.
map - A 2-dimensional list of integers that represent index of a tileset.
tileset - A list of displayables that is used as a tile of tilemap.
tile_width - width of each tile.
tile_height - height of each tile.
area - Rectangle area of the displayable that will be rendered. If it's None, default, it renders all tiles.
"""
def __init__(self, map, tileset, tile_width, tile_height, **properties):
super(Tilemap, self).__init__(**properties)
self.map = map
self.tileset = tileset
self.tile_width = tile_width
self.tile_height = tile_height
self.area = None
def render(self, width, height, st, at):
render = renpy.Render(width, height)
# Blit all tiles into the render.
for y in xrange(len(self.map)):
for x in xrange(len(self.map[y])):
render.blit(renpy.render(self.tileset[self.map[y][x]], self.tile_width, self.tile_height, st, at), (x*self.tile_width, y*self.tile_height))
# Crop the render.
if self.area == None:
render = render.subsurface((0, 0, len(self.map[y])*self.tile_width, len(self.map)*self.tile_height))
else:
render = render.subsurface(self.area)
return render
def per_interact(self):
# Redraw per interact.
renpy.redraw(self, 0)
def visit(self):
# If the displayable has child displayables, this method should be overridden to return a list of those displayables.
return self.tileset