feat(player) Implement key event handling.

Adds keyboard input handling to control the "player".
This commit is contained in:
Ritchie Cunningham 2025-09-12 23:00:42 +01:00
parent dc4dd650a4
commit c7130ac3dd
3 changed files with 54 additions and 6 deletions

View File

@ -84,11 +84,31 @@ void Bettola::process_events(void) {
while(SDL_PollEvent(&event)) { while(SDL_PollEvent(&event)) {
if(event.type == SDL_EVENT_QUIT) { if(event.type == SDL_EVENT_QUIT) {
_is_running = false; _is_running = false;
} else if(event.type == SDL_EVENT_KEY_DOWN) {
switch(event.key.key) {
case SDLK_W: _player.move_up(); break;
case SDLK_S: _player.move_down(); break;
case SDLK_A: _player.move_left(); break;
case SDLK_D: _player.move_right(); break;
}
} else if(event.type == SDL_EVENT_KEY_UP) {
switch(event.key.key) {
case SDLK_W:
case SDLK_S:
_player.stop_vertical();
break;
case SDLK_A:
case SDLK_D:
_player.stop_horizontal();
break;
}
} }
} }
} }
void Bettola::update(double dt) { void Bettola::update(double dt) {
_player.update(dt);
static char window_title[256]; static char window_title[256];
static double time_since_title_update = 0.0; static double time_since_title_update = 0.0;

View File

@ -4,8 +4,25 @@ Player::Player(void) :
_x(400.0f), _x(400.0f),
_y(300.0f), _y(300.0f),
_width(50.0f), _width(50.0f),
_height(50.0f) {} _height(50.0f),
_vx(0.0f),
_vy(0.0f),
_speed(200.0f) {}
void Player::update(double dt) { void Player::update(double dt) {
/* Do some shit? */ _x += _vx * dt;
_y += _vy * dt;
}
void Player::move_up(void) { _vy = -_speed; }
void Player::move_down(void) { _vy = _speed; }
void Player::move_left(void) { _vx = -_speed; }
void Player::move_right(void) { _vx = _speed; }
void Player::stop_vertical(void) {
_vy = 0.0f;
}
void Player::stop_horizontal(void) {
_vx = 0.0f;
} }

View File

@ -6,6 +6,13 @@ public:
void update(double dt); void update(double dt);
void move_up(void);
void move_down(void);
void move_left(void);
void move_right(void);
void stop_vertical(void);
void stop_horizontal(void);
float get_x() const { return _x; } float get_x() const { return _x; }
float get_y() const { return _y; } float get_y() const { return _y; }
float get_width() const { return _width; } float get_width() const { return _width; }
@ -16,4 +23,8 @@ private:
float _y; float _y;
float _width; float _width;
float _height; float _height;
float _vx;
float _vy;
float _speed;
}; };