💿🐜 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.

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