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.

World.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*!
  2. * \file src/World.cpp
  3. * \brief The game world (model)
  4. *
  5. * \author Mongoose
  6. */
  7. #include <cstdio>
  8. #include <math.h>
  9. #include "global.h"
  10. #include "World.h"
  11. World::~World() {
  12. destroy();
  13. }
  14. void World::addRoom(Room* room) {
  15. mRooms.emplace_back(std::unique_ptr<Room>(room));
  16. }
  17. unsigned long World::sizeRoom() {
  18. return mRooms.size();
  19. }
  20. Room& World::getRoom(unsigned long index) {
  21. assert(index < mRooms.size());
  22. return *mRooms.at(index);
  23. }
  24. void World::addSprite(SpriteSequence* sprite) {
  25. mSprites.emplace_back(std::unique_ptr<SpriteSequence>(sprite));
  26. }
  27. unsigned long World::sizeSprite() {
  28. return mSprites.size();
  29. }
  30. SpriteSequence& World::getSprite(unsigned long index) {
  31. assert(index < mSprites.size());
  32. return *mSprites.at(index);
  33. }
  34. void World::addEntity(Entity* entity) {
  35. mEntities.emplace_back(std::unique_ptr<Entity>(entity));
  36. }
  37. unsigned long World::sizeEntity() {
  38. return mEntities.size();
  39. }
  40. Entity& World::getEntity(unsigned long index) {
  41. assert(index < mEntities.size());
  42. return *mEntities.at(index);
  43. }
  44. void World::addSkeletalModel(SkeletalModel* model) {
  45. mModels.emplace_back(std::unique_ptr<SkeletalModel>(model));
  46. }
  47. unsigned long World::sizeSkeletalModel() {
  48. return mModels.size();
  49. }
  50. SkeletalModel& World::getSkeletalModel(unsigned long index) {
  51. assert(index < mModels.size());
  52. return *mModels.at(index);
  53. }
  54. void World::addStaticMesh(StaticMesh* model) {
  55. mStaticMeshes.emplace_back(std::unique_ptr<StaticMesh>(model));
  56. }
  57. unsigned long World::sizeStaticMesh() {
  58. return mStaticMeshes.size();
  59. }
  60. StaticMesh& World::getStaticMesh(unsigned long index) {
  61. assert(index < mStaticMeshes.size());
  62. return *mStaticMeshes.at(index);
  63. }
  64. void World::addMesh(Mesh* mesh) {
  65. mMeshes.emplace_back(mesh);
  66. }
  67. unsigned long World::sizeMesh() {
  68. return mMeshes.size();
  69. }
  70. Mesh& World::getMesh(unsigned long index) {
  71. assert(index < mMeshes.size());
  72. return *mMeshes.at(index);
  73. }
  74. long World::getRoomByLocation(long index, float x, float y, float z) {
  75. assert(index >= 0);
  76. assert(index < (long)mRooms.size());
  77. Room& room = *mRooms.at(index);
  78. if (room.getBoundingBox().inBox(x, y, z))
  79. return index;
  80. else
  81. return getRoomByLocation(x, y, z);
  82. }
  83. long World::getRoomByLocation(float x, float y, float z) {
  84. long hop = -1;
  85. for (unsigned long i = 0; i < mRooms.size(); i++) {
  86. if (mRooms.at(i)->getBoundingBox().inBoxPlane(x, z)) {
  87. if (mRooms.at(i)->getBoundingBox().inBox(x, y, z))
  88. return i;
  89. else
  90. hop = i; // This room is above or below current position
  91. }
  92. }
  93. return hop;
  94. }
  95. void World::destroy() {
  96. mRooms.clear();
  97. mSprites.clear();
  98. mEntities.clear();
  99. mModels.clear();
  100. mStaticMeshes.clear();
  101. mMeshes.clear();
  102. }