Browse Source

new input handling stuff

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
8b028cfdd2
4 changed files with 54 additions and 17 deletions
  1. 4
    4
      core.mod/src/entities/EntPlayer.cc
  2. 17
    2
      libswan/include/swan/Game.h
  3. 3
    7
      libswan/src/Game.cc
  4. 30
    4
      src/main.cc

+ 4
- 4
core.mod/src/entities/EntPlayer.cc View File

@@ -26,17 +26,17 @@ void EntPlayer::update(const Swan::Context &ctx, float dt) {
jump_timer_.tick(dt);

// Break block
if (ctx.game.isMousePressed())
if (ctx.game.isMousePressed(sf::Mouse::Button::Left))
ctx.plane.setTile(mouse_tile_, "core::air");

// Move left
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
if (ctx.game.isKeyPressed(sf::Keyboard::A) || ctx.game.isKeyPressed(sf::Keyboard::Left)) {
body_.force_ += Swan::Vec2(-FORCE, 0);
state_ = State::RUNNING_L;
}

// Move right
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
if (ctx.game.isKeyPressed(sf::Keyboard::D) || ctx.game.isKeyPressed(sf::Keyboard::Right)) {
body_.force_ += Swan::Vec2(FORCE, 0);
if (state_ == State::RUNNING_L)
state_ = State::IDLE;
@@ -45,7 +45,7 @@ void EntPlayer::update(const Swan::Context &ctx, float dt) {
}

// Jump
if (body_.on_ground_ && sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && jump_timer_.periodic(0.5)) {
if (body_.on_ground_ && ctx.game.isKeyPressed(sf::Keyboard::Space) && jump_timer_.periodic(0.5)) {
body_.vel_.y_ = -JUMP_FORCE;
}


+ 17
- 2
libswan/include/swan/Game.h View File

@@ -12,13 +12,25 @@ namespace Swan {

class Game {
public:
Game(Win &win): win_(win) {}
Game(Win &win):
mouse_pos_(0, 0),
keys_pressed_(sf::Keyboard::Key::KeyCount, false),
mouse_pressed_(sf::Mouse::Button::ButtonCount, false),
win_(win) {}

void loadMod(const std::string &path);
void createWorld(std::string worldgen);

void onKeyPressed(sf::Keyboard::Key key) { keys_pressed_[(int)key] = true; }
void onKeyReleased(sf::Keyboard::Key key) { keys_pressed_[(int)key] = false; }
void onMouseMove(int x, int y) { mouse_pos_ = Vec2i(x, y); }
void onMousePressed(sf::Mouse::Button button) { mouse_pressed_[(int)button] = true; }
void onMouseReleased(sf::Mouse::Button button) { mouse_pressed_[(int)button] = false; }

TilePos getMouseTile();
bool isMousePressed();
Vec2i getMousePos() { return mouse_pos_; }
bool isKeyPressed(sf::Keyboard::Key key) { return keys_pressed_[(int)key]; }
bool isMousePressed(sf::Mouse::Button button) { return mouse_pressed_[(int)button]; }

void draw();
void update(float dt);
@@ -29,6 +41,9 @@ public:
std::unique_ptr<World> world_ = NULL;

private:
Vec2i mouse_pos_;
std::vector<bool> keys_pressed_;
std::vector<bool> mouse_pressed_;
std::vector<Mod> registered_mods_;
Win &win_;
};

+ 3
- 7
libswan/src/Game.cc View File

@@ -51,14 +51,10 @@ void Game::createWorld(std::string worldgen) {
}

TilePos Game::getMouseTile() {
auto mousePos = sf::Mouse::getPosition(*win_.window_);
auto mousePos = getMousePos();
return TilePos(
(int)floor(win_.cam_.x_ + mousePos.x / (Swan::TILE_SIZE * win_.scale_)),
(int)floor(win_.cam_.y_ + mousePos.y / (Swan::TILE_SIZE * win_.scale_)));
}

bool Game::isMousePressed() {
return win_.window_->hasFocus() && sf::Mouse::isButtonPressed(sf::Mouse::Button::Left);
(int)floor(win_.cam_.x_ + mousePos.x_ / (Swan::TILE_SIZE * win_.scale_)),
(int)floor(win_.cam_.y_ + mousePos.y_ / (Swan::TILE_SIZE * win_.scale_)));
}

void Game::draw() {

+ 30
- 4
src/main.cc View File

@@ -53,11 +53,37 @@ int main() {
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
switch (event.type) {
case sf::Event::Closed:
window.close();
} else if (event.type == sf::Event::Resized) {
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
break;

case sf::Event::Resized:
window.setView(sf::View(sf::FloatRect(
0, 0, event.size.width, event.size.height)));
break;

case sf::Event::KeyPressed:
game.onKeyPressed(event.key.code);
break;

case sf::Event::KeyReleased:
game.onKeyReleased(event.key.code);
break;

case sf::Event::MouseMoved:
game.onMouseMove(event.mouseMove.x, event.mouseMove.y);
break;

case sf::Event::MouseButtonPressed:
game.onMousePressed(event.mouseButton.button);
break;

case sf::Event::MouseButtonReleased:
game.onMouseReleased(event.mouseButton.button);
break;

default: break;
}
}


Loading…
Cancel
Save