💿🐜 Antkeeper source code https://antkeeper.com
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.

580 lines
15 KiB

  1. /*
  2. * Copyright (C) 2017-2019 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef GAME_HPP
  20. #define GAME_HPP
  21. #include <emergent/emergent.hpp>
  22. using namespace Emergent;
  23. #include "state-machine.hpp"
  24. #include "entity/entity-id.hpp"
  25. #include "scheduled-function-event.hpp"
  26. #include <array>
  27. #include <map>
  28. #include <string>
  29. #include <vector>
  30. #include <fstream>
  31. class GameState;
  32. class SplashState;
  33. class SandboxState;
  34. class UIContainer;
  35. class UIBatcher;
  36. class UIImage;
  37. class UILabel;
  38. class UIRenderPass;
  39. class ClearRenderPass;
  40. class SkyRenderPass;
  41. class ShadowMapRenderPass;
  42. class LightingRenderPass;
  43. class SilhouetteRenderPass;
  44. class FinalRenderPass;
  45. class ResourceManager;
  46. class CameraRig;
  47. class OrbitCam;
  48. class FreeCam;
  49. class Tool;
  50. class Lens;
  51. class Forceps;
  52. class Brush;
  53. class ParticleSystem;
  54. class EntityManager;
  55. class ComponentManager;
  56. class SystemManager;
  57. class SoundSystem;
  58. class CollisionSystem;
  59. class RenderSystem;
  60. class CameraSystem;
  61. class ToolSystem;
  62. class BehaviorSystem;
  63. class SteeringSystem;
  64. class LocomotionSystem;
  65. class TerrainSystem;
  66. class ComponentBase;
  67. class Menu;
  68. class MenuItem;
  69. enum class ComponentType;
  70. typedef std::vector<std::vector<std::string>> StringTable;
  71. class Game:
  72. public Application,
  73. public StateMachine,
  74. public EventHandler<ScheduledFunctionEvent>
  75. {
  76. public:
  77. /**
  78. * Creates a game instance.
  79. *
  80. * @param argc Argument count
  81. * @param argv Argument list
  82. */
  83. Game(int argc, char* argv[]);
  84. /// Destroys a game instance.
  85. virtual ~Game();
  86. /**
  87. * Gets a string in the current language.
  88. *
  89. * @param name Name of the string.
  90. * @return String in the current language.
  91. */
  92. std::string getString(const std::string& name) const;
  93. /**
  94. * Changes the current language.
  95. *
  96. * @param languageIndex Index of the language to use.
  97. */
  98. void changeLanguage(std::size_t nextLanguageIndex);
  99. void nextLanguage();
  100. /// Returns the number of available languages.
  101. std::size_t getLanguageCount() const;
  102. /// Returns the index of the current language.
  103. std::size_t getLanguageIndex() const;
  104. void openMenu(Menu* menu, int selectedItemIndex);
  105. void closeCurrentMenu();
  106. void selectMenuItem(int index, bool tween);
  107. void selectNextMenuItem();
  108. void selectPreviousMenuItem();
  109. void activateMenuItem();
  110. void activateLastMenuItem();
  111. void toggleFullscreen();
  112. void toggleVSync();
  113. void setUpdateRate(double frequency);
  114. /**
  115. * Changes the game state.
  116. *
  117. * @param state New game state.
  118. */
  119. void changeState(GameState* state);
  120. const EventDispatcher* getEventDispatcher() const;
  121. EventDispatcher* getEventDispatcher();
  122. const Animator* getAnimator() const;
  123. Animator* getAnimator();
  124. void fadeIn(float duration, const Vector3& color, std::function<void()> callback);
  125. void fadeOut(float duration, const Vector3& color, std::function<void()> callback);
  126. void stopFade();
  127. void selectTool(int toolIndex);
  128. private:
  129. virtual void setup();
  130. virtual void input();
  131. virtual void update(float t, float dt);
  132. virtual void render();
  133. virtual void exit();
  134. virtual void handleEvent(const WindowResizedEvent& event);
  135. virtual void handleEvent(const GamepadConnectedEvent& event);
  136. virtual void handleEvent(const GamepadDisconnectedEvent& event);
  137. virtual void handleEvent(const ScheduledFunctionEvent& event);
  138. void setupDebugging();
  139. void setupLocalization();
  140. void setupWindow();
  141. void setupGraphics();
  142. void setupUI();
  143. void setupControls();
  144. void setupGameplay();
  145. void resetSettings();
  146. void loadSettings();
  147. void saveSettings();
  148. void loadStrings();
  149. void loadFonts();
  150. void loadControlProfile(const std::string& profileName);
  151. void saveControlProfile(const std::string& profileName);
  152. std::array<std::string, 3> getInputMappingStrings(const InputMapping* mapping);
  153. void remapControl(Control* control);
  154. void resetControls();
  155. // Callback for the input mapper
  156. void inputMapped(const InputMapping& mapping);
  157. void resizeUI(int w, int h);
  158. void restringUI();
  159. void restringControlMenuItem(MenuItem* item, const std::string& name);
  160. void resizeRenderTargets();
  161. void setTimeOfDay(float time);
  162. void toggleWireframe();
  163. void queueScreenshot();
  164. void screenshot();
  165. // State functions
  166. void enterSplashState();
  167. void exitSplashState();
  168. void enterLoadingState();
  169. void exitLoadingState();
  170. void enterTitleState();
  171. void exitTitleState();
  172. void enterPlayState();
  173. void exitPlayState();
  174. void skipSplash();
  175. void togglePause();
  176. void continueGame();
  177. void newGame();
  178. void returnToMainMenu();
  179. public:
  180. EntityID createInstance();
  181. EntityID createInstanceOf(const std::string& templateName);
  182. void destroyInstance(EntityID entity);
  183. void addComponent(EntityID entity, ComponentBase* component);
  184. void removeComponent(EntityID entity, ComponentType type);
  185. void setTranslation(EntityID entity, const Vector3& translation);
  186. void setRotation(EntityID entity, const Quaternion& rotation);
  187. void setScale(EntityID entity, const Vector3& scale);
  188. void setTerrainPatchPosition(EntityID entity, const std::tuple<int, int>& position);
  189. void boxSelect(float x, float y, float w, float h);
  190. template <typename T>
  191. bool readSetting(const std::string& name, T* value) const;
  192. public:
  193. // Game states
  194. StateMachine::State splashState;
  195. StateMachine::State loadingState;
  196. StateMachine::State titleState;
  197. StateMachine::State playState;
  198. GameState* currentState;
  199. SandboxState* sandboxState;
  200. // Paths
  201. std::string dataPath;
  202. std::string configPath;
  203. std::string controlsPath;
  204. // Settings
  205. StringTable* settingsTable;
  206. std::map<std::string, std::size_t> settingsMap;
  207. // Localization
  208. StringTable* stringTable;
  209. std::map<std::string, std::size_t> stringMap;
  210. std::size_t languageCount;
  211. std::size_t languageIndex;
  212. // Window management
  213. Window* window;
  214. int w, h;
  215. float dpi;
  216. float fontSizePX;
  217. // Input
  218. Mouse* mouse;
  219. Keyboard* keyboard;
  220. // Master control set
  221. ControlSet controls;
  222. // System control set
  223. ControlSet systemControls;
  224. Control exitControl;
  225. Control toggleFullscreenControl;
  226. Control takeScreenshotControl;
  227. // Menu control set
  228. ControlSet menuControls;
  229. Control menuUpControl;
  230. Control menuDownControl;
  231. Control menuLeftControl;
  232. Control menuRightControl;
  233. Control menuActivateControl;
  234. Control menuBackControl;
  235. // Camera control set
  236. ControlSet cameraControls;
  237. Control moveForwardControl;
  238. Control moveLeftControl;
  239. Control moveBackControl;
  240. Control moveRightControl;
  241. Control zoomInControl;
  242. Control zoomOutControl;
  243. Control orbitCCWControl;
  244. Control orbitCWControl;
  245. Control adjustCameraControl;
  246. Control dragCameraControl;
  247. Control pauseControl;
  248. // Tool control set
  249. ControlSet toolControls;
  250. Control changeToolControl;
  251. Control useToolControl;
  252. // Editor control set
  253. ControlSet editorControls;
  254. Control toggleEditModeControl;
  255. // Debug control set
  256. ControlSet debugControls;
  257. Control toggleWireframeControl;
  258. // Map of control names
  259. std::map<std::string, Control*> controlNameMap;
  260. // Input mapper
  261. InputMapper* inputMapper;
  262. // Logic
  263. float time;
  264. float timestep;
  265. // UI
  266. Typeface* debugTypeface;
  267. Font* debugFont;
  268. Typeface* menuTypeface;
  269. Font* menuFont;
  270. BillboardBatch* uiBatch;
  271. UIBatcher* uiBatcher;
  272. UIContainer* uiRootElement;
  273. UIImage* splashBackgroundImage;
  274. UIImage* splashImage;
  275. UIContainer* hudContainer;
  276. UIImage* toolIndicatorBGImage;
  277. UIImage* toolIndicatorIconImage;
  278. Rect* toolIndicatorsBounds;
  279. UIImage* toolIconBrushImage;
  280. UIImage* toolIconLensImage;
  281. UIImage* toolIconForcepsImage;
  282. UIImage* toolIconSpadeImage;
  283. UIImage* toolIconCameraImage;
  284. UIImage* toolIconMicrochipImage;
  285. UIImage* toolIconTestTubeImage;
  286. UIContainer* buttonContainer;
  287. UIImage* playButtonBGImage;
  288. UIImage* fastForwardButtonBGImage;
  289. UIImage* pauseButtonBGImage;
  290. UIImage* playButtonImage;
  291. UIImage* fastForwardButtonImage;
  292. UIImage* pauseButtonImage;
  293. UIContainer* radialMenuContainer;
  294. UIImage* radialMenuBackgroundImage;
  295. UIImage* radialMenuImage;
  296. UIImage* radialMenuSelectorImage;
  297. UIImage* blackoutImage;
  298. UIImage* cameraFlashImage;
  299. UIContainer* antTag;
  300. UIContainer* antLabelContainer;
  301. UILabel* fpsLabel;
  302. UILabel* antLabel;
  303. UIImage* antLabelTL; // Top-left
  304. UIImage* antLabelTR; // Top-right
  305. UIImage* antLabelBL; // Bottom-left
  306. UIImage* antLabelBR; // Bottom-right
  307. UIImage* antLabelCC; // Center-center
  308. UIImage* antLabelCT; // Center-top
  309. UIImage* antLabelCB; // Center-bottom
  310. UIImage* antLabelCL; // Center-left
  311. UIImage* antLabelCR; // Center-right
  312. UIImage* antLabelPinHole;
  313. UIImage* antPin;
  314. UIImage* boxSelectionImageBackground;
  315. UIImage* boxSelectionImageTop;
  316. UIImage* boxSelectionImageBottom;
  317. UIImage* boxSelectionImageLeft;
  318. UIImage* boxSelectionImageRight;
  319. UIContainer* boxSelectionContainer;
  320. float boxSelectionBorderWidth;
  321. UIImage* cameraGridY0Image;
  322. UIImage* cameraGridY1Image;
  323. UIImage* cameraGridX0Image;
  324. UIImage* cameraGridX1Image;
  325. UIImage* cameraReticleImage;
  326. UIContainer* cameraGridContainer;
  327. Vector4 cameraGridColor;
  328. Vector4 cameraReticleColor;
  329. // Menu selection
  330. UIImage* menuSelectorImage;
  331. Menu* currentMenu;
  332. Menu* previousMenu;
  333. MenuItem* currentMenuItem;
  334. MenuItem* previousMenuItem;
  335. int menuItemIndex;
  336. Vector4 menuItemActiveColor;
  337. Vector4 menuItemInactiveColor;
  338. // Main menu
  339. Menu* mainMenu;
  340. MenuItem* mainMenuContinueItem;
  341. MenuItem* mainMenuNewGameItem;
  342. MenuItem* mainMenuColoniesItem;
  343. MenuItem* mainMenuSettingsItem;
  344. MenuItem* mainMenuQuitItem;
  345. // Settings menu
  346. Menu* settingsMenu;
  347. MenuItem* settingsMenuControlsItem;
  348. MenuItem* settingsMenuFullscreenItem;
  349. MenuItem* settingsMenuVSyncItem;
  350. MenuItem* settingsMenuLanguageItem;
  351. MenuItem* settingsMenuBackItem;
  352. // Controls menu
  353. Menu* controlsMenu;
  354. MenuItem* controlsMenuMoveForwardItem;
  355. MenuItem* controlsMenuMoveLeftItem;
  356. MenuItem* controlsMenuMoveBackItem;
  357. MenuItem* controlsMenuMoveRightItem;
  358. MenuItem* controlsMenuChangeToolItem;
  359. MenuItem* controlsMenuUseToolItem;
  360. MenuItem* controlsMenuAdjustCameraItem;
  361. MenuItem* controlsMenuPauseItem;
  362. MenuItem* controlsMenuToggleFullscreenItem;
  363. MenuItem* controlsMenuTakeScreenshotItem;
  364. MenuItem* controlsMenuResetToDefaultItem;
  365. MenuItem* controlsMenuBackItem;
  366. // Pause menu
  367. Menu* pauseMenu;
  368. MenuItem* pauseMenuResumeItem;
  369. MenuItem* pauseMenuSettingsItem;
  370. MenuItem* pauseMenuMainMenuItem;
  371. MenuItem* pauseMenuQuitItem;
  372. // Rendering
  373. Renderer renderer;
  374. RenderTarget defaultRenderTarget;
  375. ClearRenderPass* clearPass;
  376. ClearRenderPass* clearSilhouettePass;
  377. SkyRenderPass* skyPass;
  378. UIRenderPass* uiPass;
  379. Compositor uiCompositor;
  380. Compositor defaultCompositor;
  381. int shadowMapResolution;
  382. GLuint shadowMapDepthTextureID;
  383. GLuint shadowMapFramebuffer;
  384. RenderTarget shadowMapRenderTarget;
  385. ShadowMapRenderPass* shadowMapPass;
  386. Compositor shadowMapCompositor;
  387. Texture2D shadowMapDepthTexture;
  388. LightingRenderPass* lightingPass;
  389. SilhouetteRenderPass* silhouettePass;
  390. FinalRenderPass* finalPass;
  391. RenderTarget silhouetteRenderTarget;
  392. // Scene
  393. Scene* worldScene;
  394. Scene* uiScene;
  395. DirectionalLight sunlight;
  396. Camera camera;
  397. Camera sunlightCamera;
  398. Camera uiCamera;
  399. // Animation
  400. Animator animator;
  401. Animation<float> antHillZoomAnimation;
  402. AnimationClip<float> antHillZoomClip;
  403. Animation<float> menuFadeAnimation;
  404. AnimationClip<float> menuFadeInClip;
  405. AnimationClip<float> menuFadeOutClip;
  406. Animation<float> splashFadeInAnimation;
  407. Animation<float> splashFadeOutAnimation;
  408. AnimationClip<float> splashFadeInClip;
  409. AnimationClip<float> splashFadeOutClip;
  410. Animation<float> fadeInAnimation;
  411. Animation<float> fadeOutAnimation;
  412. AnimationClip<float> fadeInClip;
  413. AnimationClip<float> fadeOutClip;
  414. std::function<void()> fadeInEndCallback;
  415. std::function<void()> fadeOutEndCallback;
  416. Animation<float> cameraFlashAnimation;
  417. AnimationClip<float> cameraFlashClip;
  418. Animation<float> menuSelectorSlideAnimation;
  419. AnimationClip<float> menuSelectorSlideClip;
  420. Animation<Vector4> menuItemSelectAnimation;
  421. AnimationClip<Vector4> menuItemSelectClip;
  422. Animation<Vector4> menuItemDeselectAnimation;
  423. AnimationClip<Vector4> menuItemDeselectClip;
  424. // Assets
  425. ResourceManager* resourceManager;
  426. Texture2D* splashTexture;
  427. Texture2D* hudSpriteSheetTexture;
  428. TextureAtlas hudTextureAtlas;
  429. Material* smokeMaterial;
  430. Model* lensModel;
  431. Model* forcepsModel;
  432. Model* brushModel;
  433. // Game
  434. CameraRig* cameraRig;
  435. OrbitCam* orbitCam;
  436. FreeCam* freeCam;
  437. Tool* currentTool;
  438. Lens* lens;
  439. Forceps* forceps;
  440. Brush* brush;
  441. // ECS
  442. EntityManager* entityManager;
  443. ComponentManager* componentManager;
  444. SystemManager* systemManager;
  445. SoundSystem* soundSystem;
  446. CollisionSystem* collisionSystem;
  447. CameraSystem* cameraSystem;
  448. RenderSystem* renderSystem;
  449. ToolSystem* toolSystem;
  450. BehaviorSystem* behaviorSystem;
  451. SteeringSystem* steeringSystem;
  452. LocomotionSystem* locomotionSystem;
  453. ParticleSystem* particleSystem;
  454. TerrainSystem* terrainSystem;
  455. bool screenshotQueued;
  456. bool paused;
  457. // Settings
  458. std::string language;
  459. Vector2 windowedResolution;
  460. Vector2 fullscreenResolution;
  461. bool fullscreen;
  462. bool vsync;
  463. float fontSizePT;
  464. std::string controlProfileName;
  465. bool toggleFullscreenDisabled;
  466. // Debugging
  467. std::ofstream logFileStream;
  468. bool wireframe;
  469. private:
  470. static void saveScreenshot(const std::string& filename, unsigned int width, unsigned int height, unsigned char* pixels);
  471. };
  472. inline const EventDispatcher* Game::getEventDispatcher() const
  473. {
  474. return &eventDispatcher;
  475. }
  476. inline EventDispatcher* Game::getEventDispatcher()
  477. {
  478. return &eventDispatcher;
  479. }
  480. inline const Animator* Game::getAnimator() const
  481. {
  482. return &animator;
  483. }
  484. inline Animator* Game::getAnimator()
  485. {
  486. return &animator;
  487. }
  488. inline std::size_t Game::getLanguageCount() const
  489. {
  490. return languageCount;
  491. }
  492. inline std::size_t Game::getLanguageIndex() const
  493. {
  494. return languageIndex;
  495. }
  496. #endif // GAME_HPP