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.

Game.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*!
  2. * \file src/Game.cpp
  3. * \brief Gameplay Handler
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Camera.h"
  9. #include "Console.h"
  10. #include "Game.h"
  11. #include "loader/Loader.h"
  12. #include "Log.h"
  13. #include "Menu.h"
  14. #include "Render.h"
  15. #include "SoundManager.h"
  16. #include "TextureManager.h"
  17. #include "UI.h"
  18. #include "World.h"
  19. bool Game::mLoaded = false;
  20. long Game::mLara = -1;
  21. bool Game::activeEvents[ActionEventCount];
  22. void Game::destroy() {
  23. mLoaded = false;
  24. mLara = -1;
  25. for (int i = 0; i < ActionEventCount; i++) {
  26. activeEvents[i] = false;
  27. }
  28. Render::setMode(RenderMode::LoadScreen);
  29. Camera::reset();
  30. Render::clearRoomList();
  31. SoundManager::clear();
  32. TextureManager::clear();
  33. World::destroy();
  34. }
  35. int Game::loadLevel(std::string level) {
  36. destroy();
  37. Log::get(LOG_INFO) << "Loading " << level << Log::endl;
  38. auto loader = Loader::createLoader(level);
  39. if (loader) {
  40. int error = loader->load(level);
  41. if (error != 0) {
  42. Log::get(LOG_ERROR) << "Error loading level (" << error << ")..." << Log::endl;
  43. destroy();
  44. return -2;
  45. }
  46. for (int i = 0; i < World::sizeMesh(); i++) {
  47. World::getMesh(i).prepare();
  48. }
  49. for (int i = 0; i < World::sizeRoom(); i++) {
  50. World::getRoom(i).prepare();
  51. }
  52. SoundManager::prepareSources();
  53. TextureManager::prepare();
  54. mLoaded = true;
  55. Render::setMode(RenderMode::Texture);
  56. Menu::setVisible(false);
  57. if (mLara == -1) {
  58. Log::get(LOG_WARNING) << "Can't find Lara entity in level?!" << Log::endl;
  59. } else {
  60. Camera::setPosition(glm::vec3(getLara().getPosition().x,
  61. getLara().getPosition().y - 1024.0f,
  62. getLara().getPosition().z));
  63. Camera::setRoom(getLara().getRoom());
  64. }
  65. } else {
  66. Log::get(LOG_ERROR) << "No suitable loader for this level!" << Log::endl;
  67. return -1;
  68. }
  69. return 0;
  70. }
  71. void Game::handleAction(ActionEvents action, bool isFinished) {
  72. orAssertLessThan(action, ActionEventCount);
  73. if (!mLoaded)
  74. return;
  75. if (isFinished) {
  76. if (activeEvents[action])
  77. Camera::handleAction(action, isFinished);
  78. activeEvents[action] = false;
  79. } else {
  80. if (!activeEvents[action])
  81. Camera::handleAction(action, isFinished);
  82. activeEvents[action] = true;
  83. }
  84. }
  85. void Game::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  86. if (!mLoaded)
  87. return;
  88. Camera::handleMouseMotion(xrel, yrel);
  89. }
  90. void Game::handleControllerAxis(float value, KeyboardButton axis) {
  91. if (!mLoaded)
  92. return;
  93. Camera::handleControllerAxis(value, axis);
  94. }
  95. Entity& Game::getLara() {
  96. orAssertGreaterThanEqual(mLara, 0);
  97. orAssertLessThan(mLara, World::sizeEntity());
  98. return World::getEntity(mLara);
  99. }
  100. void Game::setLara(long lara) {
  101. orAssertGreaterThanEqual(lara, 0);
  102. orAssertLessThan(lara, World::sizeEntity());
  103. mLara = lara;
  104. }