Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Game.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. getWorld().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 < getWorld().sizeMesh(); i++) {
  47. getWorld().getMesh(i).prepare();
  48. }
  49. for (int i = 0; i < getWorld().sizeRoom(); i++) {
  50. getWorld().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. }
  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. orAssertLessThan(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. orAssertGreaterThanEqual(mLara, 0);
  96. orAssertLessThan(mLara, getWorld().sizeEntity());
  97. return getWorld().getEntity(mLara);
  98. }
  99. void Game::setLara(long lara) {
  100. orAssertGreaterThanEqual(lara, 0);
  101. orAssertLessThan(lara, getWorld().sizeEntity());
  102. mLara = lara;
  103. }