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. #include "Win.h"
  9. namespace Swan {
  10. void Game::createWorld(const std::string &worldgen, const std::vector<std::string> &modPaths) {
  11. world_.reset(new World(this, time(NULL), modPaths));
  12. world_->setWorldGen(worldgen);
  13. world_->setCurrentPlane(world_->addPlane());
  14. world_->spawnPlayer();
  15. }
  16. TilePos Game::getMouseTile() {
  17. auto mousePos = getMousePos();
  18. return TilePos(
  19. (int)floor(win_.cam_.x + mousePos.x / (Swan::TILE_SIZE * win_.zoom_)),
  20. (int)floor(win_.cam_.y + mousePos.y / (Swan::TILE_SIZE * win_.zoom_)));
  21. }
  22. SDL_Color Game::backgroundColor() {
  23. return world_->backgroundColor();
  24. }
  25. void Game::draw() {
  26. world_->draw(win_);
  27. }
  28. void Game::update(float dt) {
  29. world_->update(dt);
  30. // Zoom the window using the scroll wheel
  31. win_.zoom_ += (float)wasWheelScrolled() * 0.1f * win_.zoom_;
  32. if (win_.zoom_ > 3)
  33. win_.zoom_ = 3;
  34. else if (win_.zoom_ < 0.3)
  35. win_.zoom_ = 0.3;
  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. }