refactor(player): Implement state-based input.
This commit is contained in:
parent
c7130ac3dd
commit
22d60c7192
@ -86,27 +86,31 @@ void Bettola::process_events(void) {
|
|||||||
_is_running = false;
|
_is_running = false;
|
||||||
} else if(event.type == SDL_EVENT_KEY_DOWN) {
|
} else if(event.type == SDL_EVENT_KEY_DOWN) {
|
||||||
switch(event.key.key) {
|
switch(event.key.key) {
|
||||||
case SDLK_W: _player.move_up(); break;
|
case SDLK_W: _input.up = true; break;
|
||||||
case SDLK_S: _player.move_down(); break;
|
case SDLK_S: _input.down = true; break;
|
||||||
case SDLK_A: _player.move_left(); break;
|
case SDLK_A: _input.left = true; break;
|
||||||
case SDLK_D: _player.move_right(); break;
|
case SDLK_D: _input.right = true; break;
|
||||||
}
|
}
|
||||||
} else if(event.type == SDL_EVENT_KEY_UP) {
|
} else if(event.type == SDL_EVENT_KEY_UP) {
|
||||||
switch(event.key.key) {
|
switch(event.key.key) {
|
||||||
case SDLK_W:
|
case SDLK_W: _input.up = false; break;
|
||||||
case SDLK_S:
|
case SDLK_S: _input.down = false; break;
|
||||||
_player.stop_vertical();
|
case SDLK_A: _input.left = false; break;
|
||||||
break;
|
case SDLK_D: _input.right = false; break;
|
||||||
case SDLK_A:
|
|
||||||
case SDLK_D:
|
|
||||||
_player.stop_horizontal();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bettola::update(double dt) {
|
void Bettola::update(double dt) {
|
||||||
|
float dir_x = 0.0f;
|
||||||
|
float dir_y = 0.0f;
|
||||||
|
if(_input.up) dir_y -= 1.0f;
|
||||||
|
if(_input.down) dir_y += 1.0f;
|
||||||
|
if(_input.left) dir_x -= 1.0f;
|
||||||
|
if(_input.right) dir_x += 1.0f;
|
||||||
|
_player.set_velocity_direction(dir_x, dir_y);
|
||||||
|
|
||||||
_player.update(dt);
|
_player.update(dt);
|
||||||
|
|
||||||
static char window_title[256];
|
static char window_title[256];
|
||||||
@ -127,8 +131,18 @@ void Bettola::render(void) {
|
|||||||
_shader.use();
|
_shader.use();
|
||||||
|
|
||||||
glm::mat4 model = glm::mat4(1.0f);
|
glm::mat4 model = glm::mat4(1.0f);
|
||||||
|
#if 0
|
||||||
model = glm::translate(model, glm::vec3(_player.get_x(), _player.get_y(), 0.0f));
|
model = glm::translate(model, glm::vec3(_player.get_x(), _player.get_y(), 0.0f));
|
||||||
model = glm::scale(model, glm::vec3(_player.get_width(), _player.get_height(), 1.0f));
|
model = glm::scale(model, glm::vec3(_player.get_width(), _player.get_height(), 1.0f));
|
||||||
|
#endif
|
||||||
|
/* to achieve Scale -> Rotate -> Translate,
|
||||||
|
* we need to apply operations in reverse!
|
||||||
|
*/
|
||||||
|
glm::mat4 trans = glm::translate(glm::mat4(1.0f), glm::vec3(_player.get_x(),
|
||||||
|
_player.get_y(),0.0f));
|
||||||
|
glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(_player.get_width(),
|
||||||
|
_player.get_height(), 1.0f));
|
||||||
|
model = trans * scale;
|
||||||
|
|
||||||
unsigned int model_loc = glGetUniformLocation(_shader.get_id(), "model");
|
unsigned int model_loc = glGetUniformLocation(_shader.get_id(), "model");
|
||||||
glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm::value_ptr(model));
|
glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm::value_ptr(model));
|
||||||
|
|||||||
@ -24,6 +24,13 @@ private:
|
|||||||
bool create_gl_context(void);
|
bool create_gl_context(void);
|
||||||
void setup_quad(void);
|
void setup_quad(void);
|
||||||
|
|
||||||
|
struct InputState {
|
||||||
|
bool up = false;
|
||||||
|
bool down = false;
|
||||||
|
bool left = false;
|
||||||
|
bool right = false;
|
||||||
|
};
|
||||||
|
|
||||||
bool _is_running;
|
bool _is_running;
|
||||||
|
|
||||||
SDL_Window* _window;
|
SDL_Window* _window;
|
||||||
@ -35,4 +42,5 @@ private:
|
|||||||
|
|
||||||
glm::mat4 _projection;
|
glm::mat4 _projection;
|
||||||
Player _player;
|
Player _player;
|
||||||
|
InputState _input;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
#include <cstdio>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/vec2.hpp>
|
||||||
|
|
||||||
Player::Player(void) :
|
Player::Player(void) :
|
||||||
_x(400.0f),
|
_x(400.0f),
|
||||||
@ -14,15 +17,13 @@ void Player::update(double dt) {
|
|||||||
_y += _vy * dt;
|
_y += _vy * dt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::move_up(void) { _vy = -_speed; }
|
void Player::set_velocity_direction(float dir_x, float dir_y) {
|
||||||
void Player::move_down(void) { _vy = _speed; }
|
glm::vec2 direction = { dir_x, dir_y };
|
||||||
void Player::move_left(void) { _vx = -_speed; }
|
|
||||||
void Player::move_right(void) { _vx = _speed; }
|
|
||||||
|
|
||||||
void Player::stop_vertical(void) {
|
if(glm::length(direction) > 0.0f) {
|
||||||
_vy = 0.0f;
|
direction = glm::normalize(direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::stop_horizontal(void) {
|
_vx = direction.x * _speed;
|
||||||
_vx = 0.0f;
|
_vy = direction.y * _speed;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,10 +13,12 @@ public:
|
|||||||
void stop_vertical(void);
|
void stop_vertical(void);
|
||||||
void stop_horizontal(void);
|
void stop_horizontal(void);
|
||||||
|
|
||||||
float get_x() const { return _x; }
|
void set_velocity_direction(float dir_x, float dir_y);
|
||||||
float get_y() const { return _y; }
|
|
||||||
float get_width() const { return _width; }
|
float get_x() const { return _x; }
|
||||||
float get_height() const { return _height; }
|
float get_y() const { return _y; }
|
||||||
|
float get_width() const { return _width; }
|
||||||
|
float get_height() const { return _height; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float _x;
|
float _x;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user