def test_get_free_position(self): """Given a playfield with some tiles and goldcar When get_free_position is called Then that position doesn't collide with goldcar """ # Given playfield = Playfield() playfield.level = Level() playfield.level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)) playfield.level.set_tile(Tile(Vec3D(1, 0, 0), Tile.Type.FLAT)) playfield.level.set_tile(Tile(Vec3D(2, 0, 0), Tile.Type.FLAT)) playfield.level.set_tile(Tile(Vec3D(3, 0, 0), Tile.Type.FLAT)) playfield.level.set_tile(Tile(Vec3D(4, 0, 0), Tile.Type.FLAT)) playfield.goldcars = [ GoldCar(TrailPosition(playfield.level.get_tile(1, 0), 500), 1) ] for i in range(0, 256): # When pos = playfield.get_free_position() # Then if pos is not None: assert playfield.is_free_position(pos)
def test_neighbors(self): level = Level() level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)) level.set_tile(Tile(Vec3D(0, 1, 0), Tile.Type.FLAT)) level.set_tile(Tile(Vec3D(1, 0, 0), Tile.Type.WEST_SLOPE_TOP)) level.set_tile(Tile(Vec3D(1, 1, 0), Tile.Type.WEST_SLOPE_BOT)) neighbor = level.get_tile(0, 0).get_neighbor(Direction.SOUTH) assert neighbor is not None assert neighbor.type == Tile.Type.FLAT assert neighbor == level.get_tile(0, 1) neighbor = level.get_tile(0, 1).get_neighbor(Direction.NORTH) assert neighbor == level.get_tile(0, 0) neighbor = level.get_tile(0, 0).get_neighbor(Direction.EAST) assert neighbor is None neighbor = level.get_tile(0, 1).get_neighbor(Direction.EAST) assert neighbor == level.get_tile(1, 1) level.remove_tile(0, 1) neighbor = level.get_tile(0, 0).get_neighbor(Direction.SOUTH) assert neighbor is None
def test_isnt_free_position(self): """Given a level with some flat tiles When a goldcar occupies a tile And we have a position on another tile far from goldcar Then is_free_position returns true """ # Given playfield = Playfield() playfield.level = Level() playfield.level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)) playfield.level.set_tile(Tile(Vec3D(1, 0, 0), Tile.Type.FLAT)) playfield.level.set_tile(Tile(Vec3D(2, 0, 0), Tile.Type.FLAT)) playfield.level.set_tile(Tile(Vec3D(3, 0, 0), Tile.Type.FLAT)) playfield.level.set_tile(Tile(Vec3D(4, 0, 0), Tile.Type.FLAT)) # When playfield.goldcars = [ GoldCar(TrailPosition(playfield.level.get_tile(2, 0), 900), 1) ] pos = TrailPosition(playfield.level.get_tile(2, 0), 100) # Then assert playfield.is_free_position(pos)
def test_get_flat_tile(self): level = Level() level.set_tile(Tile(Vec3D(1, 0, 0), Tile.Type.WEST_SLOPE_TOP)) level.set_tile(Tile(Vec3D(5, 5, 0), Tile.Type.FLAT)) level.set_tile(Tile(Vec3D(1, 1, 0), Tile.Type.WEST_SLOPE_BOT)) level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)) assert level.get_first_flat_tile() == level.get_tile(0,0) \ or level.get_first_flat_tile() == level.get_tile(5,5) first_tile = False second_tile = False for i in range(0, 20): # chance is realy small that all 20 will be same t = level.get_random_flat_tile() if t == level.get_tile(5, 5): first_tile = True elif t == level.get_tile(0, 0): second_tile = True else: assert False assert first_tile and second_tile
def test_slope_neighbors(self): level = Level() level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.SOUTH_SLOPE_TOP)) level.set_tile(Tile(Vec3D(-1, 2, 0), Tile.Type.SOUTH_SLOPE_BOT)) neighbor = level.get_tile(0, 0).get_neighbor(Direction.SOUTH) assert neighbor is not None and neighbor.type == Tile.Type.SOUTH_SLOPE_BOT neighbor = level.get_tile(-1, 2).get_neighbor(Direction.NORTH) assert neighbor is not None and neighbor.type == Tile.Type.SOUTH_SLOPE_TOP
def test_normal_portal_handling(self): """Given a goldcar on a small track with 2 portals When goldcar rides through one portal Then it comes out of the other""" # Given level = Level() portalA = Enterance(Vec3D(2, 0, 0)) tileAA = Tile(Vec3D(1, 0, 0), Tile.Type.FLAT) tileA = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT) tileB = Tile(Vec3D(0, 1, 0), Tile.Type.FLAT) tileC = Tile(Vec3D(0, 2, 0), Tile.Type.FLAT) tileCC = Tile(Vec3D(1, 2, 0), Tile.Type.FLAT) portalC = Enterance(Vec3D(2, 2, 0)) level.set_tile(portalA) level.set_tile(tileAA) level.set_tile(tileA) level.set_tile(tileB) level.set_tile(tileC) level.set_tile(tileCC) level.set_tile(portalC) goldcar = GoldCar(TrailPosition(tileB, 0), 0) #goldcar.pos.reverse_progress() playfield = Playfield() playfield.level = level playfield.goldcars = [goldcar] order = [tileB, tileC, tileCC, portalC, portalA, tileAA, tileA, tileB] order_at = 0 count = 0 while order_at < len(order) - 1 and count < 1000: playfield.game_tick() print goldcar.pos.tile, goldcar.pos.progress if goldcar.pos.tile == order[order_at]: pass elif goldcar.pos.tile == order[order_at + 1]: order_at += 1 else: assert False, "unknown tile after " + str(order_at) count += 1 assert count < 1000
def test_get_add_tile(self): level = Level() assert level.get_tile(0, 0) is None assert level.get_tile(-1, 9) is None tile1 = Tile(Vec3D(), Tile.Type.FLAT) tile2 = Tile(Vec3D(), Tile.Type.EAST_SLOPE_TOP) level.set_tile(tile1) assert level.get_tile(0, 0) is not None assert level.get_tile(0, 0).type == Tile.Type.FLAT level.set_tile(tile2) assert level.get_tile(0, 0) is not None assert level.get_tile(0, 0).type == Tile.Type.EAST_SLOPE_TOP level.remove_tile(0, 0) assert level.get_tile(0, 0) is None
def test_save_load_level(self): level = Level() level_loaded = Level() level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)) level.set_tile(Tile(Vec3D(0, 1, 0), Tile.Type.SOUTH_SLOPE_TOP)) assert level.get_tile(0, 1).trail.type == Trail.Type.HILL level.set_tile(Tile(Vec3D(1, 3, 0), Tile.Type.SOUTH_SLOPE_BOT)) assert level.get_tile(0, 1).trail.type == Trail.Type.HILL level.set_tile(Tile(Vec3D(1, 0, 0), Tile.Type.WEST_SLOPE_TOP)) assert level.get_tile(0, 0).get_neighbor(Direction.SOUTH) is not None assert level.get_tile(0, 0).get_neighbor( Direction.SOUTH).type == Tile.Type.SOUTH_SLOPE_TOP level.save("saveloadtest.lvl") level_loaded.set_tile(Tile(Vec3D(2, 2, 0), Tile.Type.NORTH_SLOPE_TOP)) level_loaded.load("saveloadtest.lvl") assert level_loaded.get_tile(0, 0) is not None assert level_loaded.get_tile(0, 0).type == Tile.Type.FLAT assert level_loaded.get_tile(0, 1) is not None assert level_loaded.get_tile(0, 1).type == Tile.Type.SOUTH_SLOPE_TOP assert level_loaded.get_tile(1, 3) is not None assert level_loaded.get_tile(1, 3).type == Tile.Type.SOUTH_SLOPE_BOT assert level_loaded.get_tile(1, 0) is not None assert level_loaded.get_tile(1, 0).type == Tile.Type.WEST_SLOPE_TOP assert level_loaded.get_tile(1, 1) is None # check neighbors assert level_loaded.get_tile(0, 0).get_neighbor( Direction.SOUTH) is not None assert level_loaded.get_tile(0, 0).get_neighbor( Direction.SOUTH).type == Tile.Type.SOUTH_SLOPE_TOP os.remove("saveloadtest.lvl")