Exemplo n.º 1
0
def test_making_choices_removes_listener():
    game = Game()  # noqa: F841

    # scene_0 (Decision) -> combat scene

    options_0 = {
        's': DecisionOption('load next scene', combat_scene.CombatScene)
    }
    scene_0 = DecisionScene('first scene', options_0)

    event_utils.post_scene_change(scene_0)
    ctl = _get_active_controller()
    assert isinstance(ctl, DecisionSceneController)
    # we must remove references otherwise EventManager will keep this listener
    del ctl
    del scene_0
    event_utils.simulate_key_press('s')

    # The scene machine only changes scenes on a tick
    EventManager.post(BasicEvents.TICK)

    assert not any(
        isinstance(l, DecisionScene) for l in EventManager.listeners)
    assert not any(
        isinstance(l, DecisionSceneController) for l in EventManager.listeners)

    ctl = _get_active_controller()
    assert isinstance(ctl, CombatSceneController)
    assert any(
        isinstance(l, combat_scene.CombatScene)
        for l in EventManager.listeners)
Exemplo n.º 2
0
 def handle_keypress(self, key_name: str) -> None:
     if self.get_binding(key_name) != BasicEvents.NONE:
         self.post_bound_event(key=key_name)
     else:
         input_event = InputEvent(event_type=BasicEvents.KEYPRESS,
                                  key=key_name)
         EventManager.post(input_event)
Exemplo n.º 3
0
def test_combat_scene_to_decision_scene():
    game = Game()  # noqa: F841
    view_manager = ViewManager()  # noqa: F841

    # dummy decision scene to which will will transision
    def dummy_scene():
        return DecisionScene('dummy scene for testing purposes', {})

    # combat scene with two 1 health enemies
    enemies = [_one_health_enemy() for _ in range(2)]
    scene = combat_scene.CombatScene(
        enemies, win_resolution=BasicResolution(dummy_scene))
    event_utils.post_scene_change(scene)

    assert isinstance(_get_active_controller(), CombatSceneController)

    # Start with player holding nothing
    player = get_player()
    [player.chassis.remove_mod(mod) for mod in player.chassis.all_mods()]
    assert len(list(player.chassis.all_mods())) == 1  # Base mod only

    # give player ability to fire laser

    shoot_laser = direct_damage(1,
                                label='laser',
                                time_to_resolve=0,
                                cpu_slots=0)
    laser_mod = build_mod(subroutines_granted=shoot_laser,
                          valid_slots=tuple(s for s in SlotTypes
                                            if s != SlotTypes.STORAGE))
    assert player.chassis.can_store(laser_mod)
    player.chassis.attempt_store(laser_mod)
    assert laser_mod in player.chassis.all_active_mods()

    # Kill each enemy
    for enemy in enemies:
        # click enemy
        click_on_char(enemy, scene.layout)

        assert enemy is selected_char(scene.layout)

        # select the fire laser ability
        laser_ind = [
            ind for ind, move in enumerate(scene.available_moves())
            if move.subroutine is shoot_laser
        ][0]
        event_utils.simulate_key_press(str(laser_ind + 1))
        assert is_dead(enemy)

    # check that scene has ended
    assert scene.is_resolved()

    # update the scene by waiting a tick, confirm that we've switched to a
    # Decision scene
    EventManager.post(BasicEvents.TICK)
    assert isinstance(_get_active_controller(), DecisionSceneController)
    def _notify(self, event: EventType) -> None:

        # Do not handle inputs while animation is in progress
        if self.scene.animation_progress is not None:
            return

        if isinstance(event, InputEvent):
            self._handle_input(event)
        elif isinstance(event, MoveExecutedEvent):
            if event.is_attacker_move:
                EventManager.post(SelectCharacterEvent(None))
    def _handle_input(self, input_event: InputEvent) -> None:
        if input_event.event_type == BasicEvents.MOUSE_CLICK:
            self._handle_mouse_click(input_event)
            return

        if input_event.key in COMBAT_KEYBOARD_INPUTS:
            # Player move selection
            input_key = int(input_event.key)
            moves = self.scene.available_moves()
            if len(moves) >= input_key > 0:
                selected_move = moves[input_key - 1]
                EventManager.post(SelectPlayerMoveEvent(selected_move))
Exemplo n.º 6
0
def test_press_debug_in_decision_scene_has_no_effect():
    game = Game()
    view_manager = ViewManager()

    event_utils.post_scene_change(
        DecisionScene('dummy scene for test purposes', {}))

    assert isinstance(game.scene_machine.controller, DecisionSceneController)
    assert view_manager.current_view is not None

    EventManager.post(BasicEvents.DEBUG)
    EventManager.post(BasicEvents.TICK)
Exemplo n.º 7
0
 def handle_inputs(self) -> None:
     # Called for each game tick. We check our keyboard presses here.
     for pg_event in self.get_pygame_events():
         # handle window manager closing our window
         if self.is_quit_event(pg_event):
             EventManager.post(BasicEvents.QUIT)
         # handle key down events
         elif pg_event.type == pygame.KEYDOWN:
             self.handle_keypress(pg_event.unicode)
         elif pg_event.type == pygame.KEYUP:
             pass
         elif pg_event.type == pygame.MOUSEBUTTONDOWN:
             self.handle_mouse_click()
    def _handle_mouse_click(self, input_event: InputEvent) -> None:
        x = input_event.mouse[0]
        y = input_event.mouse[1]

        # A single click on an unselected character will select it.
        # Any subsequent clicks will deselect it (and may also select a new
        # character).

        # Check if a character was newly clicked.
        clicked_obj = self.scene.layout.object_at(x, y)
        if isinstance(clicked_obj, CharacterInfo):
            if not clicked_obj.is_selected:
                EventManager.post(SelectCharacterEvent(clicked_obj.character))
                logging.debug('MOUSE: Selected: {}'.format(clicked_obj))
                return

        logging.debug('MOUSE: Clicked nothing.')
        # if no character was clicked clear field
        EventManager.post(SelectCharacterEvent(None))
    def _handle_mouse_click(self, event: InputEvent) -> None:
        x = event.mouse[0]
        y = event.mouse[1]

        # Handle clicks on mod slots
        clicked_obj = self._scene.layout.object_at(x, y)
        # Player clicks on a mod.
        if isinstance(clicked_obj, SlotRowInfo):
            EventManager.post(InventorySelectionEvent(clicked_obj.mod))
        # Player clicks on a slot category -> attempt mod transfer.
        elif isinstance(clicked_obj, SlotHeaderInfo):
            EventManager.post(InventoryTransferEvent(clicked_obj.slot))
            EventManager.post(InventorySelectionEvent(None))
        else:
            EventManager.post(InventorySelectionEvent(None))
Exemplo n.º 10
0
 def post_bound_event(self, key: str) -> None:
     binding = self.get_binding(key)
     EventManager.post(BasicEvents(binding))
Exemplo n.º 11
0
 def handle_mouse_click(self) -> None:
     mouse_event = self.mouse_event()
     EventManager.post(mouse_event)
Exemplo n.º 12
0
def simulate_mouse_click(x: int, y: int) -> None:
    event = InputEvent(event_type=BasicEvents.MOUSE_CLICK,
                       key='',
                       mouse=(x, y))
    EventManager.post(event)
Exemplo n.º 13
0
def post_scene_change(new_scene: Scene) -> None:
    EventManager.post(NewSceneEvent(new_scene))
Exemplo n.º 14
0
def simulate_key_press(key_name: str) -> None:
    event = InputEvent(event_type=BasicEvents.KEYPRESS, key=key_name)
    EventManager.post(event)
Exemplo n.º 15
0
 def deactivate(self) -> None:
     status = 'Deactivating a {}'.format(self.__class__.__name__)
     self._active = False
     EventManager.post(ControllerActivatedEvent(status))
Exemplo n.º 16
0
def test_inventory_scene_control_flow():
    game = Game()  # noqa: F841
    view_manager = ViewManager()  # noqa: F841

    # Start with player holding nothing
    chassis = get_player().chassis

    [chassis.remove_mod(mod) for mod in chassis.all_mods()]
    assert len(list(chassis.all_mods())) == 1  # Base mod only

    # Setup start scene that loads the loot scene
    mod_locs = [
        SlotTypes.HEAD, SlotTypes.HEAD, SlotTypes.CHEST, SlotTypes.LEGS
    ]
    ground_mods = _typical_mods(mod_locs)

    def start_scene() -> DecisionScene:
        loot_scene = partial(InventoryScene, start_scene, lambda: ground_mods)
        return DecisionScene('dummy scene for testing purposes',
                             {'1': DecisionOption('Loot', loot_scene)})

    # Load loot scene
    event_utils.post_scene_change(start_scene())
    event_utils.simulate_key_press('1')
    # The scene machine only changes scenes during a game tick
    EventManager.post(BasicEvents.TICK)

    assert isinstance(_get_active_controller(), InventoryController)
    inv_scene = _get_current_scene()
    assert isinstance(inv_scene, InventoryScene)

    # Player selectes head 1 and moves it to head slot
    head_mod_1 = ground_mods[0]
    event_utils.simulate_mouse_click(
        *_mod_slot_position(head_mod_1, inv_scene))

    assert head_mod_1 is inv_scene.selected_mod

    head_slot_position = _slot_header_position(SlotTypes.HEAD, inv_scene)
    event_utils.simulate_mouse_click(*(head_slot_position))

    assert inv_scene.selected_mod is None
    assert head_mod_1 in chassis.all_mods()

    # Player selects head_mod_2 and tries to move it to head slot, but it is
    # full so it remains on the ground.
    assert chassis.slot_capacities[SlotTypes.HEAD] == 1
    head_mod_2 = ground_mods[1]

    head_2_slot_pos = _mod_slot_position(head_mod_2, inv_scene)
    event_utils.simulate_mouse_click(*head_2_slot_pos)

    assert head_mod_2 is inv_scene.selected_mod

    head_slot_position = _slot_header_position(SlotTypes.HEAD, inv_scene)
    event_utils.simulate_mouse_click(*(head_slot_position))

    assert inv_scene.selected_mod is None
    assert head_mod_2 not in chassis.all_mods()
    assert head_2_slot_pos == _mod_slot_position(head_mod_2, inv_scene)

    # Player moves leg mod to storage
    leg_mod = ground_mods[3]
    assert leg_mod not in chassis.all_mods()

    event_utils.simulate_mouse_click(*_mod_slot_position(leg_mod, inv_scene))
    assert leg_mod is inv_scene.selected_mod

    storage_slot_pos = _slot_header_position(SlotTypes.STORAGE, inv_scene)
    event_utils.simulate_mouse_click(*storage_slot_pos)

    assert inv_scene.selected_mod is None
    assert leg_mod in chassis.all_mods()
    assert leg_mod in chassis.mods_in_slot(SlotTypes.STORAGE)

    # Player tries to move chest mod to arms slot, so nothing happens
    chest_mod = ground_mods[2]
    assert SlotTypes.CHEST in chest_mod.valid_slots()
    assert chest_mod not in chassis.all_mods()

    event_utils.simulate_mouse_click(*_mod_slot_position(chest_mod, inv_scene))

    assert inv_scene.selected_mod is chest_mod

    arms_slot_pos = _slot_header_position(SlotTypes.ARMS, inv_scene)
    event_utils.simulate_mouse_click(*arms_slot_pos)

    assert inv_scene.selected_mod is None
    assert chest_mod not in chassis.all_mods()