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

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