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 "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. }
  28. void Game::update(float dt) {
  29. // Zoom the window using the scroll wheel
  30. cam_.zoom += (float)wasWheelScrolled() * 0.1f * cam_.zoom;
  31. if (cam_.zoom > 3)
  32. cam_.zoom = 3;
  33. else if (cam_.zoom < 0.3)
  34. cam_.zoom = 0.3;
  35. world_->update(dt);
  36. didScroll_ = 0;
  37. didPressKeys_.reset();
  38. didReleaseKeys_.reset();
  39. didPressButtons_.reset();
  40. didReleaseButtons_.reset();
  41. }
  42. void Game::tick(float dt) {
  43. world_->tick(dt);
  44. }
  45. }