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

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