A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Game.cc 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "Game.h"
  2. #include <math.h>
  3. #include <time.h>
  4. #include <memory>
  5. #include "log.h"
  6. #include "Tile.h"
  7. #include "OS.h"
  8. #include "Win.h"
  9. namespace Swan {
  10. std::unique_ptr<Mod> Game::loadMod(const std::string &path) {
  11. OS::Dynlib dl(path + "/mod");
  12. auto init = dl.get<void (*)(Swan::Mod &)>("mod_init");
  13. if (init == NULL) {
  14. warn << path << ": No 'mod_init' function!";
  15. return nullptr;
  16. }
  17. std::unique_ptr<Mod> mod = std::make_unique<Mod>(path, std::move(dl));
  18. init(*mod);
  19. return mod;
  20. }
  21. void Game::createWorld(const std::string &worldgen, std::vector<std::unique_ptr<Mod>> &&mods) {
  22. world_.reset(new World(this, time(NULL)));
  23. for (auto &mod: mods) {
  24. world_->addMod(std::move(mod));
  25. }
  26. world_->setWorldGen(worldgen);
  27. world_->setCurrentPlane(world_->addPlane());
  28. world_->spawnPlayer();
  29. }
  30. TilePos Game::getMouseTile() {
  31. auto mousePos = getMousePos();
  32. return TilePos(
  33. (int)floor(win_.cam_.x + mousePos.x / (Swan::TILE_SIZE * win_.zoom_)),
  34. (int)floor(win_.cam_.y + mousePos.y / (Swan::TILE_SIZE * win_.zoom_)));
  35. }
  36. void Game::draw() {
  37. world_->draw(win_);
  38. }
  39. void Game::update(float dt) {
  40. world_->update(dt);
  41. // Zoom the window using the scroll wheel
  42. win_.zoom_ += (float)wasWheelScrolled() * 0.1f * win_.zoom_;
  43. if (win_.zoom_ > 3)
  44. win_.zoom_ = 3;
  45. else if (win_.zoom_ < 0.3)
  46. win_.zoom_ = 0.3;
  47. did_scroll_ = 0;
  48. did_press_keys_.reset();
  49. did_release_keys_.reset();
  50. did_press_buttons_.reset();
  51. did_release_buttons_.reset();
  52. }
  53. void Game::tick(float dt) {
  54. world_->tick(dt);
  55. }
  56. }