From 65ac156131c0cc874f5e908bb925a647ed3c8de8 Mon Sep 17 00:00:00 2001 From: Treeki Date: Fri, 4 Nov 2011 04:40:25 +0100 Subject: lots of unfinished tileset stuff --- src/common.py | 1 + src/mapdata.py | 70 +++++++++++++++++++++++++++++++++++++++++++++ src/tileset.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 src/tileset.py diff --git a/src/common.py b/src/common.py index b106ab5..3165ab4 100644 --- a/src/common.py +++ b/src/common.py @@ -3,6 +3,7 @@ from PyQt4 import QtCore, QtGui from PyQt4.QtCore import Qt from mapdata import * +from tileset import * from main import KP 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 "" % 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 diff --git a/src/tileset.py b/src/tileset.py new file mode 100644 index 0000000..7fa8733 --- /dev/null +++ b/src/tileset.py @@ -0,0 +1,89 @@ +from common import * + +class KPObjectDefinition(object): + def __init__(self): + self.rows = [] + + def render(self, size): + buf = [] + + beforeRepeat = [] + inRepeat = [] + afterRepeat = [] + + foundRepeat = False + for row in self.rows: + if (row[0][0] & 2) != 0: + inRepeat.append(row) + foundRepeat = True + else: + if foundRepeat: + afterRepeat.append(row) + else: + beforeRepeat.append(row) + + bC, iC, aC = len(beforeRepeat), len(inRepeat), len(afterRepeat) + + if iC == 0: + for y in xrange(size[1]): + buf.append(self._renderRow(beforeRepeat[y % bC], size[0])) + else: + middleUntil = size[1] - aC + + for y in xrange(size[1]): + if y < bC: + buf.append(self._renderRow(beforeRepeat[y], size[0])) + elif y < middleUntil: + buf.append(self._renderRow(inRepeat[(y - bC) % iC], size[0])) + else: + buf.append(self._renderRow(afterRepeat[y - bC - iC], size[0])) + + return buf + + def _renderRow(self, row, width): + buf = [-1 for i in xrange(width)] + + beforeRepeat = [] + inRepeat = [] + afterRepeat = [] + + foundRepeat = False + for tile in row: + if (tile[0] & 1) != 0: + inRepeat.append(tile[1]) + foundRepeat = True + else: + if foundRepeat: + afterRepeat.append(tile[1]) + else: + beforeRepeat.append(tile[1]) + + bC, iC, aC = len(beforeRepeat), len(inRepeat), len(afterRepeat) + + if iC == 0: + for x in xrange(width): + buf[x] = beforeRepeat[x % bC] + else: + middleUntil = width - aC + + for x in xrange(width): + if x < bC: + buf[x] = beforeRepeat[y] + elif x < middleUntil: + buf[x] = inRepeat[(y - bC) % iC] + else: + buf[x] = afterRepeat[y - bC - iC] + + return buf + + + + + + +class KPTileset(object): + def __init__(self): + self.texture = None + self.objects = [] + + -- cgit v1.2.3