Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Game.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. mLoaded = true;
  53. Render::setMode(RenderMode::Texture);
  54. if (mLara == -1) {
  55. Log::get(LOG_WARNING) << "Can't find Lara entity in level?!" << Log::endl;
  56. Console::setVisible(true);
  57. UI::setVisible(true);
  58. } else {
  59. Camera::setPosition(glm::vec3(getLara().getPosition().x,
  60. getLara().getPosition().y - 1024.0f,
  61. getLara().getPosition().z));
  62. }
  63. } else {
  64. Log::get(LOG_ERROR) << "No suitable loader for this level!" << Log::endl;
  65. return -1;
  66. }
  67. return 0;
  68. }
  69. void Game::handleAction(ActionEvents action, bool isFinished) {
  70. assert(action < ActionEventCount);
  71. if (!mLoaded)
  72. return;
  73. if (isFinished) {
  74. if (activeEvents[action])
  75. Camera::handleAction(action, isFinished);
  76. activeEvents[action] = false;
  77. } else {
  78. if (!activeEvents[action])
  79. Camera::handleAction(action, isFinished);
  80. activeEvents[action] = true;
  81. }
  82. }
  83. void Game::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  84. if (!mLoaded)
  85. return;
  86. Camera::handleMouseMotion(xrel, yrel);
  87. }
  88. void Game::handleControllerAxis(float value, KeyboardButton axis) {
  89. if (!mLoaded)
  90. return;
  91. Camera::handleControllerAxis(value, axis);
  92. }
  93. Entity& Game::getLara() {
  94. assert(mLara >= 0);
  95. assert(mLara < (int)getWorld().sizeEntity());
  96. return getWorld().getEntity(mLara);
  97. }
  98. void Game::setLara(long lara) {
  99. assert(lara >= 0);
  100. assert(lara < getWorld().sizeEntity());
  101. mLara = lara;
  102. }