Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Entity.cpp 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*!
  2. * \file src/Entity.cpp
  3. * \brief World Entities
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cmath>
  8. #include "global.h"
  9. #include "Console.h"
  10. #include "Entity.h"
  11. #include "Render.h"
  12. #include "World.h"
  13. #include "games/TombRaider1.h"
  14. Entity::Entity(TombRaider &tr, unsigned int index, unsigned int i, unsigned int model) {
  15. tr2_moveable_t *moveable = tr.Moveable();
  16. tr2_item_t *item = tr.Item();
  17. pos[0] = item[i].x;
  18. pos[1] = item[i].y;
  19. pos[2] = item[i].z;
  20. angles[0] = 0;
  21. angles[1] = OR_DEG_TO_RAD(((item[i].angle >> 14) & 0x03) * 90.0f);
  22. angles[2] = 0;
  23. objectId = moveable[index].object_id;
  24. moveType = MoveTypeWalk;
  25. room = getWorld().getRoomByLocation(pos[0], pos[1], pos[2]);
  26. skeletalModel = model;
  27. boneFrame = 0;
  28. animationFrame = 0;
  29. idleAnimation = 0;
  30. state = 0;
  31. }
  32. bool Entity::operator<(Entity &o) {
  33. float distA = getRender().mViewVolume.getDistToSphereFromNear(pos[0], pos[1], pos[2], 1.0f);
  34. float distB = getRender().mViewVolume.getDistToSphereFromNear(o.pos[0], o.pos[1], o.pos[2], 1.0f);
  35. return (distA < distB);
  36. }
  37. void Entity::display() {
  38. glPushMatrix();
  39. glTranslatef(pos[0], pos[1], pos[2]);
  40. glRotatef(OR_RAD_TO_DEG(angles[1]), 0, 1, 0);
  41. glRotatef(OR_RAD_TO_DEG(angles[0]), 1, 0, 0);
  42. //glRotatef(OR_RAD_TO_DEG(angles[2]), 0, 0, 1);
  43. getWorld().getSkeletalModel(skeletalModel).display(animationFrame, boneFrame);
  44. glPopMatrix();
  45. // Cycle frames
  46. if (getRender().getFlags() & Render::fAnimateAllModels) {
  47. if (boneFrame < (getModel().get(animationFrame).size() - 1))
  48. boneFrame++;
  49. else
  50. boneFrame = 0;
  51. }
  52. }
  53. void Entity::move(char movement) {
  54. const float moved = 180.0f;
  55. const float testd = 220.0f;
  56. const float camHeight = 8.0f;
  57. float x, y, z, pitch, h, floor, ceiling;
  58. int roomNew, sector;
  59. bool wall;
  60. unsigned int roomFlags;
  61. switch (moveType) {
  62. case MoveTypeWalkNoSwim:
  63. case MoveTypeWalk:
  64. pitch = 0.0f; // in the future pitch could control jump up blocks here
  65. break;
  66. case MoveTypeNoClipping:
  67. case MoveTypeFly:
  68. case MoveTypeSwim:
  69. pitch = angles[2];
  70. break;
  71. }
  72. switch (movement) {
  73. case 'f':
  74. x = pos[0] + (testd * sinf(angles[1]));
  75. y = pos[1] + (testd * sinf(pitch));
  76. z = pos[2] + (testd * cosf(angles[1]));
  77. break;
  78. case 'b':
  79. x = pos[0] - (testd * sinf(angles[1]));
  80. y = pos[1] - (testd * sinf(pitch));
  81. z = pos[2] - (testd * cosf(angles[1]));
  82. break;
  83. case 'l':
  84. x = pos[0] - (testd * sinf(angles[1] + 90.0f));
  85. y = pos[1];
  86. z = pos[2] - (testd * cosf(angles[1] + 90.0f));
  87. break;
  88. case 'r':
  89. x = pos[0] + (testd * sinf(angles[1] + 90.0f));
  90. y = pos[1];
  91. z = pos[2] + (testd * cosf(angles[1] + 90.0f));
  92. break;
  93. default:
  94. return;
  95. }
  96. roomNew = getWorld().getRoomByLocation(room, x, y, z);
  97. if (roomNew == -1) { // Will we hit a portal?
  98. roomNew = getWorld().getRoom(room).getAdjoiningRoom(pos[0], pos[1], pos[2],
  99. x, y, z);
  100. if (roomNew > -1)
  101. getConsole().print("Crossing from room %i to %i", room, roomNew);
  102. else
  103. //! \fixme mRooms, sectors, ... are now std::vector, but often upper bound checks are missing
  104. return;
  105. }
  106. roomFlags = getWorld().getRoom(roomNew).getFlags();
  107. sector = getWorld().getRoom(roomNew).getSector(x, z, &floor, &ceiling);
  108. wall = getWorld().getRoom(roomNew).isWall(sector);
  109. // If you're underwater you may want to swim =)
  110. // ...if you're worldMoveType_walkNoSwim, you better hope it's shallow
  111. if ((roomFlags & RoomFlagUnderWater) && (moveType == MoveTypeWalk))
  112. moveType = MoveTypeSwim;
  113. // Don't swim on land
  114. if (!(roomFlags & RoomFlagUnderWater) && (moveType == MoveTypeSwim))
  115. moveType = MoveTypeWalk;
  116. // Mongoose 2002.09.02, Add check for room -> room transition
  117. // (Only allow by movement between rooms by using portals)
  118. if (((moveType == MoveTypeNoClipping) ||
  119. (moveType == MoveTypeFly) ||
  120. (moveType == MoveTypeSwim)) ||
  121. ((roomNew > -1) && (!wall))) {
  122. room = roomNew;
  123. switch (movement) {
  124. case 'f':
  125. x = pos[0] + (moved * sinf(angles[1]));
  126. y = pos[1] + (moved * sinf(pitch));
  127. z = pos[2] + (moved * cosf(angles[1]));
  128. break;
  129. case 'b':
  130. x = pos[0] - (moved * sinf(angles[1]));
  131. y = pos[1] - (moved * sinf(pitch));
  132. z = pos[2] - (moved * cosf(angles[1]));
  133. break;
  134. case 'l':
  135. x = pos[0] - (moved * sinf(angles[1] + 90.0f));
  136. z = pos[2] - (moved * cosf(angles[1] + 90.0f));
  137. break;
  138. case 'r':
  139. x = pos[0] + (moved * sinf(angles[1] + 90.0f));
  140. z = pos[2] + (moved * cosf(angles[1] + 90.0f));
  141. break;
  142. }
  143. /*! \fixme Test for vector (move vector) / plane (portal) collision here
  144. * to see if we need to switch rooms... man...
  145. */
  146. h = y;
  147. getWorld().getRoom(room).getHeightAtPosition(x, &h, z);
  148. switch (moveType) {
  149. case MoveTypeFly:
  150. case MoveTypeSwim:
  151. // Don't fall out of world, avoid a movement that does
  152. if (h > y - camHeight) {
  153. pos[0] = x;
  154. pos[1] = y;
  155. pos[2] = z;
  156. }
  157. break;
  158. case MoveTypeWalk:
  159. case MoveTypeWalkNoSwim:
  160. y = pos[1]; // Override vector movement walking ( er, not pretty )
  161. // Now fake gravity
  162. // Mongoose 2002.08.14, Remember TR is upside down ( you fall 'up' )
  163. //ddist = h - pos[1];
  164. // This is to force false gravity, by making camera stay on ground
  165. pos[1] = h; //roomFloor->bbox_min[1];
  166. // Check for camera below terrian and correct
  167. if (pos[1] < h - camHeight)
  168. pos[1] = h - camHeight;
  169. pos[0] = x;
  170. pos[2] = z;
  171. break;
  172. case MoveTypeNoClipping:
  173. pos[0] = x;
  174. pos[1] = y;
  175. pos[2] = z;
  176. break;
  177. }
  178. }
  179. }
  180. void Entity::print() {
  181. getConsole().print("Entity %d:", objectId);
  182. getConsole().print(" Room %i (0x%X)", room, getWorld().getRoom(room).getFlags());
  183. getConsole().print(" %.1fx %.1fy %.1fz", pos[0], pos[1], pos[2]);
  184. getConsole().print(" %.1f Yaw", OR_RAD_TO_DEG(angles[1]));
  185. }
  186. SkeletalModel &Entity::getModel() {
  187. return getWorld().getSkeletalModel(skeletalModel);
  188. }
  189. void Entity::setSkeletalModel(unsigned int model) {
  190. skeletalModel = model;
  191. animationFrame = 0;
  192. boneFrame = 0;
  193. idleAnimation = 0;
  194. }
  195. Entity::MoveType Entity::getMoveType() {
  196. return moveType;
  197. }
  198. void Entity::setMoveType(MoveType m) {
  199. moveType = m;
  200. }
  201. int Entity::getObjectId() {
  202. return objectId;
  203. }
  204. void Entity::setAngles(float a[3]) {
  205. for (unsigned int i = 0; i < 3; i++)
  206. angles[i] = a[i];
  207. }
  208. float Entity::getPos(unsigned int i) {
  209. return pos[i];
  210. }
  211. float Entity::getAngle(unsigned int i) {
  212. return angles[i];
  213. }
  214. int Entity::getRoom() {
  215. return room;
  216. }
  217. unsigned int Entity::getAnimation() {
  218. return animationFrame;
  219. }
  220. void Entity::setAnimation(unsigned int index) {
  221. animationFrame = index;
  222. boneFrame = 0;
  223. }
  224. unsigned int Entity::getBoneFrame() {
  225. return boneFrame;
  226. }
  227. void Entity::setBoneFrame(unsigned int index) {
  228. boneFrame = index;
  229. }
  230. unsigned int Entity::getIdleAnimation() {
  231. return idleAnimation;
  232. }
  233. void Entity::setIdleAnimation(unsigned int index) {
  234. idleAnimation = index;
  235. }