Refactoring: Keyboard-Input in GameInputUser verschieben #4

Closed
opened 2026-04-16 19:33:26 +00:00 by sebastian · 0 comments
Owner

GameLayer::onUpdate() hat zwei Verantwortlichkeiten die nichts miteinander zu tun haben: Input und Spielsimulation.
3a: Keyboard-Input in GameInputUser verschieben
Aktuell in GameLayer::onUpdate() (direkt + inkonsistent):

if (InputManager::isKeyPressed(GLFW_KEY_1))
    gameMode->setActiveBuilding(BuildingType::FOREST_HUT);
// ...
if (keyboard->keyPressEvent(GLFW_KEY_U)) { ... }

Stattdessen: GameInputUser bekommt eine Methode processKeyboard():

// GameInputUser.h
struct KeyboardIntent {
    glm::vec3 moveDir = {0,0,0};
    bool nextTurn     = false;
    bool toggleUpgrade = false;
    bool cancelAction  = false;
    std::optional<BuildingType> selectBuilding;
};

KeyboardIntent processKeyboard();

GameLayer::onUpdate() ruft das nur noch ab:

void GameLayer::onUpdate() {
    mousePicker->update(*camera);

    if (gameInputUser->isKeyboardEnabled()) {
        auto intent = gameInputUser->processKeyboard();
        camera->move(intent.moveDir, 0.5f);

        if (intent.selectBuilding)
            gameMode->setActiveBuilding(*intent.selectBuilding);
        if (intent.cancelAction) {
            gameMode->resetActiveBuilding();
            BuildPreviewSystem::disableBuildPreview(*entityManager);
        }
        if (intent.nextTurn)
            turnSystem->nextTurn(*turnState, events);
        if (intent.toggleUpgrade) { /* ... */ }
    }

    worldSystems->update(/* ... */);
}
GameLayer::onUpdate() hat zwei Verantwortlichkeiten die nichts miteinander zu tun haben: Input und Spielsimulation. 3a: Keyboard-Input in GameInputUser verschieben Aktuell in GameLayer::onUpdate() (direkt + inkonsistent): ``` if (InputManager::isKeyPressed(GLFW_KEY_1)) gameMode->setActiveBuilding(BuildingType::FOREST_HUT); // ... if (keyboard->keyPressEvent(GLFW_KEY_U)) { ... } ``` Stattdessen: GameInputUser bekommt eine Methode processKeyboard(): ``` // GameInputUser.h struct KeyboardIntent { glm::vec3 moveDir = {0,0,0}; bool nextTurn = false; bool toggleUpgrade = false; bool cancelAction = false; std::optional<BuildingType> selectBuilding; }; KeyboardIntent processKeyboard(); ``` GameLayer::onUpdate() ruft das nur noch ab: ``` void GameLayer::onUpdate() { mousePicker->update(*camera); if (gameInputUser->isKeyboardEnabled()) { auto intent = gameInputUser->processKeyboard(); camera->move(intent.moveDir, 0.5f); if (intent.selectBuilding) gameMode->setActiveBuilding(*intent.selectBuilding); if (intent.cancelAction) { gameMode->resetActiveBuilding(); BuildPreviewSystem::disableBuildPreview(*entityManager); } if (intent.nextTurn) turnSystem->nextTurn(*turnState, events); if (intent.toggleUpgrade) { /* ... */ } } worldSystems->update(/* ... */); } ```
sebastian added the
refactoring
label 2026-04-16 19:33:26 +00:00
sebastian added the
engine
label 2026-04-16 19:38:36 +00:00
sebastian added this to the Dicewars project 2026-04-16 20:50:41 +00:00
sebastian self-assigned this 2026-04-16 21:04:37 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sebastian/Dicewars-Siedler#4
No description provided.