Entity.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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 <algorithm>
  17. #include <cmath>
  18. #include <tuple>
  19. #include <OgreString.h>
  20. #include "EntityCollision.h"
  21. #include "EntityDirection.h"
  22. #include "core/ScriptManager.h"
  23. /**
  24. * Action types.
  25. */
  26. enum ActionType{
  27. /**
  28. * No action.
  29. */
  30. AT_NONE,
  31. /**
  32. * Linear action.
  33. *
  34. * It starts and ends at full speed.
  35. */
  36. AT_LINEAR,
  37. /**
  38. * Smooth action.
  39. *
  40. * The action speed steadily increases when started, and it steadily
  41. * decreases before the end.
  42. */
  43. AT_SMOOTH
  44. };
  45. /**
  46. * The direction for an entity turn.
  47. */
  48. enum TurnDirection{
  49. /**
  50. * Turn clockwise.
  51. */
  52. TD_CLOCKWISE,
  53. /**
  54. * Turn anticlockwise.
  55. */
  56. TD_ANTICLOCKWISE,
  57. /**
  58. * Choose direction automatically.
  59. *
  60. * The direction in which the turn is shorter will be selected.
  61. */
  62. TD_CLOSEST
  63. };
  64. /**
  65. * Linear movement modes.
  66. */
  67. enum LinearMovement{
  68. /**
  69. * Move down.
  70. */
  71. LM_UP_TO_DOWN,
  72. /**
  73. * Move up.
  74. */
  75. LM_DOWN_TO_UP,
  76. /**
  77. * Move right.
  78. */
  79. LM_LEFT_TO_RIGHT,
  80. /**
  81. * Move left.
  82. */
  83. LM_RIGHT_TO_LEFT
  84. };
  85. /**
  86. * Any entity in a field.
  87. */
  88. /**
  89. * Arc cosine of a ratio, in radians, that never gives NaN.
  90. *
  91. * The cosine of the angle between two vectors can end up a bit over 1 or under -1 because of float
  92. * rounding (for example when moving exactly along an axis), and acosf gives NaN then. A NaN
  93. * rotation breaks the bounding box of the entity and Ogre asserts. A zero length vector gives 0.
  94. *
  95. * @param[in] dot The dot product of the vectors.
  96. * @param[in] length_product The product of the lengths of the vectors.
  97. */
  98. inline float SafeAcos(const float dot, const float length_product){
  99. if (!(length_product > 0.0f)) return 0.0f;
  100. return std::acos(std::max(-1.0f, std::min(1.0f, dot / length_product)));
  101. }
  102. class Entity{
  103. public:
  104. /**
  105. * Entity animation states.
  106. */
  107. enum AnimationState{
  108. /**
  109. * An animation has been requested.
  110. */
  111. REQUESTED_ANIMATION,
  112. /**
  113. * An animation is set to play automatically.
  114. */
  115. AUTO_ANIMATION
  116. };
  117. /**
  118. * Types of animations.
  119. */
  120. enum AnimationPlayType{
  121. /**
  122. * Default animation mode.
  123. *
  124. * @todo Same as PLAY_ONCE?
  125. */
  126. PLAY_DEFAULT,
  127. /**
  128. * Play the animation once, then stop.
  129. */
  130. PLAY_ONCE,
  131. /**
  132. * Play an animation in a continous loop.
  133. */
  134. PLAY_LOOPED
  135. };
  136. /**
  137. * Entity state.
  138. */
  139. enum State{
  140. /**
  141. * No state.
  142. *
  143. * The entity has not been placed or it has been removed.
  144. */
  145. NONE,
  146. /**
  147. * The entity is attached to the walkmesh.
  148. */
  149. WALKMESH,
  150. /**
  151. * The entity is in the middle of a linear movement.
  152. */
  153. LINEAR,
  154. /**
  155. * The entity is in the middle of a jump.
  156. */
  157. JUMP,
  158. /**
  159. * The entity has completed an action and needs to be reattached
  160. * to the walkmesh before continuing execution.
  161. */
  162. NEEDS_TO_REATTACH
  163. };
  164. /**
  165. * Constructor.
  166. *
  167. * @param[in] name Entity name.
  168. * @param[in] node Scene node to which the entity should be attached.
  169. */
  170. Entity(const Ogre::String& name, Ogre::SceneNode* node);
  171. /**
  172. * Destructor.
  173. */
  174. virtual ~Entity();
  175. /**
  176. * Updates the entity status.
  177. */
  178. virtual void Update();
  179. /**
  180. * Updates the entity status with debug information.
  181. */
  182. virtual void UpdateDebug();
  183. /**
  184. * Retrieves the entity name.
  185. *
  186. * @return The entity name.
  187. */
  188. const Ogre::String& GetName() const;
  189. /**
  190. * Sets the entity position.
  191. *
  192. * @param[in] position Entity's new position.
  193. */
  194. void SetPosition(const Ogre::Vector3& position);
  195. /**
  196. * Sets the entity position.
  197. *
  198. * It also resets the walkmesh triangle to reattach entity to walkmesh
  199. * again if needed.
  200. *
  201. * @param[in] x Entity's new position X coordinate.
  202. * @param[in] y Entity's new position Y coordinate.
  203. * @param[in] z Entity's new position Z coordinate.
  204. */
  205. void ScriptSetPosition(const float x, const float y, const float z);
  206. /**
  207. * Sets the X and Y position from a script, keeping Z and the visibility, solidity and
  208. * talkability of the entity. The walkmesh triangle is looked up again on the next move.
  209. */
  210. void ScriptSetPositionXY(const float x, const float y);
  211. /**
  212. * Moves the entity by the specified position offset.
  213. *
  214. * @param[in] x X coordinate offset.
  215. * @param[in] y Y coordinate offset.
  216. * @param[in] z Z coordinate offset.
  217. */
  218. void ScriptMoveBy(const float x, const float y, const float z);
  219. /**
  220. * Retrieves the entity position.
  221. *
  222. * @return The entity position.
  223. */
  224. const Ogre::Vector3 GetPosition() const;
  225. /**
  226. * Retrieves the entity position.
  227. *
  228. * @return The X, Y and Z coordinates for Lua scripts.
  229. */
  230. std::tuple<float, float, float> ScriptGetPosition() const;
  231. /**
  232. * Sets the entity position.
  233. *
  234. * @param[in] position The entity new position.
  235. * @todo Offset to what?
  236. */
  237. void SetOffset(const Ogre::Vector3& position);
  238. /**
  239. * Retrieves the entity position.
  240. *
  241. * @return The entity position.
  242. * @todo Offset to what?
  243. */
  244. const Ogre::Vector3 GetOffset() const;
  245. /**
  246. * Sets the entity rotation.
  247. *
  248. * @param[in] rotation The entity rotation, in degrees.
  249. */
  250. void SetRotation(const Ogre::Degree& rotation);
  251. /**
  252. * Sets the entity rotation.
  253. *
  254. * @param[in] rotation The entity rotation (0-360).
  255. */
  256. void ScriptSetRotation(const float rotation);
  257. /**
  258. * Retrieves the entity rotation.
  259. *
  260. * @return The entity rotation, in degrees.
  261. */
  262. Ogre::Degree GetRotation() const;
  263. /**
  264. * Retrieves the entity rotation.
  265. *
  266. * @return The entity rotation (0-360).
  267. */
  268. float ScriptGetRotation() const;
  269. /**
  270. * Sets the entity scale.
  271. *
  272. * @param[in] scale Three dimensional scale.
  273. */
  274. virtual void setScale(const Ogre::Vector3 &scale);
  275. /**
  276. * Sets the entity scale from a script.
  277. *
  278. * @param[in] x Scale on the X axis.
  279. * @param[in] y Scale on the Y axis.
  280. * @param[in] z Scale on the Z axis.
  281. */
  282. void ScriptSetScale(const float x, const float y, const float z);
  283. /**
  284. * Rotates the model of the entity relative to the entity itself, for example to tilt the
  285. * mesh of a visual effect. It doesn't change the direction the entity faces.
  286. *
  287. * @param[in] x Rotation around the X axis of the model, in degrees.
  288. * @param[in] y Rotation around the Y axis of the model, in degrees.
  289. * @param[in] z Rotation around the Z axis of the model, in degrees.
  290. */
  291. void ScriptSetModelRotation(const float x, const float y, const float z);
  292. /**
  293. * Blend modes for entities used as visual effects.
  294. */
  295. enum EffectBlend{
  296. /** Normal drawing. */
  297. BLEND_NORMAL = 0,
  298. /** Colours are added to the ones behind, for fire, light and so on. */
  299. BLEND_ADDITIVE = 1,
  300. /** Standard transparency. */
  301. BLEND_ALPHA = 2,
  302. };
  303. /**
  304. * Sets how the entity is blended with the scene. Meant for visual effects.
  305. *
  306. * @param[in] blend The blend mode, one of {@see EffectBlend}.
  307. */
  308. virtual void ScriptSetBlend(const int blend){}
  309. /**
  310. * Sets the entity opacity. Needs a blend mode other than normal. For additive blend it
  311. * scales the brightness.
  312. *
  313. * @param[in] alpha Opacity between 0 and 1.
  314. */
  315. virtual void ScriptSetAlpha(const float alpha){}
  316. /**
  317. * Puts an image on the model, for the effect meshes that have no texture in the files because
  318. * the code of the original effect chooses it. Call it before {@see ScriptSetAlpha}.
  319. *
  320. * @param[in] texture Name of the image, a resource of the battle models or of the images.
  321. */
  322. virtual void ScriptSetTexture(const char* texture){}
  323. /**
  324. * Gets the position in the scene of a bone of the skeleton of the model, for the effects that
  325. * start from a part of a creature. Bones are called bone0, bone1...
  326. *
  327. * @param[in] bone Number of the bone.
  328. * @return X, Y and Z in the scene (0, 0, 0 if there is no such bone).
  329. */
  330. virtual std::tuple<float, float, float> ScriptGetBonePosition(const int bone){
  331. return std::make_tuple(0.0f, 0.0f, 0.0f);
  332. }
  333. /**
  334. * Gets the position in the scene of the end of a bone of the skeleton of the entity.
  335. *
  336. * @param[in] bone Number of the bone.
  337. * @param[in] length Length of the bone, in the units of the model.
  338. * @return The position, all zeros if there is no such bone.
  339. */
  340. virtual std::tuple<float, float, float> ScriptGetBoneEnd(const int bone, const float length){
  341. return std::make_tuple(0.0f, 0.0f, 0.0f);
  342. }
  343. /**
  344. * Attaches another model to a bone of this entity's skeleton, like the weapon a battle character
  345. * holds. A previously attached model is replaced.
  346. *
  347. * @param[in] model Mesh file of the model to attach, a resource of the same group as the entity.
  348. * An empty name only removes the attached model.
  349. * @param[in] bone Number of the bone to attach the model to (-1 for the last bone of the skeleton).
  350. * @param[in] rx Extra rotation of the model around its own X axis, in degrees.
  351. * @param[in] ry Extra rotation around its Y axis, in degrees.
  352. * @param[in] rz Extra rotation around its Z axis, in degrees.
  353. * @return True if the model was attached or removed.
  354. */
  355. virtual bool ScriptAttachModel(
  356. const char* model, const int bone, const float rx, const float ry, const float rz
  357. ){return false;}
  358. /**
  359. * Frees the resources that were created for the entity, before it is removed.
  360. */
  361. virtual void ReleaseResources(){}
  362. /**
  363. * Sets the entity index in the field.
  364. *
  365. * @param[in] index Index of the entity.
  366. */
  367. void SetIndex(const int index);
  368. /**
  369. * Retrieves the entity index in the field.
  370. *
  371. * @return Index of the entity.
  372. */
  373. int GetIndex();
  374. /**
  375. * Sets the entity's absolute orientation.
  376. *
  377. * @param[in] root_orientation The entity's new orientation.
  378. */
  379. virtual void setRootOrientation(
  380. const Ogre::Quaternion &root_orientation
  381. );
  382. /**
  383. * Retrieves the entity's height.
  384. *
  385. * @return The entity's height.
  386. */
  387. float GetHeight() const;
  388. /**
  389. * Sets the entity collision height and updates its debug proxies.
  390. *
  391. * @param[in] height Height in map units.
  392. */
  393. void SetHeight(const float height);
  394. /**
  395. * Sets the orientation of the debug collision proxies.
  396. *
  397. * @param[in] orientation Proxy orientation relative to the entity.
  398. */
  399. void SetCollisionDebugOrientation(const Ogre::Quaternion& orientation);
  400. /**
  401. * Sets the entity's solid radius.
  402. *
  403. * The solid radius is used to detect collisions.
  404. *
  405. * @param[in] radius The solid radius.
  406. * @todo Is it in pixels?
  407. */
  408. void SetSolidRadius(const float radius);
  409. /**
  410. * Retrieves the entity's solid radius.
  411. *
  412. * The solid radius is used to detect collisions.
  413. *
  414. * @return The solid radius.
  415. * @todo Is it in pixels?
  416. */
  417. float GetSolidRadius() const;
  418. /**
  419. * Makes the entity solid or non-solid.
  420. *
  421. * Solid entities can produce collisions.
  422. *
  423. * @param[in] solid True to make the entity solid, false otherwise.
  424. */
  425. void SetSolid(const bool solid);
  426. /**
  427. * Checks if the entity is solid.
  428. *
  429. * Solid entities can produce collisions.
  430. *
  431. * @return True if the entity is solid, false otherwise.
  432. */
  433. bool IsSolid() const;
  434. /**
  435. * Sets the entity talk radius.
  436. *
  437. * The talk radius is the maximum distance at which an entity can be
  438. * talked to or interacted with.
  439. *
  440. * @param[in] radius The talk radius.
  441. * @todo Is it in pixels?
  442. */
  443. void SetTalkRadius(const float radius);
  444. /**
  445. * Retrieves the entity talk radius.
  446. *
  447. * The talk radius is the maximum distance at which an entity can be
  448. * talked to or interacted with.
  449. *
  450. * @return The talk radius.
  451. * @todo Is it in pixels?
  452. */
  453. float GetTalkRadius() const;
  454. /**
  455. * Sets an entity as talkable or non-talkable.
  456. *
  457. * Talkable units can be talked to or interacted with.
  458. *
  459. * @param[in] talkable True to make the entity talkable, false to make
  460. * it non-talkable.
  461. */
  462. void SetTalkable(const bool talkable);
  463. /**
  464. * Checks if an entity is talkable or non-talkable.
  465. *
  466. * Talkable units can be talked to or interacted with.
  467. *
  468. * @return True if the entity is talkable, false if it's not.
  469. */
  470. bool IsTalkable() const;
  471. /**
  472. * Makes the entity visible or invisible.
  473. *
  474. * Invisible entities can't be interacted with.
  475. *
  476. * @param[in] visible True to make the unit visible, false to make it
  477. * invisible.
  478. */
  479. virtual void SetVisible(const bool visible) = 0;
  480. /**
  481. * Checks if the entity is visible or invisible.
  482. *
  483. * Invisible entities can't be interacted with.
  484. *
  485. * @return True if the unit is visible, false if it's invisible.
  486. */
  487. virtual bool IsVisible() const = 0;
  488. /**
  489. * Retrieves the duration of an animation.
  490. *
  491. * @param[in] animation Animation name.
  492. * @return The animation length in seconds, 0 if the animation doesn't exist or the entity
  493. * has no animations.
  494. */
  495. virtual float GetAnimationLength(const Ogre::String& animation) const;
  496. /**
  497. * Retrieves the number of animations of the entity.
  498. *
  499. * @return The number of animations, 0 if the entity has no animations.
  500. */
  501. virtual unsigned int GetAnimationCount() const;
  502. /**
  503. * Gets the name of an animation by its position, in the order they were added to the model.
  504. *
  505. * @param[in] index Position of the animation, starting at 0.
  506. * @return The name, empty if there is no such animation.
  507. */
  508. virtual Ogre::String GetAnimationName(const unsigned int index) const;
  509. /**
  510. * Plays, looped, the animation at a position (see GetAnimationName).
  511. *
  512. * @param[in] index Position of the animation, starting at 0.
  513. */
  514. void ScriptPlayAnimationIndex(const unsigned int index);
  515. /**
  516. * Sets the default animation to the one at a position (see GetAnimationName).
  517. *
  518. * @param[in] index Position of the animation, starting at 0.
  519. */
  520. void ScriptSetDefaultAnimationIndex(const unsigned int index);
  521. /**
  522. * Retrieves the duration of an animation.
  523. *
  524. * To be called from Lua scripts.
  525. *
  526. * @param[in] animation Animation name.
  527. * @return The animation length in seconds, 0 if it doesn't exist.
  528. */
  529. float ScriptGetAnimationLength(const char* animation) const;
  530. /**
  531. * Sets the entity's state.
  532. *
  533. * @param[in] state The entity's state.
  534. */
  535. void SetState(const State state);
  536. /**
  537. * Retrieves the entity's state.
  538. *
  539. * @return The entity's state.
  540. */
  541. State GetState() const;
  542. /**
  543. * Sets the entity's automatic movement speed.
  544. *
  545. * @param[in] speed Automatic movement speed.
  546. * @todo Describe where this speed is used, and max and mins or
  547. * references.
  548. */
  549. void SetMoveAutoSpeed(const float speed);
  550. /**
  551. * Retrieves the entity's automatic movement speed.
  552. *
  553. * @return Automatic movement speed.
  554. * @todo Describe where this speed is used, and max and mins or
  555. * references.
  556. */
  557. float GetMoveAutoSpeed() const;
  558. /**
  559. * Retrieves the entity's walking speed.
  560. *
  561. * @return Walking speed.
  562. * @todo Describe where this speed is used, and max and mins or
  563. * references.
  564. */
  565. float GetMoveWalkSpeed() const;
  566. /**
  567. * Retrieves the entity's running speed.
  568. *
  569. * @return Running speed.
  570. * @todo Describe where this speed is used, and max and mins or
  571. * references.
  572. */
  573. float GetMoveRunSpeed() const;
  574. /**
  575. * Sets the entity's movement destination position.
  576. *
  577. * @param[in] target The destination position.
  578. */
  579. void SetMovePosition(const Ogre::Vector3& target);
  580. /**
  581. * Retrieves the entity's movement destination position.
  582. *
  583. * @return The destination position.
  584. */
  585. const Ogre::Vector3& GetMovePosition() const;
  586. /**
  587. * Retrieves the distance to destination.
  588. *
  589. * It's the distance between the entity's current position and it's
  590. * current movement destination point.
  591. *
  592. * @return The distance to destination.
  593. * @todo Verify this description. Also, indicate units (pixels?).
  594. */
  595. float GetMoveStopDistance() const;
  596. /**
  597. * Checks whether the current move was requested by a script.
  598. *
  599. * @return True for scripted movement.
  600. */
  601. bool IsScriptedMove() const;
  602. /**
  603. * Sets the destination triangle in the walkmesh.
  604. *
  605. * Sets the units destination to one of the walkmesh triangles. The
  606. * movement will stop once the unit enters the triangle.
  607. *
  608. * @param[in] triangle Destination triangle in the walkmesh.
  609. */
  610. void SetMoveTriangleId(const int triangle);
  611. /**
  612. * Sets the destination triangle in the walkmesh.
  613. *
  614. * @return Destination triangle in the walkmesh.
  615. */
  616. int GetMoveTriangleId() const;
  617. /**
  618. * Enables or disables autorotation while the entity is moving.
  619. *
  620. * @param[in] rotate If true, the entity will rotate automatically
  621. * while moving. If false, the entity will not rotate.
  622. */
  623. void SetMoveAutoRotation(const bool rotate);
  624. /**
  625. * Checks if the unit can autorotate while it's moving.
  626. *
  627. * @return If true, the entity will rotate automatically while moving.
  628. * If false, the entity will not rotate.
  629. */
  630. bool GetMoveAutoRotation() const;
  631. /**
  632. * Enables or disables autoanimation while the entity is moving.
  633. *
  634. * @param[in] animate If true, the entity will animate automatically
  635. * while moving. If false, the entity will not animate.
  636. */
  637. void SetMoveAutoAnimation(const bool animate);
  638. /**
  639. * Checks if the unit can autoanimate while it's moving.
  640. *
  641. * @return If true, the entity will animate automatically while moving.
  642. * If false, the entity will not animate.
  643. */
  644. bool GetMoveAutoAnimation() const;
  645. /**
  646. * Retrieves the entity's walk animation name.
  647. *
  648. * @return The walk animation name.
  649. */
  650. const Ogre::String& GetMoveAnimationWalkName() const;
  651. /**
  652. * Sets the animations played when the entity moves on its own.
  653. *
  654. * @param[in] walk Name of the walk animation.
  655. * @param[in] run Name of the run animation.
  656. */
  657. void SetMoveAnimations(const char* walk, const char* run);
  658. /**
  659. * Retrieves the entity's run animation name.
  660. *
  661. * @return The run animation name.
  662. */
  663. const Ogre::String& GetMoveAnimationRunName() const;
  664. /**
  665. * Makes the entity move to a point in the map.
  666. *
  667. * @param[in] x X coordinate of the destination point.
  668. * @param[in] y Y coordinate of the destination point.
  669. */
  670. void ScriptMoveToPosition(const float x, const float y);
  671. /**
  672. * Makes the unit move towards another in the map.
  673. *
  674. * @param[in] entity entity to move towards.
  675. */
  676. void ScriptMoveToEntity(Entity* entity);
  677. /**
  678. * Waits for entity's movement to end.
  679. *
  680. * @return Always -1.
  681. */
  682. int ScriptMoveSync();
  683. /**
  684. * Waits for the jump to finish.
  685. *
  686. * @return Always -1.
  687. */
  688. int ScriptJumpSync();
  689. /**
  690. * Cancels the entity's current movement.
  691. *
  692. * It also clears the movement sync queue.
  693. */
  694. void UnsetMove();
  695. /**
  696. * Linearly moves the entity.
  697. *
  698. * @param[in] x X coordinate of the destination point.
  699. * @param[in] y Y coordinate of the destination point.
  700. * @param[in] z Z coordinate of the destination point.
  701. * @param[in] movement Movement direction.
  702. * @param[in] animation Movement animation.
  703. * @param[in] orientation Orientation during the animation.
  704. * @param[in] dest_triangle Triangle to place the entity after the
  705. * linear movement.
  706. */
  707. void ScriptLinearToPosition(
  708. const float x, const float y, const float z, const LinearMovement movement,
  709. const char* animation, const float orientation, const int dest_triangle
  710. );
  711. /**
  712. * Adds the unit linear movement to the sync queue.
  713. *
  714. * @return Always -1.
  715. * @todo Properly describe this.
  716. */
  717. int ScriptLinearSync();
  718. /**
  719. * Linearly moves the entity.
  720. *
  721. * @param[in] end Destination point.
  722. * @param[in] movement Movement direction.
  723. * @param[in] animation Movement animation.
  724. * @param[in] orientation Orientation during the animation.
  725. * @param[in] dest_triangle Triangle to place the entity after the
  726. * linear movement.
  727. */
  728. void SetLinear(
  729. const Ogre::Vector3& end, const LinearMovement movement,
  730. const Ogre::String& animation, const float orientation, const int dest_triangle
  731. );
  732. /**
  733. * Cancels the entity's current linear movement.
  734. *
  735. * It also clears the movement sync queue.
  736. */
  737. void UnsetLinear();
  738. /**
  739. * Retrieves the entity's current linear movement.
  740. *
  741. * @return Current linear movement.
  742. */
  743. LinearMovement GetLinearMovement() const;
  744. /**
  745. * Retrieves the starting point of the current linear movement.
  746. *
  747. * @return Starting point.
  748. */
  749. const Ogre::Vector3& GetLinearStart() const;
  750. /**
  751. * Retrieves the ending point of the current linear movement.
  752. *
  753. * @return Ending point.
  754. */
  755. const Ogre::Vector3& GetLinearEnd() const;
  756. /**
  757. * Retrieves the ending triangle of the current linear movement.
  758. *
  759. * @return Ending triangle.
  760. */
  761. const int GetLinearDestTriangle() const;
  762. /**
  763. * Makes the unit jump to a point in the field.
  764. *
  765. * @param[in] x X coordinate of the jump destination point.
  766. * @param[in] y Y coordinate of the jump destination point.
  767. * @param[in] z Maximum height of the jump.
  768. * @param[in] seconds Jump duration.
  769. * @param[in] dest_triangle Triangle to place the entity after the
  770. * jump.
  771. */
  772. void ScriptJumpToPosition(
  773. const float x, const float y, const float z, const float seconds, const int dest_triangle
  774. );
  775. /**
  776. * Makes the unit jump to a point in the field.
  777. *
  778. * @param[in] jump_to The jump destination point.
  779. * @param[in] seconds Jump duration.
  780. * @param[in] dest_triangle Triangle to place the entity after the
  781. * jump.
  782. */
  783. void SetJump(const Ogre::Vector3& jump_to, const float seconds, const int dest_triangle);
  784. /**
  785. * Cancels the entity's current jump.
  786. *
  787. * It also clears the movement sync queue.
  788. */
  789. void UnsetJump();
  790. /**
  791. * Retrieves the starting point of the current jump.
  792. *
  793. * @return Starting point.
  794. */
  795. const Ogre::Vector3& GetJumpStart() const;
  796. /**
  797. * Retrieves the ending point of the current jump.
  798. *
  799. * @return Ending point.
  800. */
  801. const Ogre::Vector3& GetJumpEnd() const;
  802. /**
  803. * Gets the total duration of the jump.
  804. *
  805. * @return Jump total duration, in seconds.
  806. */
  807. float GetJumpSeconds() const;
  808. /**
  809. * Sets the current duration of the jump.
  810. *
  811. * @param[in] seconds Jump current duration, in seconds.
  812. */
  813. void SetJumpCurrentSeconds(const float seconds);
  814. /**
  815. * Gets the current duration of the jump.
  816. *
  817. * @return Jump current duration, in seconds.
  818. */
  819. float GetJumpCurrentSeconds() const;
  820. /**
  821. * Retrieves the ending triangle of the current jump.
  822. *
  823. * @return Ending triangle.
  824. */
  825. const int GetJumpDestTriangle() const;
  826. /**
  827. * @todo Understand and document.
  828. *
  829. * @param[in] x X coordinate of the destination point.
  830. * @param[in] y Y coordinate of the destination point.
  831. * @param[in] z Z coordinate of the destination point.
  832. * @param[in] type Type of action.
  833. * @param[in] seconds Duration of the action, in seconds.
  834. */
  835. void ScriptOffsetToPosition(
  836. const float x, const float y, const float z,
  837. const ActionType type, const float seconds
  838. );
  839. /**
  840. * @todo Understand and document.
  841. *
  842. * @return Always -1.
  843. */
  844. int ScriptOffsetSync();
  845. /**
  846. * @todo Understand and document.
  847. */
  848. void UnsetOffset();
  849. /**
  850. * @todo Understand and document.
  851. *
  852. * @return Starting position.
  853. */
  854. const Ogre::Vector3& GetOffsetPositionStart() const;
  855. /**
  856. * @todo Understand and document.
  857. *
  858. * @return Ending position.
  859. */
  860. const Ogre::Vector3& GetOffsetPositionEnd() const;
  861. /**
  862. * @todo Understand and document.
  863. *
  864. * @return The action type.
  865. */
  866. ActionType GetOffsetType() const;
  867. /**
  868. * @todo Understand and document.
  869. *
  870. * @return Action total duration in seconds.
  871. */
  872. float GetOffsetSeconds() const;
  873. /**
  874. * @todo Understand and document.
  875. *
  876. * @param[in] seconds Action current duration in seconds.
  877. */
  878. void SetOffsetCurrentSeconds(const float seconds);
  879. /**
  880. * @todo Understand and document.
  881. *
  882. * @return Action current duration in seconds.
  883. */
  884. float GetOffsetCurrentSeconds() const;
  885. /**
  886. * Makes the entity turn to a fixed direction.
  887. *
  888. * @param[in] direction Final direction to turn the entity's to.
  889. * @param[in] turn_direction Direction of the turn.
  890. * @param[in] turn_type Turn mode.
  891. * @param[in] seconds Total turn duration, in seconds.
  892. */
  893. void ScriptTurnToDirection(
  894. const float direction, const TurnDirection turn_direction,
  895. const ActionType turn_type, const float seconds
  896. );
  897. /**
  898. * Makes the entity turn towards another entity.
  899. *
  900. * @param[in] entity Entity to turn to.
  901. * @param[in] turn_direction Direction of the turn.
  902. * @param[in] seconds Total turn duration, in seconds.
  903. */
  904. void ScriptTurnToEntity(
  905. Entity* entity, const TurnDirection turn_direction,
  906. const float seconds
  907. );
  908. /**
  909. * Makes the entity face another entity at once.
  910. *
  911. * @param[in] entity Entity to face.
  912. */
  913. void ScriptFaceEntity(Entity* entity);
  914. /**
  915. * Adds the entity's turn to the sync queue.
  916. *
  917. * @return Always -1.
  918. * @todo Properly describe this.
  919. */
  920. int ScriptTurnSync();
  921. /**
  922. * Makes the entity turn towards a point or another entity.
  923. *
  924. * @param[in] direction_to Final direction to turn the entity's to.
  925. * @param[in] entity Entity to turn to.
  926. * @param[in] turn_direction Direction of the turn.
  927. * @param[in] turn_type Turn mode.
  928. * @param[in] seconds Total turn duration, in seconds.
  929. * @todo What if the point
  930. */
  931. void SetTurn(
  932. const Ogre::Degree& direction_to, Entity* entity, const TurnDirection turn_direction,
  933. const ActionType turn_type, const float seconds
  934. );
  935. /**
  936. * Cancels the entity's current jump.
  937. *
  938. * It also clears the movement sync queue.
  939. */
  940. void UnsetTurn();
  941. /**
  942. * Calculates the turn angle.
  943. *
  944. * If the turn direction is {@see TD_CLOSEST}, the result is the
  945. * smallest angle between the two orientations. Otherwise, is the angle
  946. * in the specified turn direction.
  947. *
  948. * @param[in] start Starting angle.
  949. * @param[in] end Ending angle.
  950. * @return[in] Calculated turn angle.
  951. */
  952. Ogre::Degree CalculateTurnAngle(const Ogre::Degree& start, const Ogre::Degree& end) const;
  953. /**
  954. * Retrieves the turn staring orientation.
  955. *
  956. * @return The turn starting orientation.
  957. */
  958. Ogre::Degree GetTurnDirectionStart() const;
  959. /**
  960. * Retrieves the turn ending orientation.
  961. *
  962. * @return The turn ending orientation.
  963. */
  964. Ogre::Degree GetTurnDirectionEnd() const;
  965. /**
  966. * Retrieves the turn type.
  967. *
  968. * @return The turn type.
  969. */
  970. ActionType GetTurnType() const;
  971. /**
  972. * Retrieves the turn total duration.
  973. *
  974. * @return The turn total duration, in seconds.
  975. */
  976. float GetTurnSeconds() const;
  977. /**
  978. * Sets the turn current duration.
  979. *
  980. * @param[in] seconds The turn current duration, in seconds.
  981. */
  982. void SetTurnCurrentSeconds(const float seconds);
  983. /**
  984. * Retrieves the turn current duration.
  985. *
  986. * @return The turn current duration, in seconds.
  987. */
  988. float GetTurnCurrentSeconds() const;
  989. /**
  990. * Sets the animation speed.
  991. *
  992. * @param[in] speed The animation speed.
  993. * @todo Indicate units, max and mins, or references.
  994. */
  995. void ScriptSetAnimationSpeed(const float speed);
  996. /**
  997. * Retrieves the entity's default animation name.
  998. *
  999. * @return The default animation name.
  1000. */
  1001. const Ogre::String& GetDefaultAnimationName() const;
  1002. /**
  1003. * Retrieves the entity's current animation name.
  1004. *
  1005. * @return The current animation name.
  1006. */
  1007. const Ogre::String& GetCurrentAnimationName() const;
  1008. /**
  1009. * Retrieves the entity's current animation state.
  1010. *
  1011. * @return The current animation state.
  1012. */
  1013. AnimationState GetAnimationState() const;
  1014. /**
  1015. * Plays one of the entity's animations.
  1016. *
  1017. * @param[in] animation Name of the animation to play.
  1018. * @param[in] state The animation initial state.
  1019. * @param[in] play_type The animation play type, to play it once or in
  1020. * a loop.
  1021. * @param[in] start Animation starting point in time, in seconds.
  1022. * @param[in] end Animation ending point in time, in seconds.
  1023. */
  1024. virtual void PlayAnimation(
  1025. const Ogre::String& animation, AnimationState state,
  1026. AnimationPlayType play_type, const float start, const float end
  1027. ) = 0;
  1028. /**
  1029. * Resumes an animation.
  1030. *
  1031. * @param[in] animation Name of the animation to resume.
  1032. */
  1033. virtual void PlayAnimationContinue(const Ogre::String& animation) = 0;
  1034. /**
  1035. * Updates the animation state.
  1036. *
  1037. * @param[in] delta The animation delta.
  1038. */
  1039. virtual void UpdateAnimation(const float delta) = 0;
  1040. /**
  1041. * Plays one of the entity's animations.
  1042. *
  1043. * @param[in] name Name of the animation to play.
  1044. */
  1045. void ScriptPlayAnimation(const char* name);
  1046. /**
  1047. * Stops one of the entity's animations.
  1048. *
  1049. * @param[in] name Name of the animation to stop.
  1050. */
  1051. void ScriptPlayAnimationStop(const char* name);
  1052. /**
  1053. * Plays one of the entity's animations.
  1054. *
  1055. * @param[in] name Name of the animation to play.
  1056. * @param[in] start Animation starting point in time, in seconds.
  1057. * @param[in] end Animation ending point in time, in seconds.
  1058. */
  1059. void ScriptPlayAnimation(const char* name, const float start, const float end);
  1060. /**
  1061. * Stops one of the entity's animations.
  1062. *
  1063. * @param[in] name Name of the animation to stop.
  1064. * @param[in] start Animation starting point in time, in seconds.
  1065. * @param[in] end Animation ending point in time, in seconds.
  1066. */
  1067. void ScriptPlayAnimationStop(const char* name, const float start, const float end);
  1068. /**
  1069. * Sets the default animation of the entity.
  1070. *
  1071. * @param[in] animation Name of the default animation.
  1072. */
  1073. void ScriptSetDefaultAnimation(const char* animation);
  1074. /**
  1075. * Hides the debug information of the entity (see the config variable debug_entity).
  1076. *
  1077. * For entities that are created in large numbers while the game runs, like the meshes of
  1078. * the effects of the battles, so they don't fill the debug text of the screen.
  1079. *
  1080. * @param[in] hidden True to not show any debug information.
  1081. */
  1082. void SetDebugHidden(const bool hidden);
  1083. /**
  1084. * Adds the entity's animation to the sync queue.
  1085. *
  1086. * @return Always -1.
  1087. * @todo Properly describe this.
  1088. */
  1089. int ScriptAnimationSync();
  1090. /**
  1091. * Assigns the entity as a character.
  1092. *
  1093. * Marks the entity as a character, and assigns a character name and ID. If the ID is
  1094. * passed as a negative number, the entity will no longer be considered a character.
  1095. *
  1096. * @param[in] char_id The character name.
  1097. */
  1098. void SetCharacter(int char_id);
  1099. /**
  1100. * Checks if the entity is a character.
  1101. *
  1102. * An entity is not a character until {@see SetCharacter} has been called.
  1103. *
  1104. * @return True if the entity is a character, false otherwise.
  1105. */
  1106. bool IsCharacter();
  1107. /**
  1108. * Retrieves the entity's character ID.
  1109. *
  1110. * @return The character ID, or a negative value if the entity is not a character.
  1111. */
  1112. int GetCharacterId();
  1113. /**
  1114. * Checks if the entity is a line.
  1115. *
  1116. * @return True if the entity is a line, false otherwise.
  1117. */
  1118. bool IsLine();
  1119. protected:
  1120. /**
  1121. * The name of the entity.
  1122. */
  1123. Ogre::String name_;
  1124. /**
  1125. * The scene node the entity is attached to.
  1126. */
  1127. Ogre::SceneNode* scene_node_;
  1128. /**
  1129. * The entity's model.
  1130. */
  1131. Ogre::SceneNode* model_node_;
  1132. /**
  1133. * The entity's root node.
  1134. */
  1135. Ogre::SceneNode* model_root_node_;
  1136. /**
  1137. * The entity's height.
  1138. */
  1139. float height_;
  1140. /**
  1141. * The entity's direction node.
  1142. */
  1143. Ogre::SceneNode* direction_node_;
  1144. /**
  1145. * The entity's direction.
  1146. */
  1147. EntityDirection* direction_;
  1148. /**
  1149. * The entity's collision node.
  1150. */
  1151. Ogre::SceneNode* solid_collision_node_;
  1152. /**
  1153. * The entity's collision.
  1154. */
  1155. EntityCollision* solid_collision_;
  1156. /**
  1157. * The entity's solid radius.
  1158. */
  1159. float solid_radius_;
  1160. /**
  1161. * Indicates it the entity is solid and can handle collisions.
  1162. */
  1163. bool solid_;
  1164. /**
  1165. * The entity's talk collision node.
  1166. */
  1167. Ogre::SceneNode* talk_collision_node_;
  1168. /**
  1169. * The entity's talk collision.
  1170. */
  1171. EntityCollision* talk_collision_;
  1172. /**
  1173. * The radius at which the entity can be interacted with.
  1174. */
  1175. float talk_radius_;
  1176. /**
  1177. * Indicates if the entity can be interacted with.
  1178. */
  1179. bool talkable_;
  1180. /**
  1181. * Entity's movement status.
  1182. */
  1183. State state_;
  1184. /**
  1185. * Entity's movement sync queue.
  1186. */
  1187. std::vector<ScriptId> sync_;
  1188. /**
  1189. * The entity's automatic movement speed.
  1190. */
  1191. float move_auto_speed_;
  1192. /**
  1193. * The entity's walking speed.
  1194. */
  1195. float move_walk_speed_;
  1196. /**
  1197. * The entity's running speed.
  1198. */
  1199. float move_run_speed_;
  1200. /**
  1201. * The entity's movement destination point.
  1202. */
  1203. Ogre::Vector3 move_position_;
  1204. /**
  1205. * The entity's movement destination entity.
  1206. */
  1207. Entity* move_entity_;
  1208. /**
  1209. * Distance between the entity and it's movement destination point.
  1210. */
  1211. float move_stop_distance_;
  1212. bool scripted_move_;
  1213. /**
  1214. * Entity's movement destination triangle ID.
  1215. */
  1216. int move_triangle_id_;
  1217. /**
  1218. * Indicates if the entity can rotate while moving.
  1219. */
  1220. bool move_auto_rotation_;
  1221. /**
  1222. * Indicates if the entity can animate while moving.
  1223. */
  1224. bool move_auto_animation_;
  1225. /**
  1226. * The name of the entity's walk animation.
  1227. */
  1228. Ogre::String move_animation_walk_;
  1229. /**
  1230. * The name of the entity's run animation.
  1231. */
  1232. Ogre::String move_animation_run_;
  1233. /**
  1234. * The entity's linear movement direction.
  1235. */
  1236. LinearMovement linear_movement_;
  1237. /**
  1238. * The linear movement starting point.
  1239. */
  1240. Ogre::Vector3 linear_start_;
  1241. /**
  1242. * The linear movement ending point.
  1243. */
  1244. Ogre::Vector3 linear_end_;
  1245. /**
  1246. * Triangle to set the entity on after a linear movement.
  1247. */
  1248. int linear_dest_triangle_;
  1249. /**
  1250. * The jump starting point.
  1251. */
  1252. Ogre::Vector3 jump_start_;
  1253. /**
  1254. * The jump ending point.
  1255. */
  1256. Ogre::Vector3 jump_end_;
  1257. /**
  1258. * Total jump duration.
  1259. */
  1260. float jump_seconds_;
  1261. /**
  1262. * Current jump duration.
  1263. */
  1264. float jump_current_seconds_;
  1265. /**
  1266. * Used to store solidity status before a jump.
  1267. */
  1268. bool jump_was_solid_;
  1269. /**
  1270. * Triangle to set the entity on after a jump.
  1271. */
  1272. int jump_dest_triangle_;
  1273. /**
  1274. * @todo Understand and document.
  1275. */
  1276. Ogre::Vector3 offset_position_start_;
  1277. /**
  1278. * @todo Understand and document.
  1279. */
  1280. Ogre::Vector3 offset_position_end_;
  1281. /**
  1282. * @todo Understand and document.
  1283. */
  1284. ActionType offset_type_;
  1285. /**
  1286. * @todo Understand and document.
  1287. */
  1288. float offset_seconds_;
  1289. /**
  1290. * @todo Understand and document.
  1291. */
  1292. float offset_current_seconds_;
  1293. /**
  1294. * @todo Understand and document.
  1295. */
  1296. std::vector<ScriptId> offset_sync_;
  1297. /**
  1298. * Turn movement direction.
  1299. */
  1300. TurnDirection turn_direction_;
  1301. /**
  1302. * Turn initial orientation.
  1303. */
  1304. Ogre::Degree turn_direction_start_;
  1305. /**
  1306. * Turn final orientation.
  1307. */
  1308. Ogre::Degree turn_direction_end_;
  1309. /**
  1310. * The turn destination entity.
  1311. */
  1312. Entity* turn_entity_;
  1313. /**
  1314. * The turn type.
  1315. */
  1316. ActionType turn_type_;
  1317. /**
  1318. * Total turn duration.
  1319. */
  1320. float turn_seconds_;
  1321. /**
  1322. * Current turn duration.
  1323. */
  1324. float turn_current_seconds_;
  1325. /**
  1326. * Entity's turning sync queue.
  1327. */
  1328. std::vector<ScriptId> turn_sync_;
  1329. /**
  1330. * The animation speed.
  1331. */
  1332. float animation_speed_;
  1333. /**
  1334. * The entity's current animation name.
  1335. */
  1336. Ogre::String animation_current_name_;
  1337. /**
  1338. * Entity's animation sync queue.
  1339. */
  1340. std::vector<ScriptId> animation_sync_;
  1341. /**
  1342. * The entity's current animation state.
  1343. */
  1344. AnimationState animation_state_;
  1345. /**
  1346. * The entity's current animation type.
  1347. */
  1348. AnimationPlayType animation_play_type_;
  1349. /**
  1350. * The name of the entity's default name.
  1351. */
  1352. Ogre::String animation_default_;
  1353. /**
  1354. * @todo Understand and document.
  1355. */
  1356. float animation_end_time_;
  1357. /**
  1358. * Seconds left that the animation has to wait before it can end. A range of one frame
  1359. * (start and end equal) lasts a frame of the original game (1/30 s), not an update of the
  1360. * engine, so the scripts that play an animation frame by frame are not faster at a high
  1361. * frame rate.
  1362. */
  1363. float animation_hold_ = 0.0f;
  1364. /**
  1365. * Indicates if an automation must be played automatically.
  1366. */
  1367. bool animation_auto_play_;
  1368. private:
  1369. /**
  1370. * Constructor.
  1371. */
  1372. Entity();
  1373. /**
  1374. * Calculates the angular distance to an entity.
  1375. *
  1376. * @param[in] entity Entity to calculate the angular distance to.
  1377. * @return Angular distance to the specified entity.
  1378. */
  1379. Ogre::Degree GetDirectionToEntity(Entity* entity) const;
  1380. /**
  1381. * Indicates if the entity is a playable character.
  1382. */
  1383. bool is_character_;
  1384. /**
  1385. * The ID of the playable character the entity is, if any.
  1386. */
  1387. int character_id_;
  1388. /**
  1389. * Index of the entity on the field.
  1390. */
  1391. int index_;
  1392. /**
  1393. * Indicates if the entity is a line.
  1394. */
  1395. bool is_line_;
  1396. /**
  1397. * Indicates if the debug information of the entity is never shown.
  1398. */
  1399. bool debug_hidden_;
  1400. };