Selaa lähdekoodia

new input handling stuff

opengl-renderer-broken
Martin Dørum 4 vuotta sitten
vanhempi
commit
8b028cfdd2
4 muutettua tiedostoa jossa 54 lisäystä ja 17 poistoa
  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 Näytä tiedosto

jump_timer_.tick(dt); jump_timer_.tick(dt);


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


// Move left // 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); body_.force_ += Swan::Vec2(-FORCE, 0);
state_ = State::RUNNING_L; state_ = State::RUNNING_L;
} }


// Move right // 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); body_.force_ += Swan::Vec2(FORCE, 0);
if (state_ == State::RUNNING_L) if (state_ == State::RUNNING_L)
state_ = State::IDLE; state_ = State::IDLE;
} }


// Jump // 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; body_.vel_.y_ = -JUMP_FORCE;
} }



+ 17
- 2
libswan/include/swan/Game.h Näytä tiedosto



class Game { class Game {
public: 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 loadMod(const std::string &path);
void createWorld(std::string worldgen); 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(); 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 draw();
void update(float dt); void update(float dt);
std::unique_ptr<World> world_ = NULL; std::unique_ptr<World> world_ = NULL;


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

+ 3
- 7
libswan/src/Game.cc Näytä tiedosto

} }


TilePos Game::getMouseTile() { TilePos Game::getMouseTile() {
auto mousePos = sf::Mouse::getPosition(*win_.window_);
auto mousePos = getMousePos();
return TilePos( 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() { void Game::draw() {

+ 30
- 4
src/main.cc Näytä tiedosto

while (window.isOpen()) { while (window.isOpen()) {
sf::Event event; sf::Event event;
while (window.pollEvent(event)) { while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
switch (event.type) {
case sf::Event::Closed:
window.close(); 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…
Peruuta
Tallenna