@@ -808,7 +808,7 @@ def __init__(self, id, tw, th, cells, origin=None, properties=None):
808
808
number of colums in cells
809
809
`th` : int
810
810
number of rows in cells
811
- `cells` : container that supports cells[i][j]
811
+ `cells` : container that supports cells[i][j]
812
812
elements are stored in column-major order with y increasing up
813
813
`origin` : (int, int, int)
814
814
cell block offset x,y,z ; default is (0,0,0)
@@ -847,7 +847,7 @@ def get_in_region(self, x1, y1, x2, y2):
847
847
for j in range (int (y1 ), int (y2 ))]
848
848
#print 'get_in_region result:', res
849
849
return res
850
-
850
+
851
851
def get_at_pixel (self , x , y ):
852
852
''' Return Cell at pixel px=(x,y) on the map.
853
853
@@ -887,7 +887,7 @@ def get_neighbors(self, cell):
887
887
# TODO: add checks to ensure good html. By example, what if cell is None?
888
888
def _as_xml (self , root ):
889
889
"""stores a XML representation of itself as child of root with type rectmap
890
-
890
+
891
891
"""
892
892
m = ElementTree .SubElement (root , 'rectmap' , id = self .id ,
893
893
tile_size = '%dx%d' % (self .tw , self .th ),
@@ -934,9 +934,21 @@ def collide_right(self, dx):
934
934
def collide_top (self , dy ):
935
935
pass
936
936
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 )
940
952
941
953
# resolve them and re-collide if necessary; make sure the cells
942
954
# colliding the sprite midpoints are done first
@@ -951,7 +963,7 @@ def do_collision(self, cell, last, new, dy, dx):
951
963
If there is no collision then nothing is done.
952
964
953
965
If there is a collision:
954
-
966
+
955
967
1. The "new" rect's position will be modified to its closest position
956
968
to the side of the cell that the collision is on, and
957
969
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):
980
992
dy = last .y - new .y
981
993
new .top = cell .bottom
982
994
if dy : self .collide_top (dy )
983
-
995
+
984
996
985
997
class Cell (object ):
986
998
'''Base class for cells from rect and hex maps.
@@ -1100,9 +1112,9 @@ class HexMap(RegularTesselationMap):
1100
1112
Hexmaps store their cells in an offset array, column-major with y
1101
1113
increasing up, such that a map::
1102
1114
1103
- /d\ /h\
1115
+ /d\ /h\
1104
1116
/b\_/f\_/
1105
- \_/c\_/g\
1117
+ \_/c\_/g\
1106
1118
/a\_/e\_/
1107
1119
\_/ \_/
1108
1120
@@ -1224,7 +1236,7 @@ class HexMapLayer(HexMap, MapLayer):
1224
1236
1225
1237
Hexmaps store their cells in an offset array, column-major with y
1226
1238
increasing up, such that a map::
1227
-
1239
+
1228
1240
/d\ /h\
1229
1241
/b\_/f\_/
1230
1242
\_/c\_/g\
@@ -1375,5 +1387,3 @@ def get_midbottomright(self):
1375
1387
return (x + self .width // 2 + self .width // 4 + self .width // 8 ,
1376
1388
y + self .height // 4 )
1377
1389
midbottomright = property (get_midbottomright )
1378
-
1379
-
0 commit comments