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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 pos = (getMousePos() * 2 - renderer_.winScale()) / cam_.zoom + cam_.pos;
  17. return TilePos{(int)floor(pos.x), (int)floor(pos.y)};
  18. }
  19. Cygnet::Color Game::backgroundColor() {
  20. return world_->backgroundColor();
  21. }
  22. void Game::draw() {
  23. world_->draw(renderer_);
  24. renderer_.draw(cam_);
  25. }
  26. void Game::update(float dt) {
  27. // Zoom the window using the scroll wheel
  28. cam_.zoom += (float)wasWheelScrolled() * 0.05f * cam_.zoom;
  29. if (cam_.zoom > 1)
  30. cam_.zoom = 1;
  31. else if (cam_.zoom < 0.025)
  32. cam_.zoom = 0.025;
  33. world_->update(dt);
  34. didScroll_ = 0;
  35. didPressKeys_.reset();
  36. didReleaseKeys_.reset();
  37. didPressButtons_.reset();
  38. didReleaseButtons_.reset();
  39. }
  40. void Game::tick(float dt) {
  41. world_->tick(dt);
  42. }
  43. }