Game.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * 2D Game Engine
  3. * Game.cpp: Base Game engine class
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All Right Reserved
  6. *
  7. * Created by Manoël Trapier on 09/02/2021.
  8. */
  9. #include <SDL.h>
  10. #include <SDL_image.h>
  11. #include <glm/glm.hpp>
  12. #include <stdlib.h>
  13. #include <memory>
  14. #include <fstream>
  15. #include <Game.h>
  16. #include <Logger.h>
  17. #include <ECS.h>
  18. #include <Components/Animation.h>
  19. #include <Components/Transform.h>
  20. #include <Components/RigidBody.h>
  21. #include <Components/Sprite.h>
  22. #include <Components/BoxCollider.h>
  23. #include <Components/KeyboardControl.h>
  24. #include <Components/CameraFollow.h>
  25. #include <Components/ProjectileEmitter.h>
  26. #include <Components/Heakth.h>
  27. #include <Systems/Movement.h>
  28. #include <Systems/Render.h>
  29. #include <Systems/Animation.h>
  30. #include <Systems/Collision.h>
  31. #include <Systems/Damage.h>
  32. #include <Systems/KeyboardMovement.h>
  33. #include <Systems/CameraMovement.h>
  34. #include <Systems/ProjectileEmit.h>
  35. #include <Systems/ProjectileLifeCycle.h>
  36. uint32_t Game::windowsWidth = 0;
  37. uint32_t Game::windowsHeight = 0;
  38. uint32_t Game::mapWidth = 0;
  39. uint32_t Game::mapHeight = 0;
  40. Game::Game()
  41. {
  42. this->isRunning = false;
  43. this->isDebug = false;
  44. this->millisecsPerviousFrame = 0;
  45. this->registry = std::make_unique<Registry>();
  46. this->assetStore = std::make_unique<AssetStore>();
  47. this->eventBus = std::make_unique<EventBus>();
  48. }
  49. Game::~Game()
  50. {
  51. }
  52. void Game::Initialize()
  53. {
  54. int ret;
  55. SDL_DisplayMode displayMode;
  56. ret = SDL_Init(SDL_INIT_EVERYTHING);
  57. if (ret)
  58. {
  59. Logger::Critical("SDL Initialisation error.");
  60. return;
  61. }
  62. SDL_GetCurrentDisplayMode(0, &displayMode);
  63. Game::windowsWidth = 800; //displayMode.w;
  64. Game::windowsHeight = 600; //displayMode.h;
  65. this->window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  66. Game::windowsWidth, Game::windowsHeight, SDL_WINDOW_BORDERLESS);
  67. if (!window)
  68. {
  69. Logger::Critical("SDL window creation error.");
  70. return;
  71. }
  72. this->renderer = SDL_CreateRenderer(this->window, -1, 0);
  73. if (!renderer)
  74. {
  75. Logger::Critical("SDL renderer creation error.");
  76. return;
  77. }
  78. //SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
  79. this->camera.x = 0;
  80. this->camera.y = 0;
  81. this->camera.w = Game::windowsWidth;
  82. this->camera.h = Game::windowsHeight;
  83. this->isRunning = true;
  84. }
  85. void Game::Destroy()
  86. {
  87. if (this->renderer)
  88. {
  89. SDL_DestroyRenderer(this->renderer);
  90. }
  91. if (this->window)
  92. {
  93. SDL_DestroyWindow(this->window);
  94. }
  95. SDL_Quit();
  96. }
  97. void Game::Run()
  98. {
  99. this->Setup();
  100. while(this->isRunning)
  101. {
  102. this->ProcessInput();
  103. this->Update();
  104. this->Render();
  105. }
  106. }
  107. void Game::LoadLevel(int level)
  108. {
  109. this->registry->addSystem<MovementSystem>();
  110. this->registry->addSystem<RenderSystem>();
  111. this->registry->addSystem<AnimationSystem>();
  112. this->registry->addSystem<CollisionSystem>();
  113. this->registry->addSystem<DamageSystem>();
  114. this->registry->addSystem<KeyboardMovementSystem>();
  115. this->registry->addSystem<CameraMovementSystem>();
  116. this->registry->addSystem<ProjectileEmitSystem>();
  117. this->registry->addSystem<ProjectileLifeCycleSystem>();
  118. this->assetStore->addTexture(this->renderer, "tank-image", "assets/images/tank-panther-right.png");
  119. this->assetStore->addTexture(this->renderer, "truck-image", "assets/images/truck-ford-right.png");
  120. this->assetStore->addTexture(this->renderer, "chopper-image", "assets/images/chopper-spritesheet.png");
  121. this->assetStore->addTexture(this->renderer, "main-tileset", "assets/tilemaps/jungle.png");
  122. this->assetStore->addTexture(this->renderer, "radar-image", "assets/images/radar.png");
  123. this->assetStore->addTexture(this->renderer, "bullet-image", "assets/images/bullet.png");
  124. Logger::Debug("struct SpriteComponent size is %d", sizeof(struct SpriteComponent));
  125. // jungle.map
  126. int tileSize = 32;
  127. double tileScale = 2.0;
  128. int mapNumCols = 25;
  129. int mapNumRows = 20;
  130. std::fstream mapFile;
  131. mapFile.open("assets/tilemaps/jungle.map");
  132. assert(mapFile.is_open());
  133. for (int y = 0; y < mapNumRows; y++)
  134. {
  135. for (int x = 0; x < mapNumCols; x++)
  136. {
  137. char ch[2] = {0, 0};
  138. mapFile.get(ch[0]);
  139. int srcRectY = atoi(ch) * tileSize;
  140. mapFile.get(ch[0]);
  141. int srcRectX = atoi(ch) * tileSize;
  142. mapFile.ignore();
  143. Entity tile = registry->createEntity();
  144. tile.addComponent<TransformComponent>(glm::vec2(x * (tileScale * tileSize), y * (tileScale * tileSize)), glm::vec2(tileScale, tileScale), 0.0);
  145. tile.addComponent<SpriteComponent>("main-tileset", 0, tileSize, tileSize, false, srcRectX, srcRectY);
  146. tile.group("tiles");
  147. }
  148. }
  149. mapFile.close();
  150. Game::mapWidth = mapNumCols * tileSize * tileScale;
  151. Game::mapHeight = mapNumRows * tileSize * tileScale;
  152. Entity chopper = this->registry->createEntity();
  153. chopper.addComponent<TransformComponent>(glm::vec2(10, 100), glm::vec2(1, 1), 0);
  154. chopper.addComponent<SpriteComponent>("chopper-image", 2, 32, 32);
  155. chopper.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  156. chopper.addComponent<BoxColliderComponent>(32, 32);
  157. chopper.addComponent<AnimationComponent>(2, 15, true);
  158. chopper.addComponent<KeyboardControlComponent>(glm::vec2(0, -80), glm::vec2(80, 0),
  159. glm::vec2(0, 80), glm::vec2(-80, 0));
  160. chopper.addComponent<ProjectileEmitterComponent>(glm::vec2(180, 180), 0, 10000, 10, true);
  161. chopper.addComponent<HealthComponent>(100);
  162. chopper.addComponent<CameraFollowComponent>();
  163. chopper.tag("player");
  164. Entity radar = this->registry->createEntity();
  165. radar.addComponent<TransformComponent>(glm::vec2(this->windowsWidth - 75, 10), glm::vec2(1, 1), 0);
  166. radar.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  167. radar.addComponent<SpriteComponent>("radar-image", 3, 64, 64, true);
  168. radar.addComponent<AnimationComponent>(8, 5, true);
  169. Entity tank = this->registry->createEntity();
  170. tank.addComponent<TransformComponent>(glm::vec2(500, 10), glm::vec2(1, 1), 0);
  171. tank.addComponent<RigidBodyComponent>(glm::vec2(-3, 0));
  172. tank.addComponent<SpriteComponent>("tank-image", 1, 32, 32);
  173. tank.addComponent<BoxColliderComponent>(32, 32);
  174. tank.addComponent<ProjectileEmitterComponent>(glm::vec2(100, 0), 5000, 3000, 10, false);
  175. tank.addComponent<HealthComponent>(100);
  176. tank.group("enemies");
  177. Entity truck = this->registry->createEntity();
  178. truck.addComponent<TransformComponent>(glm::vec2(10, 10), glm::vec2(1, 1), 0);
  179. truck.addComponent<RigidBodyComponent>(glm::vec2(2, 0));
  180. truck.addComponent<SpriteComponent>("truck-image", 1, 32, 32);
  181. truck.addComponent<BoxColliderComponent>(32, 32);
  182. truck.addComponent<ProjectileEmitterComponent>(glm::vec2(0, 100), 2000, 5000, 10, false);
  183. truck.addComponent<HealthComponent>(100);
  184. truck.group("enemies");
  185. }
  186. void Game::Setup()
  187. {
  188. LoadLevel(1);
  189. }
  190. void Game::Update()
  191. {
  192. uint32_t timeToWait = MILLISECS_PER_FRAME - (SDL_GetTicks() - this->millisecsPerviousFrame);
  193. if (timeToWait > 0 && timeToWait <= MILLISECS_PER_FRAME)
  194. {
  195. SDL_Delay(timeToWait);
  196. }
  197. double deltaTime = (SDL_GetTicks() - this->millisecsPerviousFrame) / 1000.0;
  198. this->millisecsPerviousFrame = SDL_GetTicks();
  199. eventBus->reset();
  200. registry->getSystem<DamageSystem>().subscriptToEvents(eventBus);
  201. registry->getSystem<KeyboardMovementSystem>().subscriptToEvents(eventBus);
  202. registry->getSystem<ProjectileEmitSystem>().subscriptToEvents(eventBus);
  203. registry->update();
  204. registry->getSystem<MovementSystem>().update(deltaTime);
  205. registry->getSystem<AnimationSystem>().update();
  206. registry->getSystem<CollisionSystem>().update(this->eventBus);
  207. registry->getSystem<ProjectileEmitSystem>().update(this->registry);
  208. registry->getSystem<ProjectileLifeCycleSystem>().update();
  209. registry->getSystem<CameraMovementSystem>().update(this->camera);
  210. }
  211. void Game::ProcessInput()
  212. {
  213. SDL_Event event;
  214. while(SDL_PollEvent(&event))
  215. {
  216. switch(event.type)
  217. {
  218. case SDL_QUIT:
  219. this->isRunning = false;
  220. break;
  221. case SDL_KEYDOWN:
  222. if (event.key.keysym.sym == SDLK_ESCAPE)
  223. {
  224. this->isRunning = false;
  225. }
  226. else if (event.key.keysym.sym == SDLK_d)
  227. {
  228. this->isDebug = !this->isDebug;
  229. }
  230. else
  231. {
  232. this->eventBus->emitEvent<KeyPressedEvent>(event.key.keysym.sym);
  233. }
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. }
  240. void Game::Render()
  241. {
  242. SDL_SetRenderDrawColor(this->renderer, 21, 21, 21, 255);
  243. SDL_RenderClear(this->renderer);
  244. registry->getSystem<RenderSystem>().update(this->renderer, this->assetStore, this->camera);
  245. if (this->isDebug)
  246. {
  247. registry->getSystem<CollisionSystem>().debugRender(this->renderer, this->camera);
  248. }
  249. SDL_RenderPresent(this->renderer);
  250. }