EntityManager.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 event[in] Event to handle.
  40. */
  41. void Input(const QGears::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 paused[in] 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 name[in] Entity name.
  83. * @param file_name[in] Path to the entity model file.
  84. * @param position[in] Entity position in the map.
  85. * @param direction[in] Entity face direction.
  86. */
  87. void AddEntity(
  88. const Ogre::String& name, const Ogre::String& file_name,
  89. const Ogre::Vector3& position, const Ogre::Degree& direction
  90. );
  91. /**
  92. * Adds an entity to the manager.
  93. *
  94. * @param name[in] Entity name.
  95. * @param file_name[in] Path to the entity model file.
  96. * @param position[in] Entity position in the map.
  97. * @param rotation[in] Entity face direction.
  98. * @param scale[in] Entity scale.
  99. * @param root_orientation[in] Map orientation.
  100. */
  101. void AddEntity(
  102. const Ogre::String& name, const Ogre::String& file_name,
  103. const Ogre::Vector3& position, const Ogre::Degree& rotation,
  104. const Ogre::Vector3& scale, const Ogre::Quaternion& root_orientation
  105. );
  106. /**
  107. * Adds an entity to the manager.
  108. *
  109. * @param name[in] Entity name.
  110. * @param file_name[in] Path to the entity model file.
  111. * @param x[in] X coordinate of the entity position in the map.
  112. * @param y[in] Y coordinate of the entity position in the map.
  113. * @param z[in] Z coordinate of the entity position in the map.
  114. * @param direction[in] Entity face direction.
  115. */
  116. void ScriptAddEntity(
  117. const char* name, const char* file_name,
  118. const float x, const float y, const float z, const float direction
  119. );
  120. /**
  121. * Adds an entity trigger to the manager.
  122. *
  123. * A trigger is a line that does something when approached or crossed.
  124. *
  125. * @param name[in] Entity trigger name.
  126. * @param point1[in] One point of the trigger line.
  127. * @param point2[in] One point of the trigger line.
  128. * @param enabled[in] True to enable the trigger, false to leave it
  129. * disabled.
  130. */
  131. void AddEntityTrigger(
  132. const Ogre::String& name, const Ogre::Vector3& point1,
  133. const Ogre::Vector3& point2, const bool enabled
  134. );
  135. /**
  136. * Adds an entity point to the manager.
  137. *
  138. * @param name[in] Entity point name.
  139. * @param position[in] Entity point position.
  140. * @param rotation[in] The point orientation.
  141. */
  142. void AddEntityPoint(
  143. const Ogre::String& name, const Ogre::Vector3& position,
  144. const float rotation
  145. );
  146. /**
  147. * Adds an entity script to the manager.
  148. *
  149. * @param name[in] Entity script name.
  150. */
  151. void AddEntityScript(const Ogre::String& name);
  152. /**
  153. * Adds an entity script to the manager.
  154. *
  155. * @param name[in] Entity script name.
  156. */
  157. void ScriptAddEntityScript(const char* name);
  158. /**
  159. * Retrieves an entity by name.
  160. *
  161. * @param name[in] Name of the entity to retrieve.
  162. * @return The entity by the specified name, or nullptr if there is no
  163. * one.
  164. */
  165. Entity* GetEntity(const Ogre::String& name) const;
  166. /**
  167. * Retrieves an entity by name.
  168. *
  169. * @param name[in] Name of the entity to retrieve.
  170. * @return The entity by the specified name, or nullptr if there is no
  171. * one.
  172. */
  173. Entity* ScriptGetEntity(const char* name) const;
  174. /**
  175. * Retrieves an entity point by name.
  176. *
  177. * @param name[in] Name of the entity point to retrieve.
  178. * @return The entity point by the specified name, or nullptr if there
  179. * is no one.
  180. */
  181. EntityPoint* ScriptGetEntityPoint(const char* name) const;
  182. /**
  183. * Sets the playable entity.
  184. *
  185. * If no entities are found by name, no one will be assigned, the
  186. * previous playable entity will remain so, and no warning will be
  187. * issued.
  188. *
  189. * @param name[in] Name of the entity to make playable.
  190. */
  191. void ScriptSetPlayerEntity(const char* name);
  192. /**
  193. * Unsets any playable entities.
  194. */
  195. void ScriptUnsetPlayerEntity();
  196. /**
  197. * Locks or unlocks player control of the playable entity.
  198. *
  199. * @param lock[in] True to lock, false to unlock.
  200. */
  201. void ScriptPlayerLock(const bool lock);
  202. /**
  203. * Sets the baseline rotation for the player controlled entity.
  204. *
  205. * @param rotation[in] Baseline rotation.
  206. * @todo Verify this description.
  207. */
  208. void SetPlayerMoveRotation(const Ogre::Radian rotation);
  209. /**
  210. * Checks if random battle encounters are active in the field.
  211. *
  212. * @return True if random battles can occur, false otherwise.
  213. */
  214. bool GetRandomEncounters();
  215. /**
  216. * Enables or disables random encounters in the field.
  217. *
  218. * @param active[in] True to enable encounters, false to deactivate
  219. * them.
  220. */
  221. void SetRandomEncounters(bool active);
  222. /**
  223. * Get the encounter rate for the field.
  224. *
  225. * @return The encounter rate, between 0 and 1 (both included).
  226. */
  227. float GetEncounterRate();
  228. /**
  229. * Sets the battle encounter rate.
  230. *
  231. * @param rate[in] The encounter rate (0-1).
  232. */
  233. void SetEncounterRate(float rate);
  234. /**
  235. * Starts a battle.
  236. *
  237. * @param formation[in] The enemy formation to fight.
  238. * @return True if the battle victory conditions are met, false
  239. * otherwise.
  240. * @todo Implement
  241. */
  242. bool StartBattleForResult(unsigned int formation);
  243. /**
  244. * Starts a battle.
  245. *
  246. * @param formation[in] The enemy formation to fight.
  247. * @todo Implement
  248. */
  249. void StartBattle(unsigned int formation);
  250. private:
  251. /**
  252. * Attaches an entity to the walkmesh.
  253. *
  254. * It sets the triangle from the entity position coordinates.
  255. *
  256. * @param entity[in] Entity to attach.
  257. * @return True if the entity was assigned to a walkmesh triangle,
  258. * false if the entity is not in a triangle.
  259. */
  260. bool SetEntityOnWalkmesh(Entity* entity);
  261. /**
  262. * @todo Understand and document.
  263. *
  264. * @param entity[in] Entity to move.
  265. * @param speed[in] Movement speed.
  266. * @return @todo.
  267. */
  268. bool PerformWalkmeshMove(Entity* entity, const float speed);
  269. /**
  270. * Cheks if the entity is crossing a walkmesh triangle border.
  271. *
  272. * @param entity[in] Entity to check.
  273. * @param position[in] @todo.
  274. * @param move_vector[in] @todo.
  275. * @return True if the entity is crossing a triangle border, false
  276. * otherwise.
  277. */
  278. bool WalkmeshBorderCross(
  279. Entity* entity, Ogre::Vector3& position,
  280. const Ogre::Vector2& move_vector
  281. );
  282. /**
  283. * Checks for collisions of the entity with other entities.
  284. *
  285. * @param entity[in] Entity to check for collisions.
  286. * @param position[in] Position of the entity.
  287. * @return True if the entity is colliding with another, false
  288. * otherwise. If the entity is not solid, always false.
  289. */
  290. bool CheckSolidCollisions(Entity* entity, Ogre::Vector3& position);
  291. /**
  292. * Sets an entity direction
  293. *
  294. * @param entity[in] Entity whose direction is to be set.
  295. * @param vector[in] Direction vector.
  296. */
  297. void SetEntityDirectionByVector(
  298. Entity* entity, const Ogre::Vector2& vector
  299. );
  300. /**
  301. * Checks for entity triggers at a specified location.
  302. *
  303. * @param entity[in] Entity to check for triggers.
  304. * @param position[in] Position of the entity trigger.
  305. * @return True if the entity is colliding with another, false
  306. * otherwise. If the entity is not solid, always false.
  307. */
  308. void CheckTriggers(Entity* entity, Ogre::Vector3& position);
  309. /**
  310. * Checks if an entity can be interacted.
  311. *
  312. * It checks if there are entities that can be interacted with from the
  313. * players current position and orientation. If there are, the most
  314. * appropriate one is selected and, if it has an on_interact script, it
  315. * is run.
  316. */
  317. void CheckEntityInteract();
  318. /**
  319. * @todo Understand and document.
  320. *
  321. * @param entity[in] @todo.
  322. */
  323. void SetNextOffsetStep(Entity* entity);
  324. /**
  325. * @todo Understand and document.
  326. *
  327. * @param entity[in] @todo.
  328. */
  329. void SetNextTurnStep(Entity* entity);
  330. /**
  331. * @todo Understand and document.
  332. *
  333. * @param entity[in] @todo.
  334. */
  335. void SetNextLinearStep(Entity* entity);
  336. /**
  337. * @todo Understand and document.
  338. *
  339. * @param entity[in] @todo.
  340. */
  341. void SetNextJumpStep(Entity* entity);
  342. /**
  343. * @todo Understand and document.
  344. */
  345. void SetNextScrollStep();
  346. /**
  347. * Indicates if the script execution is paused.
  348. */
  349. bool paused_;
  350. /**
  351. * The map walkmesh.
  352. */
  353. Walkmesh walkmesh_;
  354. /**
  355. * The map background
  356. */
  357. Background2D background_2d_;
  358. /**
  359. * The entity table name.
  360. */
  361. Ogre::String entity_table_name_;
  362. /**
  363. * The list of entities.
  364. */
  365. std::vector<Entity*> entity_;
  366. /**
  367. * The player controlled entity.
  368. */
  369. Entity* player_entity_;
  370. /**
  371. * @todo Understand and document.
  372. */
  373. Ogre::Vector3 player_move_;
  374. /**
  375. * @todo Understand and document.
  376. */
  377. Ogre::Radian player_move_rotation_;
  378. /**
  379. * Indicates if player control is locked.
  380. */
  381. bool player_lock_;
  382. /**
  383. * @todo Understand and document.
  384. */
  385. bool player_run_;
  386. /**
  387. * List of triggers.
  388. */
  389. std::vector<EntityTrigger*> entity_triggers_;
  390. /**
  391. * List of points.
  392. */
  393. std::vector<EntityPoint*> entity_points_;
  394. /**
  395. * List of scripts.
  396. */
  397. std::vector<Ogre::String> entity_scripts_;
  398. /**
  399. * The scene node.
  400. */
  401. Ogre::SceneNode* scene_node_;
  402. /**
  403. * @todo Understand and document.
  404. */
  405. Ogre::Entity* grid_;
  406. /**
  407. * @todo Understand and document.
  408. */
  409. Ogre::Entity* axis_;
  410. /**
  411. * Indicates if random encounters happen in the map.
  412. */
  413. bool random_encounters_;
  414. /**
  415. * The encounter rate of the map.
  416. */
  417. float encounter_rate_;
  418. };