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

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