Skip to content

Commit 381fd9c

Browse files
committed
finish off the rect map collider; fix scrolling viewport setting from a Window
1 parent d3f9f38 commit 381fd9c

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

cocos/layer/scrolling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def on_enter(self):
186186
def update_view_size(self):
187187
if self.viewport is not None:
188188
self.view_w, self.view_h = self.viewport.width, self.viewport.height
189-
self.view_x, self.view_y = self.viewport.position
189+
self.view_x, self.view_y = getattr(self.viewport, 'position', (0,0))
190190
if director.do_not_scale_window:
191191
self._scissor_flat = (self.view_x, self.view_y,
192192
self.view_w, self.view_h)

cocos/tiles.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ def __init__(self, id, tw, th, cells, origin=None, properties=None):
808808
number of colums in cells
809809
`th` : int
810810
number of rows in cells
811-
`cells` : container that supports cells[i][j]
811+
`cells` : container that supports cells[i][j]
812812
elements are stored in column-major order with y increasing up
813813
`origin` : (int, int, int)
814814
cell block offset x,y,z ; default is (0,0,0)
@@ -847,7 +847,7 @@ def get_in_region(self, x1, y1, x2, y2):
847847
for j in range(int(y1), int(y2))]
848848
#print 'get_in_region result:', res
849849
return res
850-
850+
851851
def get_at_pixel(self, x, y):
852852
''' Return Cell at pixel px=(x,y) on the map.
853853
@@ -887,7 +887,7 @@ def get_neighbors(self, cell):
887887
# TODO: add checks to ensure good html. By example, what if cell is None?
888888
def _as_xml(self, root):
889889
"""stores a XML representation of itself as child of root with type rectmap
890-
890+
891891
"""
892892
m = ElementTree.SubElement(root, 'rectmap', id=self.id,
893893
tile_size='%dx%d'%(self.tw, self.th),
@@ -934,9 +934,21 @@ def collide_right(self, dx):
934934
def collide_top(self, dy):
935935
pass
936936

937-
# XXX this should take a *map* to collide with and find all collisions;
938-
# def collide_map(self, map, last, new, dy, dx):
939-
# do this cleverly :)
937+
# this should take a *map* to collide with and find all collisions;
938+
def collide_map(self, map, last, new, dy, dx):
939+
tested = set()
940+
for x, y in (new.bottomleft, new.bottomright, new.topleft,
941+
new.topright, new.midleft, new.midright, new.midbottom,
942+
new.midtop):
943+
cell = map.get((int(x), int(y)))
944+
if cell is None or cell.tile is None:
945+
continue
946+
# don't re-test
947+
key = (cell.i, cell.j)
948+
if key in tested:
949+
continue
950+
tested.add(cell)
951+
self.do_collision(cell, last, new, dy, dx)
940952

941953
# resolve them and re-collide if necessary; make sure the cells
942954
# colliding the sprite midpoints are done first
@@ -951,7 +963,7 @@ def do_collision(self, cell, last, new, dy, dx):
951963
If there is no collision then nothing is done.
952964
953965
If there is a collision:
954-
966+
955967
1. The "new" rect's position will be modified to its closest position
956968
to the side of the cell that the collision is on, and
957969
2. If the "dx" and "dy" values are passed in the methods
@@ -980,7 +992,7 @@ def do_collision(self, cell, last, new, dy, dx):
980992
dy = last.y - new.y
981993
new.top = cell.bottom
982994
if dy: self.collide_top(dy)
983-
995+
984996

985997
class Cell(object):
986998
'''Base class for cells from rect and hex maps.
@@ -1100,9 +1112,9 @@ class HexMap(RegularTesselationMap):
11001112
Hexmaps store their cells in an offset array, column-major with y
11011113
increasing up, such that a map::
11021114
1103-
/d\ /h\
1115+
/d\ /h\
11041116
/b\_/f\_/
1105-
\_/c\_/g\
1117+
\_/c\_/g\
11061118
/a\_/e\_/
11071119
\_/ \_/
11081120
@@ -1224,7 +1236,7 @@ class HexMapLayer(HexMap, MapLayer):
12241236
12251237
Hexmaps store their cells in an offset array, column-major with y
12261238
increasing up, such that a map::
1227-
1239+
12281240
/d\ /h\
12291241
/b\_/f\_/
12301242
\_/c\_/g\
@@ -1375,5 +1387,3 @@ def get_midbottomright(self):
13751387
return (x + self.width // 2 + self.width // 4 + self.width // 8,
13761388
y + self.height // 4)
13771389
midbottomright = property(get_midbottomright)
1378-
1379-

0 commit comments

Comments
 (0)