示例#1
0
def test_registry_system_add_remove():
    r = Registry()
    r.register_component('position')
    r.register_component('velocity')

    def update(update, positions, velocities):
        pass

    s = System(update, ['position', 'velocity'])

    r.register_system(s)

    class Position:
        def __init__(self, x):
            self.x = x

    class Velocity:
        def __init__(self, speed):
            self.speed = speed

    p1 = Position(10)
    p2 = Position(20)
    p3 = Position(40)

    r.add_component(1, 'position', p1)
    r.add_component(2, 'position', p2)
    r.add_component(3, 'position', p3)
    assert s.entity_ids == set()

    v1 = Velocity(1)
    v2 = Velocity(5)
    v4 = Velocity(10)

    r.add_component(1, 'velocity', v1)
    assert s.entity_ids == set([1])
    r.add_component(2, 'velocity', v2)
    assert s.entity_ids == set([1, 2])
    r.add_component(4, 'velocity', v4)
    assert s.entity_ids == set([1, 2])

    r.remove_component(1, 'velocity')
    assert s.entity_ids == set([2])
示例#2
0
def test_registry_system_item():
    r = Registry()
    r.register_component('position')
    r.register_component('velocity')

    seen_entity_ids = set()

    def update_position(update, r, entity_id, position, velocity):
        assert update == 'update'
        seen_entity_ids.add(entity_id)
        position.x += velocity.speed

    r.register_system(item_system(update_position, ['position', 'velocity']))

    class Position:
        def __init__(self, x):
            self.x = x

    class Velocity:
        def __init__(self, speed):
            self.speed = speed

    p1 = Position(10)
    p2 = Position(20)
    p3 = Position(40)

    r.add_component(1, 'position', p1)
    r.add_component(2, 'position', p2)
    r.add_component(3, 'position', p3)

    v1 = Velocity(1)
    v2 = Velocity(5)
    v4 = Velocity(10)

    r.add_component(1, 'velocity', v1)
    r.add_component(2, 'velocity', v2)
    r.add_component(4, 'velocity', v4)

    assert r.get(1, 'position').x == 10
    assert r.get(2, 'position').x == 20

    assert r.get(1, 'velocity').speed == 1
    assert r.get(2, 'velocity').speed == 5

    r.execute('update')

    assert r.get(1, 'position').x == 11
    assert r.get(2, 'position').x == 25
    assert seen_entity_ids == set([1, 2])
示例#3
0
def test_registry_system_dict_container():
    r = Registry()
    r.register_component('position')
    r.register_component('velocity')

    def update_position(update, r, entity_ids, positions, velocities):
        assert update == 'update'
        assert sorted(entity_ids) == [1, 2]
        assert list(positions.keys()) == [1, 2, 3]
        assert list(velocities.keys()) == [1, 2, 4]
        for entity_id in entity_ids:
            positions[entity_id]['x'] += velocities[entity_id]['speed']

    r.register_system(System(update_position, ['position', 'velocity']))

    p1 = {'x': 10}
    p2 = {'x': 20}
    p3 = {'x': 40}

    r.add_component(1, 'position', p1)
    r.add_component(2, 'position', p2)
    r.add_component(3, 'position', p3)

    v1 = {'speed': 1}
    v2 = {'speed': 5}
    v4 = {'speed': 10}

    r.add_component(1, 'velocity', v1)
    r.add_component(2, 'velocity', v2)
    r.add_component(4, 'velocity', v4)

    assert r.get(1, 'position')['x'] == 10
    assert r.get(2, 'position')['x'] == 20

    assert r.get(1, 'velocity')['speed'] == 1
    assert r.get(2, 'velocity')['speed'] == 5

    r.execute('update')

    assert r.get(1, 'position')['x'] == 11
    assert r.get(2, 'position')['x'] == 25
    assert r.get(3, 'position')['x'] == 40