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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "Game.h"
  2. #include <math.h>
  3. #include <time.h>
  4. #include <memory>
  5. #include "Tile.h"
  6. #include "OS.h"
  7. #include "Win.h"
  8. namespace Swan {
  9. std::unique_ptr<Mod> Game::loadMod(const std::string &path) {
  10. OS::Dynlib dl(path + "/mod");
  11. auto init = dl.get<void (*)(Swan::Mod &)>("mod_init");
  12. if (init == NULL) {
  13. fprintf(stderr, "%s: No 'mod_init' function!\n", path.c_str());
  14. return NULL;
  15. }
  16. std::unique_ptr<Mod> mod = std::make_unique<Mod>(path, win_.renderer_);
  17. init(*mod);
  18. return mod;
  19. }
  20. void Game::createWorld(const std::string &worldgen, std::vector<std::unique_ptr<Mod>> &&mods) {
  21. world_.reset(new World(this, time(NULL)));
  22. for (auto &mod: mods) {
  23. world_->addMod(std::move(mod));
  24. }
  25. world_->setWorldGen(worldgen);
  26. world_->setCurrentPlane(world_->addPlane());
  27. world_->spawnPlayer();
  28. }
  29. TilePos Game::getMouseTile() {
  30. auto mousePos = getMousePos();
  31. return TilePos(
  32. (int)floor(win_.cam_.x + mousePos.x / (Swan::TILE_SIZE * win_.scale_)),
  33. (int)floor(win_.cam_.y + mousePos.y / (Swan::TILE_SIZE * win_.scale_)));
  34. }
  35. void Game::draw() {
  36. world_->draw(win_);
  37. }
  38. void Game::update(float dt) {
  39. world_->update(dt);
  40. }
  41. void Game::tick(float dt) {
  42. world_->tick(dt);
  43. }
  44. }