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

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