EntityManager.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgreSingleton.h>
  17. #include "Background2D.h"
  18. #include "Entity.h"
  19. #include "EntityPoint.h"
  20. #include "EntityTrigger.h"
  21. #include "Event.h"
  22. #include "Walkmesh.h"
  23. /**
  24. * The entity manager.
  25. */
  26. class EntityManager : public Ogre::Singleton<EntityManager>{
  27. public:
  28. /**
  29. * Constructor.
  30. */
  31. EntityManager();
  32. /**
  33. * Destructor.
  34. */
  35. virtual ~EntityManager();
  36. /**
  37. * Handles an input event.
  38. *
  39. * @param[in] event Event to handle.
  40. */
  41. void Input(const VGears::Event& event);
  42. /**
  43. * Updates the entities in the manager.
  44. */
  45. void Update();
  46. /**
  47. * Updates the entities in the manager with debug information.
  48. *
  49. * It's automatically called from {@see Update}.
  50. */
  51. void UpdateDebug();
  52. /**
  53. * Handles resizing events
  54. */
  55. void OnResize();
  56. /**
  57. * Clears the entity manager.
  58. */
  59. void Clear();
  60. /**
  61. * Pauses or resumes an entity scripts.
  62. *
  63. * @param[in] paused True to pause, false to resume.
  64. * @todo Verify the description.
  65. */
  66. void ScriptSetPaused(const bool paused);
  67. /**
  68. * Retrieves the walkmesh.
  69. *
  70. * @return The walkmesh.
  71. */
  72. Walkmesh* GetWalkmesh();
  73. /**
  74. * Retrieves the 2D background.
  75. *
  76. * @return The 2D background.
  77. */
  78. Background2D* GetBackground2D();
  79. /**
  80. * Adds an entity to the manager.
  81. *
  82. * @param[in] name Entity name.
  83. * @param[in] file_name Path to the entity model file.
  84. * @param[in] position Entity position in the map.
  85. * @param[in] direction Entity face direction.
  86. * @param[in] index Index of the entity on the map.
  87. */
  88. void AddEntity(
  89. const Ogre::String& name, const Ogre::String& file_name,
  90. const Ogre::Vector3& position, const Ogre::Degree& direction, int index
  91. );
  92. /**
  93. * Adds an entity to the manager.
  94. *
  95. * @param[in] name Entity name.
  96. * @param[in] file_name Path to the entity model file.
  97. * @param[in] position Entity position in the map.
  98. * @param[in] rotation Entity face direction.
  99. * @param[in] scale Entity scale.
  100. * @param[in] root_orientation Map orientation.
  101. * @param[in] index Index of the entity on the map.
  102. */
  103. void AddEntity(
  104. const Ogre::String& name, const Ogre::String& file_name,
  105. const Ogre::Vector3& position, const Ogre::Degree& rotation,
  106. const Ogre::Vector3& scale, const Ogre::Quaternion& root_orientation, int index
  107. );
  108. /**
  109. * Adds an entity to the manager.
  110. *
  111. * @param[in] name Entity name.
  112. * @param[in] file_name Path to the entity model file.
  113. * @param[in] x X coordinate of the entity position in the map.
  114. * @param[in] y Y coordinate of the entity position in the map.
  115. * @param[in] z Z coordinate of the entity position in the map.
  116. * @param[in] direction Entity face direction.
  117. * @param[in] index Index of the entity on the map.
  118. */
  119. void ScriptAddEntity(
  120. const char* name, const char* file_name,
  121. const float x, const float y, const float z, const float direction, int index
  122. );
  123. /**
  124. * Adds an entity trigger to the manager.
  125. *
  126. * A trigger is a line that does something when approached or crossed.
  127. *
  128. * @param[in] name Entity trigger name.
  129. * @param[in] point1 One point of the trigger line.
  130. * @param[in] point2 One point of the trigger line.
  131. * @param[in] enabled True to enable the trigger, false to leave it
  132. * disabled.
  133. */
  134. void AddEntityTrigger(
  135. const Ogre::String& name, const Ogre::Vector3& point1,
  136. const Ogre::Vector3& point2, const bool enabled
  137. );
  138. /**
  139. * Adds an entity point to the manager.
  140. *
  141. * @param[in] name Entity point name.
  142. * @param[in] position Entity point position.
  143. * @param[in] rotation The point orientation.
  144. */
  145. void AddEntityPoint(
  146. const Ogre::String& name, const Ogre::Vector3& position, const float rotation
  147. );
  148. /**
  149. * Adds an entity script to the manager.
  150. *
  151. * @param[in] name Entity script name.
  152. */
  153. void AddEntityScript(const Ogre::String& name);
  154. /**
  155. * Adds an entity script to the manager.
  156. *
  157. * @param[in] name Entity script name.
  158. */
  159. void ScriptAddEntityScript(const char* name);
  160. /**
  161. * Retrieves an entity by name.
  162. *
  163. * @param[in] name Name of the entity to retrieve.
  164. * @return The entity by the specified name, or nullptr if there is no one.
  165. */
  166. Entity* GetEntity(const Ogre::String& name) const;
  167. /**
  168. * Retrieves an entity by it's index in the field.
  169. *
  170. * @param[in] id Index of the entity to retrieve.
  171. * @return The entity with the ID, or nullptr if there is no one.
  172. */
  173. Entity* GetEntityFromIndex(const int id) const;
  174. /**
  175. * Retrieves an entity by it's assigned character ID.
  176. *
  177. * @param[in] id Character ID of the entity to retrieve.
  178. * @return The entity assigned to the character, or nullptr if there is no one.
  179. */
  180. Entity* GetEntityFromCharacterId(const int id) const;
  181. /**
  182. * Retrieves an entity by name.
  183. *
  184. * @param[in] name Name of the entity to retrieve.
  185. * @return The entity by the specified name, or nullptr if there is no one.
  186. */
  187. Entity* ScriptGetEntity(const char* name) const;
  188. /**
  189. * Retrieves an entity point by name.
  190. *
  191. * @param[in] name Name of the entity point to retrieve.
  192. * @return The entity point by the specified name, or nullptr if there is no one.
  193. */
  194. EntityPoint* ScriptGetEntityPoint(const char* name) const;
  195. /**
  196. * Sets the playable entity.
  197. *
  198. * If no entities are found by name, no one will be assigned, the previous playable entity
  199. * will remain so, and no warning will be issued.
  200. *
  201. * @param[in] name Name of the entity to make playable.
  202. */
  203. void ScriptSetPlayerEntity(const char* name);
  204. /**
  205. * Retrieves the playable entity.
  206. *
  207. * @return The playable entity.
  208. */
  209. Entity* ScriptGetPlayerEntity() const;
  210. /**
  211. * Unsets any playable entities.
  212. */
  213. void ScriptUnsetPlayerEntity();
  214. /**
  215. * Locks or unlocks player control of the playable entity.
  216. *
  217. * @param[in] lock True to lock, false to unlock.
  218. */
  219. void ScriptPlayerLock(const bool lock);
  220. /**
  221. * Sets the baseline rotation for the player controlled entity.
  222. *
  223. * @param[in] rotation Baseline rotation.
  224. * @todo Verify this description.
  225. */
  226. void SetPlayerMoveRotation(const Ogre::Radian rotation);
  227. /**
  228. * Checks if random battle encounters are active in the field.
  229. *
  230. * @return True if random battles can occur, false otherwise.
  231. */
  232. bool GetRandomEncounters();
  233. /**
  234. * Enables or disables random encounters in the field.
  235. *
  236. * @param[in] active True to enable encounters, false to deactivate them.
  237. */
  238. void SetRandomEncounters(bool active);
  239. /**
  240. * Get the encounter rate for the field.
  241. *
  242. * @return The encounter rate, between 0 and 1 (both included).
  243. */
  244. float GetEncounterRate();
  245. /**
  246. * Sets the battle encounter rate.
  247. *
  248. * @param[in] rate The encounter rate (0-1).
  249. */
  250. void SetEncounterRate(float rate);
  251. /**
  252. * Starts a battle.
  253. *
  254. * @param[in] formation The enemy formation to fight.
  255. * @return True if the battle victory conditions are met, false otherwise.
  256. * @todo Implement
  257. */
  258. bool StartBattleForResult(unsigned int formation);
  259. /**
  260. * Starts a battle.
  261. *
  262. * @param[in] formation The enemy formation to fight.
  263. * @todo Implement
  264. */
  265. void StartBattle(unsigned int formation);
  266. /**
  267. * Checks if a key is being pressed.
  268. *
  269. * @param[in] key_code The code of the key to test.
  270. * @return True if the key is being pressed, false otherwise.
  271. */
  272. bool IsKeyOn(unsigned int key_code);
  273. /**
  274. * Checks if a key is not being pressed.
  275. *
  276. * @param[in] key_code The code of the key to test.
  277. * @return False if the key is being pressed, true otherwise.
  278. */
  279. bool IsKeyOff(unsigned int key_code);
  280. /**
  281. * Assigns a character to an entity.
  282. *
  283. * @param[in] entity_name The entity name.
  284. * @param[in] char_id The character ID.
  285. */
  286. void SetEntityToCharacter(const char* entity_name, unsigned int char_id);
  287. /**
  288. * Adds a music track ID to the list of music tracks of the field.
  289. *
  290. * @param[in] id ID of the track in the map.
  291. * @param[in] track_id Music track ID.
  292. */
  293. void AddTrack(const int id, const int track_id);
  294. /**
  295. * Adds a music track ID from the list of music tracks of the field.
  296. *
  297. * @param[in] id ID of the track in the map.
  298. * @return The music track ID, or -1 if it doesn't exist.
  299. */
  300. int GetTrack(const int id);
  301. private:
  302. /**
  303. * Calculates the point elevation over a plane.
  304. *
  305. * @param[in] point Point to calculate the elevation of.
  306. * @param[in] a First point of the plane.
  307. * @param[in] b Second point of the plane.
  308. * @param[in] c Third point of the plane.
  309. * @return The elevation of the point.
  310. */
  311. static float PointElevation(
  312. const Ogre::Vector2& point,
  313. const Ogre::Vector3& a, const Ogre::Vector3& b, const Ogre::Vector3& c
  314. );
  315. /**
  316. * Determines at which side of a line a point is.
  317. *
  318. * @param[in] point The point to evaluate.
  319. * @param[in] p1 The first point of the line.
  320. * @param[in] p2 The second point of the line.
  321. * @return A negative value if the point is on the "left" of the line vector, a positive
  322. * value if the point is on the "right" of the vector line, or 0 if the point is part of
  323. * the line. "Left" and "right" are determined by going from p1 to p2.
  324. */
  325. static float SideOfVector(
  326. const Ogre::Vector2& point, const Ogre::Vector2& p1, const Ogre::Vector2& p2
  327. );
  328. /**
  329. * Calculates the square distance between a point and a line.
  330. *
  331. * The calculated distance is the vector orthogonal to the line that passes by the point,
  332. * i.e. the shortest distance.
  333. *
  334. * @param[in] point The point.
  335. * @param[in] point_a First point of the line.
  336. * @param[in] point_b Second point of the line.
  337. * @param[out] proj Vector representing the shortest distance.
  338. * @return The distance from the point to the line.
  339. */
  340. static float SquareDistanceToLine(
  341. const Ogre::Vector3& point, const Ogre::Vector3& point_a, const Ogre::Vector3& point_b,
  342. Ogre::Vector3& proj
  343. );
  344. /**
  345. * Calculates the direction degree between to points.
  346. *
  347. * @param[in] current_point Origin point.
  348. * @param[in] direction_point Next point in the direction.
  349. * @return Degrees between the two points
  350. */
  351. static Ogre::Degree GetDirectionToPoint(
  352. const Ogre::Vector3& current_point, const Ogre::Vector3& direction_point
  353. );
  354. /**
  355. * Attaches an entity to the walkmesh.
  356. *
  357. * It sets the triangle from the entity position coordinates. To account for multiple
  358. * triangles on different levels, it uses only the X and Y coordinates, and automatically
  359. * sets the Z one to the closest triangle.
  360. *
  361. * @param[in] entity Entity to attach.
  362. * @return True if the entity was assigned to a walkmesh triangle, false if the entity is
  363. * not in a triangle.
  364. */
  365. bool SetEntityOnWalkmesh(Entity* entity);
  366. /**
  367. * Moves an entity in the walkmesh.
  368. *
  369. * @param[in] entity Entity to move.
  370. * @param[in] speed Movement speed.
  371. * @return True if the movement was possible and the entity was moved, false otherwise.
  372. */
  373. bool PerformWalkmeshMove(Entity* entity, const float speed);
  374. /**
  375. * Cheks if the entity is crossing a walkmesh triangle border.
  376. *
  377. * @param[in] entity Entity to check.
  378. * @param[in] position The position. @todo document more.
  379. * @param[in] move_vector The move vector. @todo document more.
  380. * @return True if the entity is crossing a triangle border, false otherwise.
  381. */
  382. bool WalkmeshBorderCross(
  383. Entity* entity, Ogre::Vector3& position, const Ogre::Vector2& move_vector
  384. );
  385. /**
  386. * Checks for collisions of the entity with other entities.
  387. *
  388. * @param[in] entity Entity to check for collisions.
  389. * @param[in] position Position of the entity.
  390. * @return True if the entity is colliding with another, false otherwise. If the entity is
  391. * not solid, always false.
  392. */
  393. bool CheckSolidCollisions(Entity* entity, Ogre::Vector3& position);
  394. /**
  395. * Sets an entity direction
  396. *
  397. * @param[in] entity Entity whose direction is to be set.
  398. * @param[in] vector Direction vector.
  399. */
  400. void SetEntityDirectionByVector(Entity* entity, const Ogre::Vector2& vector);
  401. /**
  402. * Checks for entity triggers near a entity.
  403. *
  404. * If there are triggers, and the conditions are met, the appropriate trigger function will
  405. * be added to the queue. This must be tested every time an entity moves. If the entity is
  406. * not the playable character, is not solid or is locked, it will do nothing.
  407. *
  408. * @param[in] entity Entity to check for nearby triggers.
  409. * @param[in] position The position of the entity.
  410. */
  411. void CheckTriggers(Entity* entity, const Ogre::Vector3& position);
  412. /**
  413. * Checks if an entity can be interacted.
  414. *
  415. * It checks if there are entities that can be interacted with from the players current
  416. * position and orientation. If there are, the most appropriate one is selected and, if it
  417. * has an on_interact script, it is run.
  418. */
  419. void CheckEntityInteract();
  420. /**
  421. * Calculates the next offset step for an entity.
  422. *
  423. * @param[in] entity The entity.
  424. */
  425. void SetNextOffsetStep(Entity* entity);
  426. /**
  427. * Calculates the next turn step for an entity
  428. *
  429. * @param[in] entity The entity.
  430. */
  431. void SetNextTurnStep(Entity* entity);
  432. /**
  433. * Calculates and sets the next position during a linear movement.
  434. *
  435. * @param[in] entity The moving entity.
  436. */
  437. void SetNextLinearStep(Entity* entity);
  438. /**
  439. * Calculates and sets the next position during a jump.
  440. *
  441. * @param[in] entity The jump entity.
  442. */
  443. void SetNextJumpStep(Entity* entity);
  444. /**
  445. * @todo Understand and document.
  446. */
  447. void SetNextScrollStep();
  448. /**
  449. * Indicates if the script execution is paused.
  450. */
  451. bool paused_;
  452. /**
  453. * The map walkmesh.
  454. */
  455. Walkmesh walkmesh_;
  456. /**
  457. * The map background
  458. */
  459. Background2D background_2d_;
  460. /**
  461. * The entity table name.
  462. */
  463. Ogre::String entity_table_name_;
  464. /**
  465. * The list of entities.
  466. */
  467. std::vector<Entity*> entity_;
  468. /**
  469. * The player controlled entity.
  470. */
  471. Entity* player_entity_;
  472. /**
  473. * The playable entity current movement indicator.
  474. *
  475. * Applies only for manual movement.
  476. */
  477. Ogre::Vector3 player_move_;
  478. /**
  479. * The playable entity current movement turn indicator.
  480. *
  481. * Applies only for manual movement.
  482. */
  483. Ogre::Radian player_move_rotation_;
  484. /**
  485. * Indicates if player control is locked.
  486. */
  487. bool player_lock_;
  488. /**
  489. * Indicates if the playable is being moved by running.
  490. *
  491. * Applies only for manual movement.
  492. */
  493. bool player_run_;
  494. /**
  495. * List of triggers.
  496. */
  497. std::vector<EntityTrigger*> entity_triggers_;
  498. /**
  499. * List of points.
  500. */
  501. std::vector<EntityPoint*> entity_points_;
  502. /**
  503. * List of scripts.
  504. */
  505. std::vector<Ogre::String> entity_scripts_;
  506. /**
  507. * The scene node.
  508. */
  509. Ogre::SceneNode* scene_node_;
  510. /**
  511. * @todo Understand and document.
  512. */
  513. Ogre::Entity* grid_;
  514. /**
  515. * @todo Understand and document.
  516. */
  517. Ogre::Entity* axis_;
  518. /**
  519. * Indicates if random encounters happen in the map.
  520. */
  521. bool random_encounters_;
  522. /**
  523. * The encounter rate of the map.
  524. */
  525. float encounter_rate_;
  526. /**
  527. * IDs of the music tracks of the field.
  528. */
  529. std::unordered_map<int, int> tracks_;
  530. };