-
Notifications
You must be signed in to change notification settings - Fork 0
Handling Input
In order for the player to interact with the game, we need to handle the keyboard, mouse, and gamepad inputs.
This can be achieved by using the InputSystem
class, which provides a set of methods to retrieve information about the different input events.
To use the InputSystem
class, you must first retrieve its instance by calling the InputSystem::Get
method.
To check the current position of the mouse, you can use the GetMousePosition
method, which returns the position in screen coordinates.
Alternatively, you can use the GetMouseDelta
method to check how much the mouse has moved since the last frame.
Furthermore, you can manually alter the mouse position by using the SetMousePosition
method.
If you need to check the state of a mouse button, there are three methods that can help you:
IsMouseButtonDown
allows you to check if a button is currently down, while WasMouseButtonPressed
and WasMouseButtonReleased
tell you if a button was pressed or released in the current frame.
You can also check how the mouse scroll varied since the last frame by using the GetMouseScroll
method.
Finally, the input system allows you to change the mouse behavior by using the SetMouseMode
method, which can be set to Normal
, Hidden
, Locked
, or Confined
.
void MyEntity::Update(float deltaTime)
{
// Retrieve the input system instance
InputSystem& inputSystem = InputSystem::Get();
// Get the mouse position
Vector2 mousePosition = inputSystem.GetMousePosition();
// Get the mouse delta
Vector2 mouseDelta = inputSystem.GetMouseDelta();
// Check if the left mouse button is down
if (inputSystem.IsMouseButtonDown(MouseButton::Left))
{
// Do something
}
// Check if the right mouse button was pressed
if (inputSystem.WasMouseButtonPressed(MouseButton::Right))
{
// Do something
}
// Check if the middle mouse button was released
if (inputSystem.WasMouseButtonReleased(MouseButton::Middle))
{
// Do something
}
// Get the mouse scroll
float mouseScroll = inputSystem.GetMouseScroll();
// Set the mouse mode to hidden
inputSystem.SetMouseMode(MouseMode::Hidden);
}
Detecting keyboard input is very similar to detecting mouse input.
Similarly to the mouse, you can check if a key is currently down by using the IsKeyDown
method, or if a key was pressed or released in the current frame by using the WasKeyPressed
and WasKeyReleased
methods.
Additionally, you can check if a key was repeated in the current frame using the WasKeyRepeated
method.
void MyEntity::Update(float deltaTime)
{
// Retrieve the input system instance
InputSystem& inputSystem = InputSystem::Get();
// Check if the 'A' key is down
if (inputSystem.IsKeyDown(KeyCode::A))
{
// Do something
}
// Check if the '1' key was pressed
if (inputSystem.WasKeyPressed(KeyCode::Num1))
{
// Do something
}
// Check if the F12 key was released
if (inputSystem.WasKeyReleased(KeyCode::F12))
{
// Do something
}
// Check if the space key was repeated
if (inputSystem.WasKeyRepeated(KeyCode::Space))
{
// Do something
}
}
At the moment, gamepad input is not supported by the engine.