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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include <bitset>
  3. #include <map>
  4. #include <string>
  5. #include <optional>
  6. #include <GLFW/glfw3.h>
  7. #include <cygnet/Renderer.h>
  8. #include <cygnet/util.h>
  9. #include "common.h"
  10. #include "Mod.h"
  11. #include "World.h"
  12. namespace Swan {
  13. class Game {
  14. public:
  15. void createWorld(const std::string &worldgen, const std::vector<std::string> &modPaths);
  16. void onKeyDown(int scancode) {
  17. pressedKeys_[scancode] = true;
  18. didPressKeys_[scancode] = true;
  19. }
  20. void onKeyUp(int scancode) {
  21. pressedKeys_[scancode] = false;
  22. didReleaseKeys_[scancode] = true;
  23. }
  24. void onMouseMove(float x, float y) {
  25. mousePos_ = (Vec2{x, y} / (Vec2)cam_.size) * renderer_.winScale();
  26. }
  27. void onMouseDown(int button) {
  28. pressedButtons_[button] = true;
  29. didPressButtons_[button] = true;
  30. }
  31. void onMouseUp(int button) {
  32. pressedButtons_[button] = false;
  33. didReleaseButtons_[button] = true;
  34. }
  35. void onScrollWheel(double dy) {
  36. didScroll_ += dy;
  37. }
  38. bool isKeyPressed(int key) { return pressedKeys_[glfwGetKeyScancode(key)]; }
  39. bool wasKeyPressed(int key) { return didPressKeys_[glfwGetKeyScancode(key)]; }
  40. bool wasKeyReleased(int key) { return didReleaseKeys_[glfwGetKeyScancode(key)]; }
  41. Vec2 getMousePos() { return mousePos_; }
  42. bool isMousePressed(int button) { return pressedButtons_[button]; }
  43. bool wasMousePressed(int button) { return didPressButtons_[button]; }
  44. bool wasMouseReleased(int button) { return didReleaseButtons_[button]; }
  45. double wasWheelScrolled() { return didScroll_; }
  46. TilePos getMouseTile();
  47. Cygnet::Color backgroundColor();
  48. void draw();
  49. void update(float dt);
  50. void tick(float dt);
  51. std::unique_ptr<World> world_ = NULL;
  52. Cygnet::Renderer renderer_;
  53. Cygnet::RenderCamera cam_{.zoom = 0.125};
  54. private:
  55. std::bitset<512> pressedKeys_;
  56. std::bitset<512> didPressKeys_;
  57. std::bitset<512> didReleaseKeys_;
  58. Vec2 mousePos_;
  59. std::bitset<8> pressedButtons_;
  60. std::bitset<8> didPressButtons_;
  61. std::bitset<8> didReleaseButtons_;
  62. double didScroll_ = 0;
  63. };
  64. }