Пример #1
0
    def test_get_remove_pickup(self):
        # Given
        playfield = Playfield()

        playfield.level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(0, 1, 0), Tile.Type.FLAT))

        pickup = CopperCoin()
        playfield.level.get_tile(0, 0).pickup = pickup

        # When
        playfieldstate = ai.PlayfieldState(playfield)

        # Then
        assert playfieldstate.get_pickup(playfield.level.get_tile(0,
                                                                  0)) is pickup
        assert playfieldstate.get_pickup(playfield.level.get_tile(0,
                                                                  1)) is None

        # And when
        playfieldstate.remove_pickup(playfield.level.get_tile(0, 0))

        # Then
        assert playfieldstate.get_pickup(playfield.level.get_tile(0,
                                                                  0)) is None
Пример #2
0
    def test_init(self):
        playfield = Playfield()

        playfield.level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(0, 1, 0), Tile.Type.FLAT))

        playfieldstate = ai.PlayfieldState(playfield)
Пример #3
0
    def test_add_and_sub(self):
        top = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        left = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        bottom = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        right = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)

        top.trail.type = Trail.Type.SW
        left.trail.type = Trail.Type.SE
        bottom.trail.type = Trail.Type.NE
        right.trail.type = Trail.Type.NW

        top.set_neighbor(left, Direction.WEST)
        top.set_neighbor(right, Direction.SOUTH)
        left.set_neighbor(top, Direction.EAST)
        left.set_neighbor(bottom, Direction.SOUTH)
        bottom.set_neighbor(left, Direction.NORTH)
        bottom.set_neighbor(right, Direction.EAST)
        right.set_neighbor(top, Direction.NORTH)
        right.set_neighbor(bottom, Direction.WEST)

        pos = TrailPosition(top, top.get_length() / 2)
        assert pos.tile == top
        next = pos + top.get_length()
        assert next.tile == left
        assert pos.tile == top
        pos += top.get_length()
        assert pos == next

        pos = TrailPosition(top, top.get_length() / 2)
        assert pos.tile == top
        next = pos - top.get_length()
        assert next.tile == right
        assert pos.tile == top
        pos -= top.get_length()
        assert pos == next
Пример #4
0
    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
Пример #5
0
    def test_save_load(self):
        tile1 = Tile(Vec3D(6, 5, 4), Tile.Type.NORTH_SLOPE_TOP)
        tile2 = Tile(Vec3D(3, 4, 1), Tile.Type.FLAT)
        tile2.trail.type = Trail.Type.SW

        # save tiles
        f = open("tilesave", "wb")

        tile1.save(f)
        tile2.save(f)

        f.close()

        # load tiles
        f = open("tilesave", "rb")

        copy1 = Tile.load(f)
        copy2 = Tile.load(f)

        assert copy1.type == Tile.Type.NORTH_SLOPE_TOP
        assert copy1.trail.type == Trail.Type.HILL
        assert copy1.pos == Vec3D(6, 5, 4)
        assert copy2.type == Tile.Type.FLAT
        assert copy2.trail.type == Trail.Type.SW
        assert copy2.pos == Vec3D(3, 4, 1)

        f.close()

        os.remove("tilesave")
Пример #6
0
    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)
Пример #7
0
    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)
Пример #8
0
    def test_align_switch(self):
        center = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        center.trail.type = Trail.Type.SE
        north = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        north.trail.type = Trail.Type.NS
        east = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        east.trail.type = Trail.Type.EW
        west = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        west.trail.type = Trail.Type.EW

        center.set_neighbor(north, Direction.NORTH)
        center.set_neighbor(east, Direction.EAST)
        center.set_neighbor(west, Direction.WEST)
        north.set_neighbor(center, Direction.SOUTH)
        east.set_neighbor(center, Direction.WEST)
        west.set_neighbor(center, Direction.EAST)

        center.trail.align()
        tt = center.trail.type
        assert tt == Trail.Type.NE or tt == Trail.Type.NW or tt == Trail.Type.EW
        center.trail.align()
        assert center.trail.type == tt

        center.trail.type = Trail.Type.EW
        center.trail.align(Direction.NORTH)
        tt = center.trail.type
        assert tt == Trail.Type.NE or tt == Trail.Type.NW
Пример #9
0
    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
Пример #10
0
 def test_constructor( self ):
     a = Vec3D()
     b = Vec3D( z = 1 )
     c = Vec3D( 3, 2, 1 )
     assert a.x == b.x
     assert a.z != b.z
     assert c.z == b.z
Пример #11
0
 def test_cross_product( self ):
     a = Vec3D( 4, 1, 5 )
     b = Vec3D( 1, -2, 3 )
     c = a.cross( b )
     assert a.cross( b ) == -b.cross( a )
     assert a.dot( c ) == 0
     assert b.dot( c ) == 0
Пример #12
0
    def test_neighbor_offset(self):
        flat = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        assert flat.get_neighbor_offset(Direction.NORTH) == Vec3D(0, -1, 0)

        slope = Tile(Vec3D(0, 0, 0), Tile.Type.EAST_SLOPE_BOT)
        assert slope.get_neighbor_offset(Direction.WEST) == Vec3D(0, -1, 0)
        assert slope.get_neighbor_offset(Direction.SOUTH) == Vec3D(0, 0, 0)
Пример #13
0
    def test_dot_product( self ):
        a = Vec3D( 1, 1, 0 )
        b = Vec3D( 1, -1, 0 )
        assert a.dot( b ) == 0

        a = Vec3D( 4, 1, 5 )
        b = Vec3D( 1, -2, 3 )
        assert a.dot( b ) == b.dot( a )
Пример #14
0
    def test_move(self):
        top = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        left = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        bottom = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        right = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)

        top.trail.type = Trail.Type.SW
        left.trail.type = Trail.Type.SE
        bottom.trail.type = Trail.Type.NE
        right.trail.type = Trail.Type.NW

        top.set_neighbor(left, Direction.WEST)
        top.set_neighbor(right, Direction.SOUTH)
        left.set_neighbor(top, Direction.EAST)
        left.set_neighbor(bottom, Direction.SOUTH)
        bottom.set_neighbor(left, Direction.NORTH)
        bottom.set_neighbor(right, Direction.EAST)
        right.set_neighbor(top, Direction.NORTH)
        right.set_neighbor(bottom, Direction.WEST)

        pos = TrailPosition(top, top.get_length() / 2)
        assert pos.tile == top
        pos -= top.get_length()
        assert pos.tile == right
        pos -= right.get_length()
        assert pos.tile == bottom
        pos -= bottom.get_length()
        assert pos.tile == left
        pos -= left.get_length()
        assert pos.tile == top

        pos += top.get_length()
        assert pos.tile == left
        pos += left.get_length()
        assert pos.tile == bottom
        pos += bottom.get_length()
        assert pos.tile == right
        pos += right.get_length()
        assert pos.tile == top

        pos -= 2 * top.get_length()
        assert pos.tile == bottom
        pos -= 5 * top.get_length()
        assert pos.tile == left

        # Test bounce agains wrong trail
        left.trail.type = Trail.Type.NS
        pos.set_position(top, top.get_length() / 2)
        if pos.is_reversed():
            pos.reverse_progress()

        pos += top.get_length()
        assert pos.tile == top

        right.trail.type = Trail.Type.EW
        pos.set_position(top, top.get_length() / 2)
        pos -= top.get_length()
        assert pos.tile == top
Пример #15
0
    def test_constructors(self):
        t = Tile(Vec3D(-1, 3, 0), Tile.Type.SOUTH_SLOPE_TOP, Trail.Type.HILL)
        assert t.pos == Vec3D(-1, 3, 0)
        assert t.type == Tile.Type.SOUTH_SLOPE_TOP

        t = Tile(Vec3D(-1, 3, 0), Tile.Type.SOUTH_SLOPE_TOP)

        t = Tile(Vec3D(0, 1, 0), Tile.Type.SOUTH_SLOPE_TOP)
        assert t.trail.type == Trail.Type.HILL
Пример #16
0
    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
Пример #17
0
    def test_trail_in_out_direction(self):
        t = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT, Trail.Type.SE)
        assert t.trail.get_in_direction() in [Direction.EAST, Direction.SOUTH]
        assert t.trail.get_out_direction() in [Direction.EAST, Direction.SOUTH]
        assert t.trail.get_in_direction() <> t.trail.get_out_direction()

        t = Tile(Vec3D(0, 0, 0), Tile.Type.NORTH_SLOPE_TOP)
        assert t.trail.get_in_direction() in [Direction.NORTH, Direction.SOUTH]
        assert t.trail.get_out_direction() in [
            Direction.NORTH, Direction.SOUTH
        ]
        assert t.trail.get_in_direction() <> t.trail.get_out_direction()
Пример #18
0
    def test_neighbors(self):
        top = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT, Trail.Type.SW)
        left = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT, Trail.Type.SE)
        right = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT, Trail.Type.NW)

        top.set_neighbor(right, Direction.SOUTH)
        top.set_neighbor(left, Direction.WEST)
        left.set_neighbor(top, Direction.EAST)
        right.set_neighbor(top, Direction.NORTH)

        assert top.get_in_tile() in [left, right]
        assert top.get_out_tile() in [left, right]
        assert top.get_in_tile() <> top.get_out_tile()
Пример #19
0
 def test_add_sub_mul( self ):
     a = Vec3D( 1, 1, 0 )
     b = Vec3D( 0, 2, 3 )
     assert a + b == Vec3D( 1, 3, 3 )
     assert a - b == Vec3D( 1, -1, -3 )
     assert b * 5 == Vec3D( 0, 10, 15 )
     assert -b == Vec3D( 0, -2, -3 )
     assert Vec3D( 2, 8, 34 ) / 2 == Vec3D( 1, 4, 17 )
Пример #20
0
    def test_is_switch(self):
        center = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        north = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        east = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        west = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)

        center.set_neighbor(north, Direction.NORTH)
        center.set_neighbor(east, Direction.EAST)
        center.set_neighbor(west, Direction.WEST)
        north.set_neighbor(center, Direction.SOUTH)
        east.set_neighbor(center, Direction.WEST)
        west.set_neighbor(center, Direction.EAST)

        assert not north.trail.is_switch()
        assert center.trail.is_switch()
Пример #21
0
    def test_get_out_nodes(self):
        center = Tile(Vec3D(), Tile.Type.FLAT, Trail.Type.NS)
        south = Tile(Vec3D(), Tile.Type.FLAT, Trail.Type.NS)
        west = Tile(Vec3D(), Tile.Type.FLAT, Trail.Type.EW)

        center.set_neighbor(south, Direction.SOUTH)
        center.set_neighbor(west, Direction.WEST)
        south.set_neighbor(center, Direction.NORTH)
        west.set_neighbor(center, Direction.EAST)

        node = TrailNode(center, None)

        outs = node.get_out_nodes()
        assert len(outs) == 2
        assert outs[0].tile in [south, west]
        assert outs[1].tile in [south, west]
        assert outs[0] <> outs[1]
Пример #22
0
    def test_clone(self):
        # Given
        playfield = Playfield()

        playfield.level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(0, 1, 0), Tile.Type.FLAT))

        pickup = CopperCoin()
        playfield.level.get_tile(0, 0).pickup = pickup
        playfieldstate = ai.PlayfieldState(playfield)

        # When
        clone = playfieldstate.clone()

        # Then
        assert clone.playfield is playfieldstate.playfield
        assert clone.pickups is not playfieldstate.pickups
Пример #23
0
    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
Пример #24
0
    def test_edge_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(1, 0, 0))
        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)
        portalC = Enterance(Vec3D(1, 2, 0))

        level.set_tile(portalA)
        level.set_tile(tileA)
        level.set_tile(tileB)
        level.set_tile(tileC)
        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, portalC, portalA, 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
Пример #25
0
    def testGetSetPortal(self):
        enterances = [Enterance(Vec3D(0, 0, 0)) for i in range(0, 3)]

        enterances[0].set_portals(enterances)

        test = [False for i in range(0, len(enterances))]
        for i in range(0, 20):  # Should have received all after 50 tries
            index = enterances.index(enterances[0].get_random_portal())
            test[index] = True

        assert test.count(False) == 0
Пример #26
0
    def test_goldcar_rides_to_next_tile(self):
        """Given a goldcar on one of 2 sequent tiles
           When the goldcar rides past the first one
           Then the goldcar is located on the second one"""
        # Given
        level = Level()
        tileA = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        tileB = Tile(Vec3D(0, 1, 0), Tile.Type.FLAT)
        level.set_tile(Tile(Vec3D(0, -1, 0), Tile.Type.FLAT))
        level.set_tile(tileA)
        level.set_tile(tileB)
        level.set_tile(Tile(Vec3D(0, 2, 0), Tile.Type.FLAT))

        goldcar = GoldCar(TrailPosition(tileA, 0), 0)

        count = 0
        while goldcar.pos.tile != tileB and count < 100:
            goldcar.game_tick()
            print goldcar.pos.tile, goldcar.pos.progress
            count += 1

        assert count < 100
Пример #27
0
    def test_collision(self):
        tile = Tile(Vec3D(), Tile.Type.FLAT, Trail.Type.NS)

        goldcar1 = GoldCar(TrailPosition(tile, 100), 0)
        goldcar2 = GoldCar(
            TrailPosition(tile, 100 + GoldCar.COLLIDE_DISTANCE + 20), 0)

        goldcar1.handle_collision(goldcar2)
        assert goldcar1.pos == TrailPosition(tile, 100)
        assert goldcar2.pos == TrailPosition(
            tile, 100 + GoldCar.COLLIDE_DISTANCE + 20)

        goldcar1.speed = 15
        goldcar2.pos.reverse_progress()
        goldcar2.speed = 15

        goldcar1.game_tick()
        goldcar2.game_tick()

        goldcar1.handle_collision(goldcar2)
        assert goldcar1.pos.get_distance(
            goldcar2.pos) < GoldCar.COLLIDE_DISTANCE + 20
        assert goldcar1.pos.get_distance(
            goldcar2.pos) >= GoldCar.COLLIDE_DISTANCE

        goldcar1 = GoldCar(TrailPosition(tile, 100), 0)
        goldcar2 = GoldCar(
            TrailPosition(tile, 100 + GoldCar.COLLIDE_DISTANCE + 10), 0)

        goldcar1.speed = 30
        goldcar2.speed = 10

        print goldcar1.pos.progress
        print goldcar2.pos.progress

        goldcar1.game_tick()
        goldcar2.game_tick()

        print goldcar1.pos.progress
        print goldcar2.pos.progress
        print GoldCar.COLLIDE_DISTANCE

        print goldcar1.speed
        print goldcar2.speed

        goldcar1.handle_collision(goldcar2)
        assert goldcar1.pos.get_distance(
            goldcar2.pos) < GoldCar.COLLIDE_DISTANCE + 20
        assert goldcar1.pos.get_distance(
            goldcar2.pos) >= GoldCar.COLLIDE_DISTANCE
Пример #28
0
    def test_switch_trail(self):
        top = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        left = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        bottom = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        right = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)

        top.set_neighbor(left, Direction.SOUTH)
        top.set_neighbor(right, Direction.EAST)
        left.set_neighbor(top, Direction.NORTH)
        left.set_neighbor(bottom, Direction.EAST)
        bottom.set_neighbor(left, Direction.WEST)
        bottom.set_neighbor(right, Direction.NORTH)
        right.set_neighbor(top, Direction.WEST)
        right.set_neighbor(bottom, Direction.SOUTH)

        top.trail.switch_it()
        assert top.trail.type == Trail.Type.SE
        left.trail.switch_it()
        assert left.trail.type == Trail.Type.NE
        bottom.trail.switch_it()
        assert bottom.trail.type == Trail.Type.NW
        right.trail.switch_it()
        assert right.trail.type == Trail.Type.SW
Пример #29
0
    def test_get_out_nodes_enterance(self):
        # Given
        enter_a = Enterance(Vec3D())
        enter_a.trail.type = Trail.Type.EW
        enter_b = Enterance(Vec3D())
        enter_b.trail.type = Trail.Type.EW
        enter_c = Enterance(Vec3D())
        enter_c.trail.type = Trail.Type.EW

        tile_b = Tile(Vec3D(), Tile.Type.FLAT, Trail.Type.EW)
        tile_c = Tile(Vec3D(), Tile.Type.FLAT, Trail.Type.EW)

        enter_b.set_neighbor(tile_b, Direction.WEST)
        enter_c.set_neighbor(tile_c, Direction.WEST)
        tile_b.set_neighbor(enter_b, Direction.EAST)
        tile_c.set_neighbor(enter_c, Direction.EAST)

        enter_a.set_portals([enter_b, enter_c])

        # When
        node = TrailNode(enter_a, Direction.WEST)

        # Then
        outs = node.get_out_nodes()
        assert len(outs) == 2
        assert outs[0].tile in [enter_b, enter_c]
        assert outs[1].tile in [enter_b, enter_c]
        assert outs[0] <> outs[1]

        # And when
        node = node.get_out_nodes()[0]
        outs = node.get_out_nodes()

        # Then
        assert len(outs) == 1
        assert (node.tile is enter_b and outs[0].tile is tile_b) or \
               (node.tile is enter_c and outs[0].tile is tile_c)
Пример #30
0
    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")