Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented draw method and added tests.

  • Loading branch information...
commit af79b92537bc25d5d10cc446321a53e0253dd540 1 parent fa91118
@erdbeere authored
Showing with 50 additions and 16 deletions.
  1. +23 −12 tml/items.py
  2. +26 −3 tml/test_items.py
  3. +1 −1  tml/test_utils.py
View
35 tml/items.py
@@ -17,9 +17,9 @@
import png
-from .constants import ITEM_TYPES, TML_DIR, TILEFLAG_VFLIP, \
+from constants import ITEM_TYPES, TML_DIR, TILEFLAG_VFLIP, \
TILEFLAG_HFLIP, TILEFLAG_OPAQUE, TILEFLAG_ROTATE
-from .utils import ints_to_string
+from utils import ints_to_string
#GAMELAYER_IMAGE = PIL.Image.open(os.path.join(TML_DIR,
# os.extsep.join(('entities', 'png'))))
@@ -286,20 +286,25 @@ def select(self, x, y, w=1, h=1):
y = max(0, min(y, self.height-1))
w = max(1, min(w, self.width-x))
h = max(1, min(h, self.height-y))
- layer = TileLayer(w, h)
- layer.color = self.color
- layer.game = self.game
+ layer = TileLayer(w, h, game=self.game, color=self.color,
+ color_env=self.color_env,
+ color_env_offset=self.color_env_offset,
+ image_id=self.image_id)
+ data = []
for _y in range(h):
for _x in range(w):
- layer.tiles[_y*w+_x] = self.tiles.tiles[(y+_y)*self.width+(x+_x)]
+ data.append(self.tiles.tiles[(y+_y)*self.width+(x+_x)])
+ layer.tiles = TileManager(data=data)
if self.tele_tiles and len(self.tele_tiles.tiles) == len(self.tiles.tiles):
for _y in range(h):
for _x in range(w):
- layer.tele_tiles[_y*w+_x] = self.tele_tiles.tiles[(y+_y)*self.width+(x+_x)]
+ data.append(self.tele_tiles.tiles[(y+_y)*self.width+(x+_x)])
+ layer.tele_tiles = TileManager(data=data)
if self.speedup_tiles and len(self.speedup_tiles.tiles) == len(self.tiles.tiles):
for _y in range(h):
for _x in range(w):
- layer.speedup_tiles[_y*w+_x] = self.speedup_tiles.tiles[(y+_y)*self.width+(x+_x)]
+ data.append(self.speedup_tiles.tiles[(y+_y)*self.width+(x+_x)])
+ layer.speedup_tiles = TileManager(data=data)
return layer
def draw(self, x, y, tilelayer):
@@ -312,7 +317,14 @@ def draw(self, x, y, tilelayer):
x = max(0, min(x, self.width-1))
y = max(0, min(y, self.height-1))
- # TODO
+
+ for _y in range(tilelayer.height):
+ for _x in range(tilelayer.width):
+ tile = tilelayer.tiles[_y*tilelayer.width+_x]
+ try:
+ self.tiles[(y+_y)*self.width+(x+_x)] = tile
+ except IndexError:
+ pass
@property
def width(self):
@@ -537,8 +549,7 @@ class TileManager(object):
def __init__(self, size=0, tiles=None, data=None, _type=0):
self.type = _type
if tiles is not None:
- #TODO: convert Tiles to string
- self.tiles = tiles
+ self.tiles = [self._tile_to_string(tile) for tile in tiles]
elif data is not None:
self.tiles = data
else:
@@ -546,7 +557,7 @@ def __init__(self, size=0, tiles=None, data=None, _type=0):
def __getitem__(self, value):
if isinstance(value, slice):
- return TileManager(tiles=self.tiles[value])
+ return TileManager(data=self.tiles[value])
if self.type == 1:
return TeleTile(self.tiles[value])
if self.type == 2:
View
29 tml/test_items.py
@@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
import unittest
-from .items import Layer, TileLayer, TileManager, Tile, QuadLayer, QuadManager, \
+from items import Layer, TileLayer, TileManager, Tile, QuadLayer, QuadManager, \
Quad
class TestTileLayer(unittest.TestCase):
@@ -137,8 +137,31 @@ def test_resizing(self):
self.layer.height = -1
def test_draw(self):
- # TODO
- pass
+ layer = TileLayer(10, 5)
+ tile = Tile(10)
+ layer.set_tile(0, 0, tile)
+ layer.set_tile(0, 4, tile)
+ layer.set_tile(9, 0, tile)
+ layer.set_tile(9, 4, tile)
+ self.layer.set_tile(10, 5, Tile(5))
+
+ self.layer.draw(0, 0, layer)
+ self.assertEqual(self.layer.get_tile(0, 0).index, 10)
+ self.assertEqual(self.layer.get_tile(0, 4).index, 10)
+ self.assertEqual(self.layer.get_tile(9, 0).index, 10)
+ self.assertEqual(self.layer.get_tile(9, 4).index, 10)
+ self.assertEqual(self.layer.get_tile(10, 5).index, 5)
+
+ self.layer.draw(5, 3, layer)
+ self.assertEqual(self.layer.get_tile(5, 3).index, 10)
+ self.assertEqual(self.layer.get_tile(5, 7).index, 10)
+ self.assertEqual(self.layer.get_tile(14, 3).index, 10)
+ self.assertEqual(self.layer.get_tile(14, 7).index, 10)
+ self.assertEqual(self.layer.get_tile(10, 5).index, 0)
+
+ self.layer.draw(49, 48, layer)
+ self.assertEqual(self.layer.get_tile(49, 48).index, 10)
+ self.assertEqual(self.layer.get_tile(49, 49).index, 0)
class TestTileManager(unittest.TestCase):
View
2  tml/test_utils.py
@@ -2,7 +2,7 @@
import unittest
-from .utils import ints_to_string, string_to_ints
+from utils import ints_to_string, string_to_ints
TEST_INT = [-186256396, -2139062144, -2139062144, -2139062144, -2139062144,
-2139062144, -2139062144, -2139062272]
Please sign in to comment.
Something went wrong with that request. Please try again.