PolyEntity.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. #include "PolyEntity.h"
  20. #include "PolyRenderer.h"
  21. using namespace Polycode;
  22. Rotation::Rotation() {
  23. pitch = 0;
  24. yaw = 0;
  25. roll = 0;
  26. }
  27. Entity::Entity() : EventDispatcher() {
  28. userData = NULL;
  29. scale.set(1,1,1);
  30. renderer = NULL;
  31. enabled = true;
  32. depthTest = true;
  33. visible = true;
  34. bBoxRadius = 0;
  35. color.setColor(1.0f,1.0f,1.0f,1.0f);
  36. parentEntity = NULL;
  37. matrixDirty = true;
  38. matrixAdj = 1.0f;
  39. billboardMode = false;
  40. billboardRoll = false;
  41. billboardIgnoreScale = false;
  42. backfaceCulled = true;
  43. depthOnly = false;
  44. depthWrite = true;
  45. ignoreParentMatrix = false;
  46. alphaTest = false;
  47. blendingMode = Renderer::BLEND_MODE_NORMAL;
  48. lockMatrix = false;
  49. renderWireframe = false;
  50. colorAffectsChildren = true;
  51. visibilityAffectsChildren = true;
  52. ownsChildren = false;
  53. enableScissor = false;
  54. editorOnly = false;
  55. tags = NULL;
  56. }
  57. Entity *Entity::getEntityById(String id, bool recursive) {
  58. for(int i=0;i<children.size();i++) {
  59. if(children[i]->id == id) {
  60. return children[i];
  61. } else {
  62. if(recursive) {
  63. Entity *ret = children[i]->getEntityById(id, recursive);
  64. if(ret) {
  65. return ret;
  66. }
  67. }
  68. }
  69. }
  70. return NULL;
  71. }
  72. Entity *Entity::Clone(bool deepClone, bool ignoreEditorOnly) {
  73. Entity *newEntity = new Entity();
  74. applyClone(newEntity, deepClone, ignoreEditorOnly);
  75. return newEntity;
  76. }
  77. void Entity::applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) {
  78. clone->ownsChildren = ownsChildren;
  79. clone->position = position;
  80. clone->rotation = rotation;
  81. clone->scale = scale;
  82. clone->color = color;
  83. clone->custEntityType = custEntityType;
  84. clone->billboardMode = billboardMode;
  85. clone->billboardRoll = billboardRoll;
  86. clone->alphaTest = alphaTest;
  87. clone->backfaceCulled = backfaceCulled;
  88. clone->renderWireframe = renderWireframe;
  89. clone->depthWrite = depthWrite;
  90. clone->depthTest = depthTest;
  91. clone->blendingMode = blendingMode;
  92. clone->colorAffectsChildren = colorAffectsChildren;
  93. clone->visibilityAffectsChildren = visibilityAffectsChildren;
  94. clone->depthOnly = depthOnly;
  95. clone->setUserData(getUserData());
  96. clone->entityProps = entityProps;
  97. clone->bBox = bBox;
  98. clone->ignoreParentMatrix = ignoreParentMatrix;
  99. clone->enableScissor = enableScissor;
  100. clone->scissorBox = scissorBox;
  101. clone->editorOnly = editorOnly;
  102. clone->id = id;
  103. if(tags == NULL) {
  104. clone->tags = NULL;
  105. } else {
  106. clone->tags = new std::vector<String>();
  107. for(int i=0; i < tags->size(); i++) {
  108. clone->addTag((*tags)[i]);
  109. }
  110. }
  111. clone->setRenderer(renderer);
  112. if(deepClone) {
  113. for(int i=0; i < children.size(); i++) {
  114. if(children[i]->editorOnly && ignoreEditorOnly) {
  115. } else {
  116. Entity *childClone = children[i]->Clone(deepClone, ignoreEditorOnly);
  117. clone->addChild(childClone);
  118. }
  119. }
  120. }
  121. }
  122. void Entity::setOwnsChildrenRecursive(bool val) {
  123. ownsChildren = val;
  124. for(int i=0; i < children.size(); i++) {
  125. children[i]->setOwnsChildrenRecursive(val);
  126. }
  127. }
  128. std::vector<Entity*> Entity::getEntitiesByTag(String tag, bool recursive) {
  129. std::vector<Entity*> retVector;
  130. for(int i=0;i<children.size();i++) {
  131. if(children[i]->hasTag(tag)) {
  132. retVector.push_back(children[i]);
  133. }
  134. if(recursive) {
  135. std::vector<Entity*> childVector = children[i]->getEntitiesByTag(tag, recursive);
  136. retVector.insert(retVector.end(), childVector.begin(), childVector.end());
  137. }
  138. }
  139. return retVector;
  140. }
  141. void Entity::setUserData(void *userData) {
  142. this->userData = userData;
  143. }
  144. void *Entity::getUserData() {
  145. return userData;
  146. }
  147. Entity *Entity::getParentEntity() const {
  148. return parentEntity;
  149. }
  150. Color Entity::getCombinedColor() const {
  151. if(parentEntity) {
  152. if(parentEntity->colorAffectsChildren)
  153. return color * parentEntity->getCombinedColor();
  154. else
  155. return color;
  156. } else {
  157. return color;
  158. }
  159. }
  160. Matrix4 Entity::getLookAtMatrix(const Vector3 &loc, const Vector3 &upVector) {
  161. rebuildTransformMatrix();
  162. Vector3 D;
  163. if(parentEntity)
  164. D = loc - (parentEntity->getConcatenatedMatrix() *position);
  165. else
  166. D = loc - position;
  167. Vector3 back = D * -1;
  168. back.Normalize();
  169. Vector3 right = back.crossProduct(upVector) ;
  170. right.Normalize();
  171. right = right * -1;
  172. Vector3 up = back.crossProduct(right);
  173. Matrix4 newMatrix(right.x, right.y, right.z, 0,
  174. up.x, up.y, up.z, 0,
  175. back.x, back.y, back.z, 0,
  176. 0, 0 , 0, 1);
  177. return newMatrix;
  178. }
  179. void Entity::lookAt(const Vector3 &loc, const Vector3 &upVector) {
  180. Matrix4 newMatrix = getLookAtMatrix(loc, upVector);
  181. rotationQuat.createFromMatrix(newMatrix);
  182. matrixDirty = true;
  183. }
  184. void Entity::lookAtEntity(Entity *entity, const Vector3 &upVector) {
  185. if(entity->getParentEntity())
  186. lookAt(entity->getParentEntity()->getConcatenatedMatrix() * (entity->getPosition()), upVector);
  187. else
  188. lookAt(entity->getPosition(), upVector);
  189. }
  190. void Entity::removeChild(Entity *entityToRemove) {
  191. for(int i=0;i<children.size();i++) {
  192. if(children[i] == entityToRemove) {
  193. children.erase(children.begin()+i);
  194. return;
  195. }
  196. }
  197. }
  198. unsigned int Entity::getNumChildren() {
  199. return children.size();
  200. }
  201. Entity *Entity::getChildAtIndex(unsigned int index) {
  202. if(index < children.size()) {
  203. return children[index];
  204. }
  205. return NULL;
  206. }
  207. void Entity::addChild(Entity *newChild) {
  208. addEntity(newChild);
  209. }
  210. void Entity::setColor(Color color) {
  211. this->color.setColor(&color);
  212. }
  213. void Entity::setColorInt(int r, int g, int b, int a) {
  214. color.setColorRGBA(r,g, b, a);
  215. }
  216. void Entity::setColor(Number r, Number g, Number b, Number a) {
  217. color.setColor(r,g,b,a);
  218. }
  219. void Entity::recalculateBBox() {
  220. }
  221. void Entity::setBlendingMode(int newBlendingMode) {
  222. blendingMode = newBlendingMode;
  223. }
  224. Number Entity::getBBoxRadius() const {
  225. Number compRad;
  226. Number biggest = bBoxRadius;
  227. for(int i=0;i<children.size();i++) {
  228. compRad = children[i]->getCompoundBBoxRadius();
  229. if(compRad > biggest)
  230. biggest = compRad;
  231. }
  232. return biggest;
  233. }
  234. Number Entity::getCompoundBBoxRadius() const {
  235. Number compRad;
  236. Number biggest = bBoxRadius + position.distance(Vector3(0,0,0));
  237. for(int i=0;i<children.size();i++) {
  238. compRad = children[i]->getCompoundBBoxRadius();
  239. if(compRad > biggest)
  240. biggest = compRad;
  241. }
  242. return biggest;
  243. }
  244. void Entity::setBBoxRadius(Number rad) {
  245. bBoxRadius = rad;
  246. }
  247. Entity::~Entity() {
  248. if(ownsChildren) {
  249. for(int i=0; i < children.size(); i++) {
  250. delete children[i];
  251. }
  252. }
  253. if(tags) delete tags;
  254. }
  255. Vector3 Entity::getChildCenter() const {
  256. return childCenter;
  257. }
  258. Matrix4 Entity::buildPositionMatrix() {
  259. Matrix4 posMatrix;
  260. posMatrix.m[3][0] = position.x*matrixAdj;
  261. posMatrix.m[3][1] = position.y*matrixAdj;
  262. posMatrix.m[3][2] = position.z*matrixAdj;
  263. return posMatrix;
  264. }
  265. void Entity::rebuildTransformMatrix() {
  266. if(lockMatrix)
  267. return;
  268. if(billboardMode){
  269. transformMatrix.identity();
  270. } else {
  271. transformMatrix = rotationQuat.createMatrix();
  272. }
  273. Matrix4 scaleMatrix;
  274. scaleMatrix.m[0][0] *= scale.x;
  275. scaleMatrix.m[1][1] *= scale.y;
  276. scaleMatrix.m[2][2] *= scale.z;
  277. Matrix4 posMatrix = buildPositionMatrix();
  278. transformMatrix = scaleMatrix*transformMatrix*posMatrix;
  279. matrixDirty = false;
  280. }
  281. void Entity::doUpdates() {
  282. Update();
  283. for(int i=0; i < children.size(); i++) {
  284. children[i]->doUpdates();
  285. }
  286. }
  287. void Entity::checkTransformSetters() {
  288. if(_position != position) {
  289. _position = position;
  290. matrixDirty = true;
  291. }
  292. if(_scale != scale) {
  293. _scale = scale;
  294. matrixDirty = true;
  295. }
  296. if(_rotation != rotation) {
  297. _rotation = rotation;
  298. rebuildRotation();
  299. matrixDirty = true;
  300. }
  301. }
  302. void Entity::updateEntityMatrix() {
  303. checkTransformSetters();
  304. if(matrixDirty)
  305. rebuildTransformMatrix();
  306. for(int i=0; i < children.size(); i++) {
  307. children[i]->updateEntityMatrix();
  308. }
  309. }
  310. Vector3 Entity::getCompoundScale() const {
  311. if(parentEntity != NULL) {
  312. Vector3 parentScale = parentEntity->getCompoundScale();
  313. return Vector3(scale.x * parentScale.x, scale.y * parentScale.y,scale.z * parentScale.z);
  314. }
  315. else
  316. return scale;
  317. }
  318. Matrix4 Entity::getConcatenatedRollMatrix() const {
  319. Quaternion q;
  320. q.createFromAxisAngle(0.0f, 0.0f, 1.0f, _rotation.roll*matrixAdj);
  321. Matrix4 transformMatrix = q.createMatrix();
  322. if(parentEntity != NULL)
  323. return transformMatrix * parentEntity->getConcatenatedRollMatrix();
  324. else
  325. return transformMatrix;
  326. }
  327. void Entity::transformAndRender() {
  328. if(!renderer || !enabled)
  329. return;
  330. if(depthOnly) {
  331. renderer->drawToColorBuffer(false);
  332. }
  333. bool isScissorEnabled;
  334. Polycode::Rectangle oldScissorBox;
  335. if(enableScissor) {
  336. isScissorEnabled = renderer->isScissorEnabled();
  337. oldScissorBox = renderer->getScissorBox();
  338. renderer->enableScissor(true);
  339. Polycode::Rectangle finalScrissorBox = scissorBox;
  340. // make sure that our scissor box is constrained to the parent one if it exists
  341. if(isScissorEnabled) {
  342. if(finalScrissorBox.x < oldScissorBox.x)
  343. finalScrissorBox.x = oldScissorBox.x;
  344. if(finalScrissorBox.x > oldScissorBox.x + oldScissorBox.w)
  345. finalScrissorBox.x = oldScissorBox.x + oldScissorBox.w;
  346. if(finalScrissorBox.x+finalScrissorBox.w > oldScissorBox.x + oldScissorBox.w)
  347. finalScrissorBox.w = oldScissorBox.x + oldScissorBox.w - finalScrissorBox.x;
  348. if(finalScrissorBox.y < oldScissorBox.y)
  349. finalScrissorBox.y = oldScissorBox.y;
  350. if(finalScrissorBox.y > oldScissorBox.y + oldScissorBox.h)
  351. finalScrissorBox.y = oldScissorBox.y + oldScissorBox.h;
  352. if(finalScrissorBox.y+finalScrissorBox.h > oldScissorBox.y + oldScissorBox.h)
  353. finalScrissorBox.h = oldScissorBox.y + oldScissorBox.h - finalScrissorBox.y;
  354. }
  355. renderer->setScissorBox(finalScrissorBox);
  356. }
  357. renderer->pushMatrix();
  358. if(ignoreParentMatrix && parentEntity) {
  359. renderer->multModelviewMatrix(parentEntity->getConcatenatedMatrix().Inverse());
  360. // renderer->setCurrentModelMatrix(parentEntity->getConcatenatedMatrix().Inverse());
  361. }
  362. renderer->multModelviewMatrix(transformMatrix);
  363. renderer->setCurrentModelMatrix(transformMatrix);
  364. renderer->setVertexColor(color.r,color.g,color.b,color.a);
  365. if(billboardMode) {
  366. if(billboardIgnoreScale) {
  367. renderer->billboardMatrix();
  368. } else {
  369. renderer->billboardMatrixWithScale(getCompoundScale());
  370. }
  371. if(billboardRoll) {
  372. renderer->multModelviewMatrix(getConcatenatedRollMatrix());
  373. }
  374. }
  375. if(!depthWrite)
  376. renderer->enableDepthWrite(false);
  377. else
  378. renderer->enableDepthWrite(true);
  379. if(!depthTest)
  380. renderer->enableDepthTest(false);
  381. else
  382. renderer->enableDepthTest(true);
  383. renderer->enableAlphaTest(alphaTest);
  384. Color combined = getCombinedColor();
  385. renderer->setVertexColor(combined.r,combined.g,combined.b,combined.a);
  386. renderer->setBlendingMode(blendingMode);
  387. renderer->enableBackfaceCulling(backfaceCulled);
  388. int mode = renderer->getRenderMode();
  389. if(renderWireframe)
  390. renderer->setRenderMode(Renderer::RENDER_MODE_WIREFRAME);
  391. else
  392. renderer->setRenderMode(Renderer::RENDER_MODE_NORMAL);
  393. if(visible) {
  394. Render();
  395. }
  396. if(visible || (!visible && !visibilityAffectsChildren)) {
  397. adjustMatrixForChildren();
  398. renderChildren();
  399. }
  400. renderer->setRenderMode(mode);
  401. renderer->popMatrix();
  402. if(!depthWrite)
  403. renderer->enableDepthWrite(true);
  404. if(depthOnly) {
  405. renderer->drawToColorBuffer(true);
  406. }
  407. if(enableScissor) {
  408. renderer->enableScissor(isScissorEnabled);
  409. renderer->setScissorBox(oldScissorBox);
  410. }
  411. }
  412. void Entity::setRenderer(Renderer *renderer) {
  413. this->renderer = renderer;
  414. for(int i=0;i<children.size();i++) {
  415. children[i]->setRenderer(renderer);
  416. }
  417. }
  418. void Entity::addEntity(Entity *newChild) {
  419. newChild->setRenderer(renderer);
  420. newChild->setParentEntity(this);
  421. children.push_back(newChild);
  422. }
  423. void Entity::renderChildren() {
  424. for(int i=0;i<children.size();i++) {
  425. children[i]->transformAndRender();
  426. }
  427. }
  428. void Entity::dirtyMatrix(bool val) {
  429. matrixDirty = val;
  430. }
  431. void Entity::setRotationQuat(Number w, Number x, Number y, Number z) {
  432. rotationQuat.w = w;
  433. rotationQuat.x = x;
  434. rotationQuat.y = y;
  435. rotationQuat.z = z;
  436. matrixDirty = true;
  437. }
  438. Quaternion Entity::getRotationQuat() const {
  439. return rotationQuat;
  440. }
  441. Vector3 Entity::getScale() const {
  442. return scale;
  443. }
  444. Matrix4 Entity::getConcatenatedMatrixRelativeTo(Entity *relativeEntity) {
  445. checkTransformSetters();
  446. if(matrixDirty)
  447. rebuildTransformMatrix();
  448. if(parentEntity != NULL && parentEntity != relativeEntity)
  449. return transformMatrix * parentEntity->getConcatenatedMatrixRelativeTo(relativeEntity);
  450. else
  451. return transformMatrix;
  452. }
  453. Matrix4 Entity::getConcatenatedMatrix() {
  454. checkTransformSetters();
  455. if(matrixDirty)
  456. rebuildTransformMatrix();
  457. if(parentEntity != NULL)
  458. return transformMatrix * parentEntity->getConcatenatedMatrix();
  459. else
  460. return transformMatrix;
  461. }
  462. const Matrix4& Entity::getTransformMatrix() const {
  463. return transformMatrix;
  464. }
  465. void Entity::Pitch(Number pitch) {
  466. rotation.pitch += pitch;
  467. matrixDirty = true;
  468. }
  469. void Entity::Yaw(Number yaw) {
  470. rotation.yaw += yaw;
  471. matrixDirty = true;
  472. }
  473. void Entity::Roll(Number roll) {
  474. rotation.roll += roll;
  475. matrixDirty = true;
  476. }
  477. void Entity::setRoll(Number roll) {
  478. rotation.roll = roll;
  479. matrixDirty = true;
  480. }
  481. void Entity::setPitch(Number pitch) {
  482. rotation.pitch = pitch;
  483. matrixDirty = true;
  484. }
  485. void Entity::setYaw(Number yaw) {
  486. rotation.yaw = yaw;
  487. matrixDirty = true;
  488. }
  489. void Entity::rebuildRotation() {
  490. rotationQuat.fromAxes(_rotation.pitch, _rotation.yaw, _rotation.roll);
  491. }
  492. void Entity::setEntityProp(const String& propName, const String& propValue) {
  493. for(int i=0; i < entityProps.size(); i++) {
  494. if(entityProps[i].propName == propName) {
  495. entityProps[i].propValue = propValue;
  496. return;
  497. }
  498. }
  499. EntityProp entityProp;
  500. entityProp.propName = propName;
  501. entityProp.propValue = propValue;
  502. entityProps.push_back(entityProp);
  503. }
  504. String Entity::getEntityProp(const String& propName) {
  505. for(int i=0; i < entityProps.size(); i++) {
  506. if(entityProps[i].propName == propName) {
  507. return entityProps[i].propValue;
  508. }
  509. }
  510. return "null";
  511. }
  512. Vector3 Entity::getCombinedPosition() const {
  513. if(parentEntity != NULL)
  514. return (parentEntity->getCombinedPosition())+position;
  515. else
  516. return position;
  517. }
  518. void Entity::setParentEntity(Entity *entity) {
  519. parentEntity = entity;
  520. }
  521. Number Entity::getPitch() const {
  522. return rotation.pitch;
  523. }
  524. Number Entity::getYaw() const {
  525. return rotation.yaw;
  526. }
  527. Number Entity::getRoll() const {
  528. return rotation.roll;
  529. }
  530. void Entity::setTransformByMatrixPure(const Matrix4& matrix) {
  531. transformMatrix = matrix;
  532. }
  533. void Entity::setPosition(const Vector3 &posVec) {
  534. position = posVec;
  535. matrixDirty = true;
  536. }
  537. void Entity::setPositionX(Number x) {
  538. position.x = x;
  539. matrixDirty = true;
  540. }
  541. void Entity::setPositionY(Number y) {
  542. position.y = y;
  543. matrixDirty = true;
  544. }
  545. void Entity::setPositionZ(Number z) {
  546. position.z = z;
  547. matrixDirty = true;
  548. }
  549. void Entity::setScaleX(Number x) {
  550. scale.x = x;
  551. matrixDirty = true;
  552. }
  553. void Entity::setScaleY(Number y) {
  554. scale.y = y;
  555. matrixDirty = true;
  556. }
  557. void Entity::setScaleZ(Number z) {
  558. scale.z = z;
  559. matrixDirty = true;
  560. }
  561. void Entity::setScale(const Vector3 &v) {
  562. scale.x = v.x;
  563. scale.y = v.y;
  564. scale.z = v.z;
  565. matrixDirty = true;
  566. }
  567. void Entity::setPosition(Number x, Number y, Number z) {
  568. position.x = x;
  569. position.y = y;
  570. position.z = z;
  571. matrixDirty = true;
  572. }
  573. void Entity::Translate(const Vector3 &tVec) {
  574. position += tVec;
  575. matrixDirty = true;
  576. }
  577. void Entity::Translate(Number x, Number y, Number z) {
  578. position.x += x;
  579. position.y += y;
  580. position.z += z;
  581. matrixDirty = true;
  582. }
  583. void Entity::Scale(Number x, Number y, Number z) {
  584. scale.x *= x;
  585. scale.y *= y;
  586. scale.z *= z;
  587. matrixDirty = true;
  588. }
  589. void Entity::setScale(Number x, Number y, Number z) {
  590. scale.x = x;
  591. scale.y = y;
  592. scale.z = z;
  593. matrixDirty = true;
  594. }
  595. Vector3 Entity::getPosition() const {
  596. return position;
  597. }
  598. Number Entity::getCombinedPitch() const {
  599. if(parentEntity != NULL)
  600. return parentEntity->getCombinedPitch()+rotation.pitch;
  601. else
  602. return rotation.pitch;
  603. }
  604. Number Entity::getCombinedYaw() const {
  605. if(parentEntity != NULL)
  606. return parentEntity->getCombinedYaw()+rotation.yaw;
  607. else
  608. return rotation.yaw;
  609. }
  610. Number Entity::getCombinedRoll() const {
  611. if(parentEntity != NULL)
  612. return parentEntity->getCombinedRoll()+rotation.roll;
  613. else
  614. return rotation.roll;
  615. }
  616. unsigned int Entity::getNumTags() const {
  617. if(!tags) return 0;
  618. return tags->size();
  619. }
  620. String Entity::getTagAtIndex(unsigned int index) const {
  621. if(!tags) return "";
  622. if(index < tags->size())
  623. return (*tags)[index];
  624. return "";
  625. }
  626. bool Entity::hasTag(String tag) const {
  627. if(!tags) return false;
  628. for(int i=0; i < tags->size(); i++) {
  629. if((*tags)[i] == tag)
  630. return true;
  631. }
  632. return false;
  633. }
  634. void Entity::clearTags() {
  635. if(!tags) return;
  636. tags->clear();
  637. }
  638. void Entity::addTag(String tag) {
  639. if(!tags) tags = new std::vector<String>();
  640. if(!hasTag(tag)) {
  641. tags->push_back(tag);
  642. }
  643. }