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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "Game.h"
  2. #include <dlfcn.h>
  3. #include <math.h>
  4. #include <SFML/Window/Mouse.hpp>
  5. #include "Tile.h"
  6. #include "Asset.h"
  7. namespace Swan {
  8. void Game::loadMod(const std::string &path) {
  9. std::string dlpath = path + "/mod.so";
  10. void *dl = dlopen(dlpath.c_str(), RTLD_LAZY);
  11. if (dl == NULL) {
  12. fprintf(stderr, "%s\n", dlerror());
  13. return;
  14. }
  15. void (*mod_init)(Mod &) = (void (*)(Mod &))dlsym(dl, "mod_init");
  16. if (mod_init == NULL) {
  17. fprintf(stderr, "%s\n", dlerror());
  18. }
  19. registered_mods_.push_back(Mod());
  20. Mod &mod = registered_mods_.back();
  21. mod.path_ = path;
  22. mod_init(mod);
  23. }
  24. void Game::createWorld(std::string worldgen) {
  25. world_.reset(new World(this));
  26. for (auto &mod: registered_mods_) {
  27. world_->registerTile(std::shared_ptr<Tile>(Tile::createInvalid()));
  28. for (auto &tile: mod.tiles_)
  29. world_->registerTile(tile);
  30. for (auto &item: mod.items_)
  31. world_->registerItem(item);
  32. for (auto &worldgen: mod.worldgens_)
  33. world_->registerWorldGen(worldgen);
  34. for (auto &entity: mod.entities_)
  35. world_->registerEntity(entity);
  36. for (auto &asset: mod.assets_)
  37. world_->registerAsset(asset);
  38. }
  39. world_->setWorldGen(worldgen);
  40. world_->setCurrentPlane(world_->addPlane());
  41. world_->spawnPlayer();
  42. }
  43. TilePos Game::getMouseTile() {
  44. auto mousePos = getMousePos();
  45. return TilePos(
  46. (int)floor(win_.cam_.x_ + mousePos.x_ / (Swan::TILE_SIZE * win_.scale_)),
  47. (int)floor(win_.cam_.y_ + mousePos.y_ / (Swan::TILE_SIZE * win_.scale_)));
  48. }
  49. void Game::draw() {
  50. world_->draw(win_);
  51. }
  52. void Game::update(float dt) {
  53. world_->update(dt);
  54. }
  55. void Game::tick() {
  56. world_->tick();
  57. }
  58. void Game::initGlobal() {
  59. Tile::initGlobal();
  60. Item::initGlobal();
  61. Asset::initGlobal();
  62. }
  63. }