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

607 lines
16 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 setTransform(EntityID entity, const Transform& transform);
  197. void setTranslation(EntityID entity, const Vector3& translation);
  198. void setRotation(EntityID entity, const Quaternion& rotation);
  199. void setScale(EntityID entity, const Vector3& scale);
  200. void translate(EntityID entity, const Vector3& translation);
  201. void rotate(EntityID entity, const Quaternion& rotation);
  202. void scale(EntityID entity, const Vector3& scale);
  203. void setTerrainPatchPosition(EntityID entity, const std::tuple<int, int>& position);
  204. void boxSelect(float x, float y, float w, float h);
  205. template <typename T>
  206. bool readSetting(const std::string& name, T* value) const;
  207. template <typename T>
  208. bool writeSetting(const std::string& name, const T& value) const;
  209. public:
  210. // Game states
  211. StateMachine::State languageSelectState;
  212. StateMachine::State splashState;
  213. StateMachine::State loadingState;
  214. StateMachine::State titleState;
  215. StateMachine::State playState;
  216. GameState* currentState;
  217. SandboxState* sandboxState;
  218. // Paths
  219. std::string dataPath;
  220. std::string configPath;
  221. std::string controlsPath;
  222. // Settings
  223. bool firstRun;
  224. StringTable* settingsTable;
  225. StringTableIndex settingsTableIndex;
  226. // Localization
  227. StringTable* stringTable;
  228. StringTableIndex stringTableIndex;
  229. std::size_t languageCount;
  230. std::size_t currentLanguageIndex;
  231. // Window management
  232. Window* window;
  233. int w, h;
  234. float dpi;
  235. float fontSizePX;
  236. // Input
  237. Mouse* mouse;
  238. Keyboard* keyboard;
  239. // Master control set
  240. ControlSet controls;
  241. // System control set
  242. ControlSet systemControls;
  243. Control exitControl;
  244. Control toggleFullscreenControl;
  245. Control takeScreenshotControl;
  246. // Menu control set
  247. ControlSet menuControls;
  248. Control menuUpControl;
  249. Control menuDownControl;
  250. Control menuLeftControl;
  251. Control menuRightControl;
  252. Control menuActivateControl;
  253. Control menuBackControl;
  254. // Camera control set
  255. ControlSet cameraControls;
  256. Control moveForwardControl;
  257. Control moveLeftControl;
  258. Control moveBackControl;
  259. Control moveRightControl;
  260. Control zoomInControl;
  261. Control zoomOutControl;
  262. Control orbitCCWControl;
  263. Control orbitCWControl;
  264. Control adjustCameraControl;
  265. Control dragCameraControl;
  266. Control pauseControl;
  267. // Tool control set
  268. ControlSet toolControls;
  269. Control changeToolControl;
  270. Control useToolControl;
  271. // Editor control set
  272. ControlSet editorControls;
  273. Control toggleEditModeControl;
  274. // Map of control names
  275. std::map<std::string, Control*> controlNameMap;
  276. // Input mapper
  277. InputMapper* inputMapper;
  278. // Logic
  279. float time;
  280. float timestep;
  281. // UI
  282. Font* debugFont;
  283. Font* menuFont;
  284. std::vector<Font*> languageSelectionFonts;
  285. BillboardBatch* uiBatch;
  286. UIBatcher* uiBatcher;
  287. UIContainer* uiRootElement;
  288. UIImage* splashBackgroundImage;
  289. UIImage* splashImage;
  290. UIContainer* hudContainer;
  291. UIImage* toolIndicatorBGImage;
  292. UIImage* toolIndicatorIconImage;
  293. Rect* toolIndicatorsBounds;
  294. UIImage* toolIconBrushImage;
  295. UIImage* toolIconLensImage;
  296. UIImage* toolIconForcepsImage;
  297. UIImage* toolIconSpadeImage;
  298. UIImage* toolIconCameraImage;
  299. UIImage* toolIconMicrochipImage;
  300. UIImage* toolIconTestTubeImage;
  301. UIContainer* buttonContainer;
  302. UIImage* playButtonBGImage;
  303. UIImage* fastForwardButtonBGImage;
  304. UIImage* pauseButtonBGImage;
  305. UIImage* playButtonImage;
  306. UIImage* fastForwardButtonImage;
  307. UIImage* pauseButtonImage;
  308. UIContainer* radialMenuContainer;
  309. UIImage* radialMenuBackgroundImage;
  310. UIImage* radialMenuImage;
  311. UIImage* radialMenuSelectorImage;
  312. UIImage* blackoutImage;
  313. UIImage* languageSelectBGImage;
  314. UIImage* cameraFlashImage;
  315. UIContainer* antTag;
  316. UIContainer* antLabelContainer;
  317. UILabel* fpsLabel;
  318. UILabel* antLabel;
  319. UIImage* antLabelTL; // Top-left
  320. UIImage* antLabelTR; // Top-right
  321. UIImage* antLabelBL; // Bottom-left
  322. UIImage* antLabelBR; // Bottom-right
  323. UIImage* antLabelCC; // Center-center
  324. UIImage* antLabelCT; // Center-top
  325. UIImage* antLabelCB; // Center-bottom
  326. UIImage* antLabelCL; // Center-left
  327. UIImage* antLabelCR; // Center-right
  328. UIImage* antLabelPinHole;
  329. UIImage* antPin;
  330. UIImage* boxSelectionImageBackground;
  331. UIImage* boxSelectionImageTop;
  332. UIImage* boxSelectionImageBottom;
  333. UIImage* boxSelectionImageLeft;
  334. UIImage* boxSelectionImageRight;
  335. UIContainer* boxSelectionContainer;
  336. float boxSelectionBorderWidth;
  337. UIImage* cameraGridY0Image;
  338. UIImage* cameraGridY1Image;
  339. UIImage* cameraGridX0Image;
  340. UIImage* cameraGridX1Image;
  341. UIImage* cameraReticleImage;
  342. UIContainer* cameraGridContainer;
  343. Vector4 cameraGridColor;
  344. Vector4 cameraReticleColor;
  345. // Menu selection
  346. UIImage* menuSelectorImage;
  347. Menu* currentMenu;
  348. Menu* previousMenu;
  349. MenuItem* currentMenuItem;
  350. MenuItem* previousMenuItem;
  351. int menuItemIndex;
  352. Vector4 standardMenuActiveColor;
  353. Vector4 standardMenuInactiveColor;
  354. Vector4 languageMenuActiveColor;
  355. Vector4 languageMenuInactiveColor;
  356. Vector4 menuItemActiveColor;
  357. Vector4 menuItemInactiveColor;
  358. // Main menu
  359. Menu* mainMenu;
  360. MenuItem* mainMenuContinueItem;
  361. MenuItem* mainMenuNewGameItem;
  362. MenuItem* mainMenuColoniesItem;
  363. MenuItem* mainMenuSettingsItem;
  364. MenuItem* mainMenuQuitItem;
  365. // Settings menu
  366. Menu* settingsMenu;
  367. MenuItem* settingsMenuControlsItem;
  368. MenuItem* settingsMenuFullscreenItem;
  369. MenuItem* settingsMenuVSyncItem;
  370. MenuItem* settingsMenuLanguageItem;
  371. MenuItem* settingsMenuBackItem;
  372. // Controls menu
  373. Menu* controlsMenu;
  374. MenuItem* controlsMenuMoveForwardItem;
  375. MenuItem* controlsMenuMoveLeftItem;
  376. MenuItem* controlsMenuMoveBackItem;
  377. MenuItem* controlsMenuMoveRightItem;
  378. MenuItem* controlsMenuChangeToolItem;
  379. MenuItem* controlsMenuUseToolItem;
  380. MenuItem* controlsMenuAdjustCameraItem;
  381. MenuItem* controlsMenuPauseItem;
  382. MenuItem* controlsMenuToggleFullscreenItem;
  383. MenuItem* controlsMenuTakeScreenshotItem;
  384. MenuItem* controlsMenuResetToDefaultItem;
  385. MenuItem* controlsMenuBackItem;
  386. // Pause menu
  387. Menu* pauseMenu;
  388. MenuItem* pauseMenuResumeItem;
  389. MenuItem* pauseMenuSettingsItem;
  390. MenuItem* pauseMenuMainMenuItem;
  391. MenuItem* pauseMenuQuitItem;
  392. Menu* languageMenu;
  393. std::vector<MenuItem*> languageMenuItems;
  394. // Rendering
  395. Renderer renderer;
  396. RenderTarget defaultRenderTarget;
  397. ClearRenderPass* clearPass;
  398. ClearRenderPass* clearSilhouettePass;
  399. SkyRenderPass* skyPass;
  400. UIRenderPass* uiPass;
  401. Compositor uiCompositor;
  402. Compositor defaultCompositor;
  403. int shadowMapResolution;
  404. GLuint shadowMapDepthTextureID;
  405. GLuint shadowMapFramebuffer;
  406. RenderTarget shadowMapRenderTarget;
  407. ShadowMapRenderPass* shadowMapPass;
  408. Compositor shadowMapCompositor;
  409. Texture2D shadowMapDepthTexture;
  410. LightingRenderPass* lightingPass;
  411. SilhouetteRenderPass* silhouettePass;
  412. FinalRenderPass* finalPass;
  413. RenderTarget silhouetteRenderTarget;
  414. // Scene
  415. Scene* worldScene;
  416. Scene* uiScene;
  417. DirectionalLight sunlight;
  418. Camera camera;
  419. Camera sunlightCamera;
  420. Camera uiCamera;
  421. // Animation
  422. Animator animator;
  423. Animation<float> antHillZoomAnimation;
  424. AnimationClip<float> antHillZoomClip;
  425. Animation<float> menuFadeAnimation;
  426. AnimationClip<float> menuFadeInClip;
  427. AnimationClip<float> menuFadeOutClip;
  428. Animation<float> splashFadeInAnimation;
  429. Animation<float> splashFadeOutAnimation;
  430. AnimationClip<float> splashFadeInClip;
  431. AnimationClip<float> splashFadeOutClip;
  432. Animation<float> fadeInAnimation;
  433. Animation<float> fadeOutAnimation;
  434. AnimationClip<float> fadeInClip;
  435. AnimationClip<float> fadeOutClip;
  436. std::function<void()> fadeInEndCallback;
  437. std::function<void()> fadeOutEndCallback;
  438. Animation<float> cameraFlashAnimation;
  439. AnimationClip<float> cameraFlashClip;
  440. Animation<float> menuSelectorSlideAnimation;
  441. AnimationClip<float> menuSelectorSlideClip;
  442. Animation<Vector4> menuItemSelectAnimation;
  443. AnimationClip<Vector4> menuItemSelectClip;
  444. Animation<Vector4> menuItemDeselectAnimation;
  445. AnimationClip<Vector4> menuItemDeselectClip;
  446. // Assets
  447. ResourceManager* resourceManager;
  448. Texture2D* splashTexture;
  449. Texture2D* hudSpriteSheetTexture;
  450. TextureAtlas hudTextureAtlas;
  451. Material* smokeMaterial;
  452. Model* lensModel;
  453. Model* forcepsModel;
  454. Model* brushModel;
  455. // Game
  456. CameraRig* cameraRig;
  457. OrbitCam* orbitCam;
  458. FreeCam* freeCam;
  459. Tool* currentTool;
  460. Lens* lens;
  461. Forceps* forceps;
  462. Brush* brush;
  463. // ECS
  464. EntityManager* entityManager;
  465. ComponentManager* componentManager;
  466. SystemManager* systemManager;
  467. SoundSystem* soundSystem;
  468. CollisionSystem* collisionSystem;
  469. CameraSystem* cameraSystem;
  470. RenderSystem* renderSystem;
  471. ToolSystem* toolSystem;
  472. BehaviorSystem* behaviorSystem;
  473. SteeringSystem* steeringSystem;
  474. LocomotionSystem* locomotionSystem;
  475. ParticleSystem* particleSystem;
  476. TerrainSystem* terrainSystem;
  477. bool screenshotQueued;
  478. bool paused;
  479. // Settings
  480. std::string language;
  481. Vector2 windowedResolution;
  482. Vector2 fullscreenResolution;
  483. bool fullscreen;
  484. bool vsync;
  485. float fontSizePT;
  486. std::string controlProfileName;
  487. bool toggleFullscreenDisabled;
  488. // Debugging
  489. Logger* logger;
  490. std::ofstream logFileStream;
  491. CommandInterpreter* cli;
  492. private:
  493. static void saveScreenshot(const std::string& filename, unsigned int width, unsigned int height, unsigned char* pixels);
  494. };
  495. inline const EventDispatcher* Game::getEventDispatcher() const
  496. {
  497. return &eventDispatcher;
  498. }
  499. inline EventDispatcher* Game::getEventDispatcher()
  500. {
  501. return &eventDispatcher;
  502. }
  503. inline const Animator* Game::getAnimator() const
  504. {
  505. return &animator;
  506. }
  507. inline Animator* Game::getAnimator()
  508. {
  509. return &animator;
  510. }
  511. inline std::size_t Game::getLanguageCount() const
  512. {
  513. return languageCount;
  514. }
  515. inline std::size_t Game::getCurrentLanguageIndex() const
  516. {
  517. return currentLanguageIndex;
  518. }
  519. #endif // GAME_HPP