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

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