PolyEntity.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyString.h"
  22. #include "PolyMatrix4.h"
  23. #include "PolyVector2.h"
  24. #include "PolyQuaternion.h"
  25. #include "PolyColor.h"
  26. #include "PolyRectangle.h"
  27. #include "PolyRay.h"
  28. #include "PolyEventDispatcher.h"
  29. #include <vector>
  30. namespace Polycode {
  31. class Renderer;
  32. class _PolyExport MouseEventResult {
  33. public:
  34. bool hit;
  35. bool blocked;
  36. };
  37. class _PolyExport EntityProp {
  38. public:
  39. String propName;
  40. String propValue;
  41. };
  42. class _PolyExport AABB {
  43. public:
  44. Vector3 min;
  45. Vector3 max;
  46. };
  47. class _PolyExport Rotation {
  48. public:
  49. Rotation();
  50. Number pitch;
  51. Number yaw;
  52. Number roll;
  53. inline bool operator == ( const Rotation& r2) {
  54. return (r2.pitch == pitch && r2.roll == roll && r2.yaw == yaw);
  55. }
  56. inline bool operator != ( const Rotation& r2) {
  57. return (r2.pitch != pitch || r2.yaw != yaw || r2.roll != roll);
  58. }
  59. };
  60. /**
  61. * Base class for both 2D and 3D objects in Polycode. It provides position and color transformations as well as hierarchy for all Polycode objects.
  62. */
  63. class _PolyExport Entity : public EventDispatcher {
  64. public:
  65. Entity();
  66. Entity(Number width, Number height, Number depth = 0.01);
  67. virtual ~Entity();
  68. void initEntity();
  69. /**
  70. * Main render method. Override this to do your own drawing.
  71. */
  72. virtual void Render(){};
  73. /**
  74. * Main update method. Override this to do your updates before the render cycle.
  75. */
  76. virtual void Update(){};
  77. virtual void fixedUpdate(){};
  78. void transformAndRender();
  79. void renderChildren();
  80. /**
  81. * Clones the entity, return an exact copy. This method must be implemented in an Entity subclass for you to be able to clone it.
  82. * @param deepClone If true, perform a deep clone, cloning all the children.
  83. * @param ignoreEditorOnly If true, ignore all child entities where editorOnly is set to true (will still clone the entity you call Clone() on even if its editorOnly flag is set to true.
  84. * @return The clone of the entity.
  85. */
  86. virtual Entity *Clone(bool deepClone, bool ignoreEditorOnly) const;
  87. /**
  88. * This method must be implemented by all subclasses implementing Clone.
  89. */
  90. virtual void applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const;
  91. // ----------------------------------------------------------------------------------------------------------------
  92. /** @name Matrix operations.
  93. * These methods operate directly on the entity's matrix.
  94. */
  95. //@{
  96. /**
  97. * Sets the state of the dirty matrix flag. The dirty matrix flag gets set automatically when a transformation is applied to the entity and the transfrom matrix needs to be rebuilt. This method is provided to manually override that flag.
  98. @param val New value of the dirty matrix flag.
  99. */
  100. void dirtyMatrix(bool val);
  101. /**
  102. * Forces the transformation matrix to be rebuilt.
  103. */
  104. void rebuildTransformMatrix();
  105. /**
  106. * Forces the matrix to be rebuilt if the matrix flag is dirty. This is also called on all of the entity's children.
  107. */
  108. void updateEntityMatrix();
  109. /**
  110. * Returns the entity's transform matrix.
  111. @return Transform matrix.
  112. */
  113. const Matrix4& getTransformMatrix() const;
  114. /**
  115. * Returns the entity's matrix multiplied by its parent's concatenated matrix. This, in effect, returns the entity's actual world transformation.
  116. @return Entity's concatenated matrix.
  117. */
  118. Matrix4 getConcatenatedMatrix();
  119. /**
  120. * Returns the concatenated matrix up to the specified parent entity.
  121. * @param relativeEntity Parent entity, relative to which to return the transform matrix.
  122. */
  123. Matrix4 getConcatenatedMatrixRelativeTo(Entity *relativeEntity);
  124. /**
  125. * Returns the concatenated matrix, multiplied by the entity's anchor adjustment.
  126. * @see setAnchorPoint
  127. */
  128. Matrix4 getAnchorAdjustedMatrix();
  129. /**
  130. * Returns Same as getConcatenatedMatrix(), but contains only roll information for rotation. Used internally for billboards.
  131. @return Entity's concatenated roll matrix.
  132. @see getConcatenatedMatrix()
  133. */
  134. Matrix4 getConcatenatedRollMatrix() const;
  135. /**
  136. * Sets the transform matrix directly, without setting all of the individual transfrom properties of the entity.
  137. @param matrix 4x4 transform matrix to apply.
  138. */
  139. void setTransformByMatrixPure(const Matrix4& matrix);
  140. /** Returns the matrix for the entity looking at a location based on a location and an up vector.
  141. * @param loc Location to look at.
  142. * @param upVector Up vector.
  143. * @return The resulting lookAt matrix.
  144. */
  145. Matrix4 getLookAtMatrix(const Vector3 &loc, const Vector3 &upVector = Vector3(0,1,0));
  146. //@}
  147. // ----------------------------------------------------------------------------------------------------------------
  148. /** @name Hierarchy operations.
  149. * These methods add and remove entities to and from each other.
  150. */
  151. //@{
  152. /**
  153. * Adds another entity as a child. The children inherit the parent's transforms.
  154. @param newChild The entity to be added.
  155. */
  156. virtual void addChild(Entity *newChild);
  157. /**
  158. * Removes an entity from the entity's children.
  159. @param entityToRemove Entity to be removed.
  160. */
  161. virtual void removeChild(Entity *entityToRemove);
  162. /**
  163. * Moves the specified child one position up the render list.
  164. */
  165. void moveChildUp(Entity *child);
  166. /**
  167. * Moves the specified child one position down the render list.
  168. */
  169. void moveChildDown(Entity *child);
  170. /**
  171. * Moves the specified child up to the top of the render list.
  172. */
  173. void moveChildTop(Entity *child);
  174. /**
  175. * Moves the specified child up to the bottom of the render list.
  176. */
  177. void moveChildBottom(Entity *child);
  178. /**
  179. * Manually sets the entity's parent. This method does not add the entity to the parent and should not be called manually.
  180. @param entity Parent entity.
  181. */
  182. void setParentEntity(Entity *entity);
  183. /**
  184. * Returns the parent entity of the entity.
  185. @return Parent entity of this entity.
  186. */
  187. Entity *getParentEntity() const;
  188. /**
  189. * Returns the number of child entities belonging to this entity.
  190. * @return Number of child entities.
  191. */
  192. unsigned int getNumChildren();
  193. /**
  194. * Returns the child entity at specified index.
  195. * @param index Index to return entity at.
  196. * @return Child entity at specified index or NULL of index out of range.
  197. */
  198. Entity *getChildAtIndex(unsigned int index);
  199. /**
  200. * If set to true, will automatically delete children upon destruction. (defaults to false).
  201. */
  202. bool ownsChildren;
  203. /**
  204. * Sets the ownsChildren flag for this entity and recursively for all its child entities.
  205. * @see ownsChildren
  206. */
  207. void setOwnsChildrenRecursive(bool val);
  208. //@}
  209. // ----------------------------------------------------------------------------------------------------------------
  210. /** @name Transform operations.
  211. * These methods apply various transformations to the entity.
  212. */
  213. //@{
  214. /**
  215. * Returns the entity's position.
  216. @return Entity's position as a vector.
  217. */
  218. Vector3 getPosition() const;
  219. /**
  220. * Returns the entity's position as a Vector2
  221. */
  222. Vector2 getPosition2D() const;
  223. /**
  224. * Returns the entity's position added to the combined position of its parent. This method is here only for convenience of calculating certain properties and should not be used to get an entity's actual position in the world. To get the actual world position of the entity, use the entity's concatendated matrix.
  225. @see getConcatenatedMatrix()
  226. @return Entity's position as a vector.
  227. */
  228. Vector3 getCombinedPosition() const;
  229. /**
  230. * Sets the entity's position.
  231. @param x X-axis value.
  232. @param y Y-axis value.
  233. @param z Z-axis value.
  234. */
  235. void setPosition(Number x, Number y, Number z=0.0);
  236. /**
  237. * Sets the entity's position with a vector.
  238. @param posVec New position as a vector.
  239. */
  240. void setPosition(const Vector3 &posVec);
  241. /**
  242. * Returns the entity's position on the X axis.
  243. @param x X-axis value.
  244. */
  245. void setPositionX(Number x);
  246. /**
  247. * Returns the entity's position on the Y axis.
  248. @param y Y-axis value.
  249. */
  250. void setPositionY(Number y);
  251. /**
  252. * Translates the entity relative to its current position.
  253. @param x X-axis value.
  254. @param y Y-axis value.
  255. @param z Z-axis value.
  256. */
  257. void Translate(Number x, Number y, Number z=0.0);
  258. /**
  259. * Translates the entity relative to its current position with a vector.
  260. @param tVec New position as a vector.
  261. */
  262. void Translate(const Vector3 &tVec);
  263. /**
  264. * Returns the entity's position on the Z axis.
  265. @param z Z-axis value.
  266. */
  267. void setPositionZ(Number z);
  268. /**
  269. * Returns the entity's scale on the X axis.
  270. @param x X-axis scale value.
  271. */
  272. void setScaleX(Number x);
  273. /**
  274. * Returns the entity's scale on the Y axis.
  275. @param y Y-axis scale value.
  276. */
  277. void setScaleY(Number y);
  278. /**
  279. * Returns the entity's scale on the Z axis.
  280. @param z Z-axis scale value.
  281. */
  282. void setScaleZ(Number z);
  283. /**
  284. * Scales the entity relative to its current scale.
  285. @param x X-axis value.
  286. @param y Y-axis value.
  287. @param z Z-axis value.
  288. */
  289. void Scale(Number x, Number y, Number z=0.0);
  290. /**
  291. * Scales the entity relative to its current scale.
  292. @param scale Scale vector.
  293. */
  294. void Scale(const Vector3 &scale);
  295. /**
  296. * Sets the entity's scale.
  297. @param x X-axis value.
  298. @param y Y-axis value.
  299. @param z Z-axis value.
  300. */
  301. void setScale(Number x, Number y, Number z=1.0);
  302. /**
  303. * Sets the entity's scale.
  304. @param v New scale vector.
  305. */
  306. void setScale(const Vector3 &v);
  307. /**
  308. * Returns the entity's scale multiplied by its parent's compound scale.
  309. * @return Compound scale as vector.
  310. */
  311. Vector3 getCompoundScale() const;
  312. /**
  313. * Returns the entity's scale.
  314. @return Entity's scale as a vector.
  315. */
  316. Vector3 getScale() const;
  317. /**
  318. * Returns the entity's rotation as euler angles
  319. @return Entity's rotation as euler angles
  320. */
  321. Vector3 getRotationEuler() const;
  322. /**
  323. * Returns the entity's pitch combined with the combined pitch of its parent.
  324. @return Entity's combined pitch.
  325. */
  326. Number getCombinedPitch() const;
  327. /**
  328. * Returns the entity's yaw combined with the combined yaw of its parent.
  329. @return Entity's combined yaw.
  330. */
  331. Number getCombinedYaw() const;
  332. /**
  333. * Returns the entity's roll combined with the combined roll of its parent.
  334. @return Entity's combined roll.
  335. */
  336. Number getCombinedRoll() const;
  337. /**
  338. * Forces the rotation quaternion to be rebuilt.
  339. */
  340. void rebuildRotation();
  341. /**
  342. * Sets rotation from euler angles
  343. * @param rotation New rotation values
  344. */
  345. void setRotationEuler(const Vector3 &rotation);
  346. /**
  347. * Sets the pitch rotation of the entity.
  348. * @param pitch New pitch value in degrees.
  349. */
  350. void setPitch(Number pitch);
  351. /**
  352. * Sets the yaw rotation of the entity.
  353. * @param yaw New yaw value in degrees.
  354. */
  355. void setYaw(Number yaw);
  356. /**
  357. * Sets the roll rotation of the entity.
  358. * @param roll New roll value in degrees.
  359. */
  360. void setRoll(Number roll);
  361. /**
  362. * Rolls the entity relative to its current roll.
  363. * @param roll Roll value in degrees.
  364. */
  365. void Roll(Number roll);
  366. /**
  367. * Yaws the entity relative to its current yaw.
  368. * @param yaw Yaw value in degrees.
  369. */
  370. void Yaw(Number yaw);
  371. /**
  372. * Pitches the entity relative to its current pitch.
  373. * @param pitch Pitch value in degrees.
  374. */
  375. void Pitch(Number pitch);
  376. /**
  377. * Returns the current pitch of the entity.
  378. * @return Current pitch value.
  379. */
  380. Number getPitch() const;
  381. /**
  382. * Returns the current yaw of the entity.
  383. * @return Current yaw value.
  384. */
  385. Number getYaw() const;
  386. /**
  387. * Returns the current roll of the entity.
  388. * @return Current roll value.
  389. */
  390. Number getRoll() const;
  391. /**
  392. * Returns the bounding box X value.
  393. */
  394. Number getWidth() const;
  395. /**
  396. * Returns the bounding box Y value.
  397. */
  398. Number getHeight() const;
  399. /**
  400. * Returns the bounding box Z value.
  401. */
  402. Number getDepth() const;
  403. /**
  404. * Sets the bounding box X value.
  405. */
  406. void setWidth(Number width);
  407. /**
  408. * Sets the bounding box Y value.
  409. */
  410. void setHeight(Number height);
  411. /**
  412. * Sets the bounding box Z value.
  413. */
  414. void setDepth(Number depth);
  415. /**
  416. * Sets the rotation with quaternion value.
  417. */
  418. void setRotationQuat(Number w, Number x, Number y, Number z);
  419. /*
  420. * Sets the rotation with quaternion value.
  421. */
  422. void setRotationByQuaternion(const Quaternion &quaternion);
  423. /**
  424. * Returns the current rotation as a quaternion.
  425. * @return Current rotation value.
  426. */
  427. Quaternion getRotationQuat() const;
  428. Quaternion getConcatenatedQuat() const;
  429. /**
  430. * Orients the entity towards the specified location with the provided up vector. The up vector determines which side of the entity will be pointing in that direction.
  431. * @param loc Location to look at.
  432. * @param upVector The up vector.
  433. */
  434. void lookAt(const Vector3 &loc, const Vector3 &upVector = Vector3(0,1,0));
  435. /**
  436. * Orients the entity towards another entity with the provided up vector. The up vector determines which side of the entity will be pointing in that direction.
  437. * @param entity Entity to look at.
  438. * @param upVector The up vector.
  439. * @see lookAt()
  440. */
  441. void lookAtEntity(Entity *entity, const Vector3 &upVector = Vector3(0,1,0));
  442. /**
  443. * Returns the entity's color multiplied by its parent entity's combined color.
  444. * @return Entity's combined color.
  445. */
  446. Color getCombinedColor() const;
  447. /**
  448. * Sets the color of the entity as normalized floating point values.
  449. * @param r Red value as a 0-1 floating point number.
  450. * @param g Green value as a 0-1 floating point number.
  451. * @param b Blue value as a 0-1 floating point number.
  452. * @param a Alpha value as a 0-1 floating point number.
  453. */
  454. void setColor(Number r, Number g, Number b, Number a);
  455. /**
  456. * Sets the color of the entity as 0-255 integers.
  457. * @param r Red value as a 0-255 integer.
  458. * @param g Green value as a 0-255 integer.
  459. * @param b Blue value as a 0-255 integer.
  460. * @param a Alpha value as a 0-255 integer.
  461. */
  462. void setColorInt(int r, int g, int b, int a);
  463. /**
  464. * Sets the color of the entity.
  465. * @param color Color to set the entity color to.
  466. */
  467. void setColor(Color color);
  468. //@}
  469. /**
  470. * Sets the anchor (center) point of the entity as normalized half bounding box coordinates. (i.e. -1.0 or 1.0 will offset the entity by half on a particular axis).
  471. * @param anchorPoint Anchor point as a 3D Vector.
  472. */
  473. void setAnchorPoint(const Vector3 &anchorPoint);
  474. /**
  475. * Sets the anchor (center) point of the entity as normalized half bounding box coordinates. (i.e. -1.0 or 1.0 will offset the entity by half on a particular axis).
  476. * @param x X Offset
  477. * @param y Y Offset
  478. * @param z Z Offset
  479. */
  480. void setAnchorPoint(Number x, Number y, Number z);
  481. /**
  482. * Returns the current anchor (center) point of the entity.
  483. */
  484. Vector3 getAnchorPoint() const;
  485. virtual MouseEventResult onMouseDown(const Ray &ray, int mouseButton, int timestamp);
  486. virtual MouseEventResult onMouseUp(const Ray &ray, int mouseButton, int timestamp);
  487. virtual MouseEventResult onMouseMove(const Ray &ray, int timestamp);
  488. virtual MouseEventResult onMouseWheelUp(const Ray &ray, int timestamp);
  489. virtual MouseEventResult onMouseWheelDown(const Ray &ray, int timestamp);
  490. /** @name Rendering properties
  491. * Methods and properties affecting the way the entity is rendered.
  492. */
  493. //@{
  494. /**
  495. * If this flag is true, the entity will always face the camera. False by default.
  496. */
  497. bool billboardMode;
  498. /**
  499. * Normally, if billboardMode is on, no rotation is allowed at all. If this flag is also true, you can rotate the entity around the axis pointing to the camera.
  500. */
  501. bool billboardRoll;
  502. /**
  503. * If set to true, the entity will not be scaled by the modelview
  504. * matrix when billboardMode is enabled
  505. */
  506. bool billboardIgnoreScale;
  507. /**
  508. * The entity's color.
  509. */
  510. Color color;
  511. /**
  512. * If this flag is set to false, this entity will not be rendered or updated.
  513. */
  514. bool enabled;
  515. /**
  516. * If this flag is set to false, this entity will not be rendered.
  517. */
  518. bool visible;
  519. /**
  520. * If this flag is set to false, this entity will not write to the depth buffer when it's rendered.
  521. */
  522. bool depthWrite;
  523. /**
  524. * If this flag is set to false, this entity will not check the depth buffer when it's rendering.
  525. */
  526. bool depthTest;
  527. /**
  528. * Entity blending mode. Possible values are Renderer::BLEND_MODE_NONE, Renderer::BLEND_MODE_NORMAL, Renderer::BLEND_MODE_LIGHTEN, Renderer::BLEND_MODE_COLOR, Renderer::BLEND_MODE_PREMULTIPLIED, Renderer::BLEND_MODE_MULTIPLY. See the Renderer class for details on individual blending modes.
  529. This blending mode is overridden by the material.
  530. */
  531. int blendingMode;
  532. /**
  533. * If set to false, the children of this entity will not multiply by this entity's color. Set to true by default.
  534. */
  535. bool colorAffectsChildren;
  536. /**
  537. * If set to false, the children will be rendered even if the entity is invisible.
  538. */
  539. bool visibilityAffectsChildren;
  540. /**
  541. * If this flag is set to true, this entity will render only into the depth buffer. This, effectively, means that it will be invisible, but still obscuring other entities.
  542. */
  543. bool depthOnly;
  544. /**
  545. * If this flag is set to true, this entity's transformations will not take into account its parent, making its transforms always relative to 0.
  546. */
  547. //@}
  548. // ----------------------------------------------------------------------------------------------------------------
  549. /**
  550. * Sets user data pointer.
  551. * @param userData User data pointer
  552. */
  553. void setUserData(void *userData);
  554. /**
  555. * Returns the user data pointer.
  556. * @return User data pointer
  557. */
  558. void *getUserData() const;
  559. /**
  560. * Sets the entity's blending mode.
  561. * @param newBlendingMode New blending mode to set. Possible values are Renderer::BLEND_MODE_NORMAL, Renderer::BLEND_MODE_LIGHTEN, Renderer::BLEND_MODE_COLOR, Renderer::BLEND_MODE_PREMULTIPLIED, Renderer::BLEND_MODE_MULTIPLY. See the Renderer class for details on individual blending modes.
  562. * @see Renderer
  563. */
  564. void setBlendingMode(int newBlendingMode);
  565. /**
  566. * Returns the first child entity that has the specified string id.
  567. * @param id Specified id to search for.
  568. * @param recursive If set to true, will search all child entities recursively.
  569. * @return Entity with specified string id or NULL if not found.
  570. */
  571. Entity *getEntityById(String id, bool recursive) const;
  572. /**
  573. * Returns all child entities which have the specified tag.
  574. * @param tag Tag to search for.
  575. * @param recursive If set to true, will search all child entities recursively.
  576. * @return List of child entities that contain the specified tag.
  577. */
  578. std::vector<Entity*> getEntitiesByTag(String tag, bool recursive) const;
  579. /**
  580. * Returns all child entities that have the specified layer ID. Layer IDs are used by the entity instances to separate entities into groups.
  581. * @param Layer ID to search for.
  582. * @param recursive If set to true, will search all child entities recursively.
  583. * @return List of child entities that contain the specified layer ID.
  584. */
  585. std::vector<Entity*> getEntitiesByLayerID(unsigned char layerID, bool recursive) const;
  586. /**
  587. * Returns custom string dictionary property of the entity based on the property name.
  588. * @param Property name to look up.
  589. * @return String property for specified property name or "null" if this property doesn't exist.
  590. */
  591. String getEntityProp(const String& propName);
  592. /**
  593. * Sets the entity property for a specified property name in the entity's custom property dictionary.
  594. * @param propName Property name to set.
  595. * @param propValue Value to set for the specified property name.
  596. */
  597. void setEntityProp(const String& propName, const String& propValue);
  598. /**
  599. * If set to true, the y position of the entity matrix will be multiplied by -1.0, inverting its Y-axis coordinate system.
  600. */
  601. void setInverseY(bool val);
  602. /**
  603. * Returns true if the entity is set to use an inverse Y-coordinate system.
  604. */
  605. bool getInverseY();
  606. void doUpdates();
  607. void doFixedUpdates();
  608. virtual Matrix4 buildPositionMatrix();
  609. void setRenderer(Renderer *renderer);
  610. /**
  611. * Implement this method to do custom ray hit detection beyond a bounding box check. Always returns true by default.
  612. */
  613. virtual bool customHitDetection(const Ray &ray) { return true; }
  614. /**
  615. * If set to true, the entity's transformations will not be affected by its parents. Defaults to false.
  616. */
  617. bool ignoreParentMatrix;
  618. /**
  619. * If set to true, will constrain the rendering of this entity into the viewport coordinates defined by scissorBox.
  620. * @see scissorBox
  621. */
  622. bool enableScissor;
  623. /**
  624. * Defines the viewport coordinates to clip rendering to if enableScissor is defined.
  625. * @see enableScissor
  626. */
  627. Polycode::Rectangle scissorBox;
  628. /**
  629. * Flags an editor only entity. If set to true, this entity will not be saved to file by entity instances or show up in the IDE entity editor.
  630. */
  631. bool editorOnly;
  632. /**
  633. * String ID of the entity. Can be used to retrieve specific entities by their ID.
  634. */
  635. String id;
  636. /**
  637. * Returns the number of tags this entity has.
  638. */
  639. unsigned int getNumTags() const;
  640. /**
  641. * Returns the tag at specified index or an empty string if index is invalid.
  642. */
  643. String getTagAtIndex(unsigned int index) const;
  644. /**
  645. * Returns true if this entity contains the specified tag.
  646. * @param tag Tag to look up.
  647. * @return True if this entity contains the specified tag, false if it doesn't.
  648. */
  649. bool hasTag(String tag) const;
  650. /**
  651. * Removes all tags from this entity.
  652. */
  653. void clearTags();
  654. /**
  655. * Adds a string tag to the entity.
  656. * @param tag Tag to add.
  657. */
  658. void addTag(String tag);
  659. /**
  660. * Entity collision type for physics module. This is set per physics module documentaiton.
  661. */
  662. unsigned char collisionShapeType;
  663. /**
  664. * If set to true, will automatically process mouse events and dispatch its own input events if mouse events intersect with the entity's bounding box. Defaults to false.
  665. Attention: All of the entity's parents' processInputEvents flags must be set to true for this to function including the parent Scene's rootEntity!
  666. */
  667. bool processInputEvents;
  668. /**
  669. * If set to true, will block input events for entities below itself in the parent's entiy list.
  670. */
  671. bool blockMouseInput;
  672. /**
  673. * Returns the screen pixel position of the entity using a specified projection matrix, camera matrix and viewport.
  674. * @param projectionMatrix Projection matrix to use.
  675. * @param cameraMatrix Camera matrix to use.
  676. * @param viewport Viewport rectangle.
  677. * @return Pixel position of the entity on the screen.
  678. */
  679. Vector2 getScreenPosition(const Matrix4 &projectionMatrix, const Matrix4 &cameraMatrix, const Polycode::Rectangle &viewport);
  680. /**
  681. * Returns the screen pixel position of the entity using the last projection matrix, camera matrix and viewport that were set in the renderer.
  682. * @return Pixel position of the entity on the screen.
  683. */
  684. Vector2 getScreenPositionForMainCamera();
  685. /**
  686. * If set to true, will round the position of this entity to integral values. Use this if you need pixel-perfect positioning in 2D.
  687. */
  688. bool snapToPixels;
  689. bool mouseOver;
  690. /**
  691. * Sets the default blending mode for all created entities.
  692. */
  693. static int defaultBlendingMode;
  694. void recalculateAABBAllChildren();
  695. void recalculateAABB();
  696. /**
  697. Return axis-aligned bounding box in world space.
  698. */
  699. AABB getWorldAABB();
  700. /**
  701. * Returns the bounding box of the entity. This is used for hit-testing as well as visibility calculation.
  702. */
  703. Vector3 getLocalBoundingBox();
  704. /**
  705. * Sets the bounding box of the entity as a 3D Vector. This is used for hit-testing as well as visibility calculation.
  706. */
  707. void setLocalBoundingBox(const Vector3 box);
  708. /**
  709. * Sets the bounding box of the entity. This is used for hit-testing as well as visibility calculation.
  710. */
  711. void setLocalBoundingBox(Number x, Number y, Number z);
  712. /**
  713. * Sets the bounding box X-axis value of the entity.
  714. */
  715. void setLocalBoundingBoxX(Number x);
  716. /**
  717. * Sets the bounding box Y-axis value of the entity.
  718. */
  719. void setLocalBoundingBoxY(Number y);
  720. /**
  721. * Sets the bounding box Z-axis value of the entity.
  722. */
  723. void setLocalBoundingBoxZ(Number z);
  724. bool rendererVis;
  725. /**
  726. * Layer ID. Used by entity instances to separate entities into groups.
  727. */
  728. unsigned char layerID;
  729. std::vector <EntityProp> entityProps;
  730. protected:
  731. AABB aabb;
  732. Vector3 bBox;
  733. int lastClickTicks;
  734. Number yAdjust;
  735. std::vector<String> *tags;
  736. void *userData;
  737. std::vector<Entity*> children;
  738. Vector3 anchorPoint;
  739. Vector3 position;
  740. Vector3 scale;
  741. Vector3 rotation;
  742. Quaternion rotationQuat;
  743. bool lockMatrix;
  744. bool matrixDirty;
  745. Matrix4 transformMatrix;
  746. Entity *parentEntity;
  747. Renderer *renderer;
  748. };
  749. typedef Entity SceneEntity;
  750. typedef Entity ScreenEntity;
  751. }