A command-based communication system
Up to now, you have seen how events and real-time inputs are handled. In our game, we might handle them as follows (the aircraft methods are fictional to show the principle):
// One-time events sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::X) mPlayerAircraft->launchMissile(); } // Real-time input if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) mPlayerAircraft->moveLeft(); else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) mPlayerAircraft->moveRight();
There are several problems with this approach. On one side, we have hardcoded keys, so a user cannot choose WASD instead of arrow keys for movement. On the other side, we gather input and logic code in one place. The game logic code grows as we implement more features, and makes the originally simple code complicated.
For example, imagine that we want to keep the airplane inside...