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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.push_back(&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.push_back(&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.push_back(&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.push_back(&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. mMeshes.push_back(&model);
  56. }
  57. unsigned long World::sizeStaticMesh() {
  58. return mMeshes.size();
  59. }
  60. StaticMesh &World::getStaticMesh(unsigned long index) {
  61. assert(index < mMeshes.size());
  62. return *mMeshes.at(index);
  63. }
  64. long World::getRoomByLocation(long index, float x, float y, float z)
  65. {
  66. assert(index >= 0);
  67. assert(index < (long)mRooms.size());
  68. Room &room = *mRooms.at(index);
  69. if (room.getBoundingBox().inBox(x, y, z))
  70. return index;
  71. else
  72. return getRoomByLocation(x, y, z);
  73. }
  74. long World::getRoomByLocation(float x, float y, float z) {
  75. long hop = -1;
  76. for (unsigned long i = 0; i < mRooms.size(); i++) {
  77. if (mRooms.at(i)->getBoundingBox().inBoxPlane(x, z)) {
  78. if (mRooms.at(i)->getBoundingBox().inBox(x, y, z))
  79. return i;
  80. else
  81. hop = i; // This room is above or below current position
  82. }
  83. }
  84. return hop;
  85. }
  86. void World::destroy() {
  87. for (unsigned long i = 0; i < mRooms.size(); i++)
  88. delete mRooms[i];
  89. mRooms.clear();
  90. for (unsigned long i = 0; i < mSprites.size(); i++)
  91. delete mSprites[i];
  92. mSprites.clear();
  93. for (unsigned long i = 0; i < mEntities.size(); i++)
  94. delete mEntities[i];
  95. mEntities.clear();
  96. for (unsigned long i = 0; i < mModels.size(); i++)
  97. delete mModels[i];
  98. mModels.clear();
  99. for (unsigned long i = 0; i < mMeshes.size(); i++)
  100. delete mMeshes[i];
  101. mMeshes.clear();
  102. }