def load(self, tex_name, tiles_info_file): self.texture, self.width, self.height = GLTexture.load_image(tex_name) f = open(tiles_info_file, "r") for line in f.readlines(): if line == '': continue tile = line.split(' ') nums = [int(n) for n in tile[1:] if n != ''] nlength = len(nums) if nlength == 4 or nlength == 8: self.tiles[tile[0]] = TileTexture(Rect(*nums[:4])) elif nlength == 5 or nlength == 9: self.tiles[tile[0]] = AnimatedTileTexture( Rect(*nums[:4]), nums[4]) if nlength == 8 or nlength == 9: rect = Rect(*nums[4:8]) if nlength == 8 else Rect(*nums[5:9]) self.colliders[tile[0]] = rect
def test_edges(): rect = Rect(10, 10, 20, 20) edges = [ LineSegment((10, 10), (30, 10)), LineSegment((30, 10), (30, 30)), LineSegment((30, 30), (10, 30)), LineSegment((10, 30), (10, 10)), ] for i in range(4): assert edges[i] == rect.edges[i], "Asserting: "+str(edges[i] == rect.edges[i])#str(edges[i])+" "+str(rect.edges[i])
def calculate_rects(self): pos = self.entity.get_component(TransformComponent).pos if not self.tile_map: return tile_width, tile_height = (self.size[0] / len(self.tile_map[0]), self.size[0] / len(self.tile_map)) self.tile_size = (tile_width, tile_height) for y in range(len(self.tile_map)): for x in range(len(self.tile_map[y])): for i in range(len(self.tile_map[y][x])): self.tile_map[y][x][i].rect = Rect(pos.x + x * tile_width, pos.y + y * tile_height, tile_width, tile_height)
def test_intersect_line5(): line = LineSegment(Vector2(0, 5), Vector2(13, 5)) rect = Rect(5, 0, 11, 11) assert rect.intersect_line(line) == Vector2(5, 5)
def test_intersect_line4(): line = LineSegment(Vector2(0, 0), Vector2(-5, -5)) rect = Rect(-5, -5, 11, 11) assert rect.intersect_line(line) == Vector2(-5, -5)
def test_intersect_line3(): line = LineSegment(Vector2(0, 0), Vector2(3, 3)) rect = Rect(5, 5, 11, 11) assert rect.intersect_line(line) is None
def test_intersect_line(): line = LineSegment(Vector2(0, 0), Vector2(10, 10)) rect = Rect(5, 5, 11, 11) assert rect.intersect_line(line) == Vector2(5, 5)
def get_collider(self): trans = self.entity.get_component(TransformComponent) return [ Rect(trans.x + self.offset.x, trans.y + self.offset.y, self.size[0], self.size[1]) ]