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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. namespace Swan {
  9. void Game::createWorld(const std::string &worldgen, const std::vector<std::string> &modPaths) {
  10. world_.reset(new World(this, time(NULL), modPaths));
  11. world_->setWorldGen(worldgen);
  12. world_->setCurrentPlane(world_->addPlane());
  13. world_->spawnPlayer();
  14. }
  15. TilePos Game::getMouseTile() {
  16. auto mousePos = getMousePos();
  17. return TilePos(
  18. (int)floor(cam_.pos.x + mousePos.x / (Swan::TILE_SIZE * cam_.zoom)),
  19. (int)floor(cam_.pos.y + mousePos.y / (Swan::TILE_SIZE * cam_.zoom)));
  20. }
  21. SDL_Color Game::backgroundColor() {
  22. return world_->backgroundColor();
  23. }
  24. void Game::draw() {
  25. world_->draw(renderer_);
  26. renderer_.draw(cam_);
  27. info << "Rendered cam pos " << cam_.pos << " size " << cam_.size << " zoom " << cam_.zoom;
  28. }
  29. void Game::update(float dt) {
  30. // Zoom the window using the scroll wheel
  31. cam_.zoom += (float)wasWheelScrolled() * 0.1f * cam_.zoom;
  32. if (cam_.zoom > 3)
  33. cam_.zoom = 3;
  34. else if (cam_.zoom < 0.3)
  35. cam_.zoom = 0.3;
  36. world_->update(dt);
  37. didScroll_ = 0;
  38. didPressKeys_.reset();
  39. didReleaseKeys_.reset();
  40. didPressButtons_.reset();
  41. didReleaseButtons_.reset();
  42. }
  43. void Game::tick(float dt) {
  44. world_->tick(dt);
  45. }
  46. }