diff options
Diffstat (limited to 'src/mapdata.py')
-rw-r--r-- | src/mapdata.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/mapdata.py b/src/mapdata.py index 5e65599..b43ff38 100644 --- a/src/mapdata.py +++ b/src/mapdata.py @@ -1,5 +1,21 @@ from common import * +TILE_SIZE = (24,24) +MAP_SIZE_IN_TILES = (512,512) +MAP_SIZE = (MAP_SIZE_IN_TILES[0] * TILE_SIZE[0], MAP_SIZE_IN_TILES[1] * TILE_SIZE[1]) + +class KPObject(object): + def __init__(self): + self.position = (0,0) + self.size = (1,1) + self.kind = 0 + self.cache = [] + self.tileset = None + + def updateCache(self): + self.cache = KP.map.tilesets[self.tileset].objects[self.kind].render(self.size) + + class KPLayer(object): def __repr__(self): return "<KPLayer %r>" % self.name @@ -9,6 +25,59 @@ class KPLayer(object): self.tileset = '' self.objects = [] self.visible = True + self.cache = [] + + def updateCache(self): + x1, x2 = MAP_SIZE_IN_TILES[0] - 1, 0 + y1, y2 = MAP_SIZE_IN_TILES[1] - 1, 0 + + for obj in self.objects: + pos, size = obj.position, obj.size + + if pos[0] < x1: + x1 = pos[0] + if pos[1] < y1: + y1 = pos[1] + if (pos[0] + size[0]) > x2: + x2 = pos[0] + size[0] + if (pos[1] + size[1]) > y2: + y2 = pos[1] + size[1] + + + # resize the cache if needed + cache = self.cache + size = (x2 - x1, y2 - y1) + width, height = size + oldSize = self.cacheSize + + if oldSize[1] < size[1]: + for i in xrange(size[1] - oldSize[1]): + cache.append([0 for i in xrange(width)]) + elif oldSize[1] > size[1]: + del cache[size[1]:] + + for i, elem in enumerate(cache): + if len(elem) != width: + cache[i] = [0 for i in xrange(width)] + + self.cacheBasePos = (x1, y1) + self.cacheSize = size + + # now generate the thing + for obj in self.objects: + oX, oY = obj.position + baseX = oX - x1 + y = oY - y1 + + for row in obj.cache: + destRow = cache[y] + x = baseX + for tile in row: + destRow[x] = tile + x += 1 + y += 1 + + class KPNodeAction(object): @@ -52,6 +121,7 @@ class KPMap(object): self.nodes = [] self.paths = [] self.doodads = [] + self.tilesets = {} # LAYERS |