summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorColin Noga <Tempus@Spectrum-Song.local>2011-11-04 22:02:59 -0500
committerColin Noga <Tempus@Spectrum-Song.local>2011-11-04 22:02:59 -0500
commita8d9f1a2ccfb412af8b9d0a9398247204bfc05d1 (patch)
treeae17551d1a7183a645674f3c0240f48de087e392 /src
parent38e50fb9870ff1b253a28ba6143339727a5b476f (diff)
parente47b198bb0d967282de1e170d9d0fe17e45b4910 (diff)
downloadkoopatlas-a8d9f1a2ccfb412af8b9d0a9398247204bfc05d1.tar.gz
koopatlas-a8d9f1a2ccfb412af8b9d0a9398247204bfc05d1.zip
Merge branch 'master' of ssh://treeki.shacknet.nu:30000/Koopatlas
Diffstat (limited to 'src')
-rw-r--r--src/common.py1
-rw-r--r--src/mapdata.py70
-rw-r--r--src/tileset.py89
3 files changed, 160 insertions, 0 deletions
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 "<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
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 = []
+
+