A 2D tile-based sandbox game.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #include <bitset>
  3. #include <map>
  4. #include <string>
  5. #include <SDL.h>
  6. #include "common.h"
  7. #include "Resource.h"
  8. #include "Mod.h"
  9. #include "World.h"
  10. namespace Swan {
  11. class Game {
  12. public:
  13. Game(Win &win):
  14. win_(win),
  15. mouse_pos_(0, 0) {}
  16. std::unique_ptr<Mod> loadMod(const std::string &path, World &world);
  17. void createWorld(const std::string &worldgen, std::vector<std::string> mods);
  18. void onKeyDown(SDL_Keysym sym) {
  19. pressed_keys_[sym.scancode] = true;
  20. did_press_keys_[sym.scancode] = true;
  21. }
  22. void onKeyUp(SDL_Keysym sym) {
  23. pressed_keys_[sym.scancode] = false;
  24. did_release_keys_[sym.scancode] = true;
  25. }
  26. void onMouseMove(Sint32 x, Sint32 y) {
  27. mouse_pos_ = { x, y };
  28. }
  29. void onMouseDown(Sint32 x, Sint32 y, Uint8 button) {
  30. mouse_pos_ = { x, y };
  31. pressed_buttons_[button] = true;
  32. did_press_buttons_[button] = true;
  33. }
  34. void onMouseUp(Sint32 x, Sint32 y, Uint8 button) {
  35. mouse_pos_ = { x, y };
  36. pressed_buttons_[button] = false;
  37. did_release_buttons_[button] = true;
  38. }
  39. void onScrollWheel(Sint32 y) {
  40. did_scroll_ = (y > 0 ? 1 : -1 );
  41. }
  42. bool isKeyPressed(SDL_Scancode code) { return pressed_keys_[code]; }
  43. bool wasKeyPressed(SDL_Scancode code) { return did_press_keys_[code]; }
  44. bool wasKeyReleased(SDL_Scancode code) { return did_release_keys_[code]; }
  45. Vec2i getMousePos() { return mouse_pos_; }
  46. bool isMousePressed(Uint8 button) { return pressed_buttons_[button]; }
  47. bool wasMousePressed(Uint8 button) { return did_press_buttons_[button]; }
  48. bool wasMouseReleased(Uint8 button) { return did_release_buttons_[button]; }
  49. int wasWheelScrolled() { return did_scroll_; }
  50. TilePos getMouseTile();
  51. SDL_Color backgroundColor();
  52. void draw();
  53. void update(float dt);
  54. void tick(float dt);
  55. std::unique_ptr<World> world_ = NULL;
  56. std::unique_ptr<ImageResource> invalid_image_ = NULL;
  57. std::unique_ptr<Tile> invalid_tile_ = NULL;
  58. std::unique_ptr<Item> invalid_item_ = NULL;
  59. Win &win_;
  60. private:
  61. std::bitset<SDL_NUM_SCANCODES> pressed_keys_;
  62. std::bitset<SDL_NUM_SCANCODES> did_press_keys_;
  63. std::bitset<SDL_NUM_SCANCODES> did_release_keys_;
  64. Vec2i mouse_pos_;
  65. std::bitset<SDL_BUTTON_X2> pressed_buttons_;
  66. std::bitset<SDL_BUTTON_X2> did_press_buttons_;
  67. std::bitset<SDL_BUTTON_X2> did_release_buttons_;
  68. int did_scroll_ = 0;
  69. };
  70. }