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

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