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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*!
  2. * \file src/Game.cpp
  3. * \brief Game abstraction
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include <map>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include "global.h"
  12. #include "Camera.h"
  13. #include "Game.h"
  14. #include "loader/Loader.h"
  15. #include "Log.h"
  16. #include "Render.h"
  17. #include "SoundManager.h"
  18. #include "StaticMesh.h"
  19. #include "system/Sound.h"
  20. #include "TextureManager.h"
  21. #include "UI.h"
  22. #include "World.h"
  23. #include "utils/strings.h"
  24. Game::Game() {
  25. mLoaded = false;
  26. mLara = -1;
  27. }
  28. Game::~Game() {
  29. }
  30. int Game::initialize() {
  31. // Enable Renderer
  32. Render::setMode(RenderMode::LoadScreen);
  33. return 0;
  34. }
  35. void Game::display() {
  36. Render::display();
  37. }
  38. void Game::destroy() {
  39. mLoaded = false;
  40. mLara = -1;
  41. Render::setMode(RenderMode::LoadScreen);
  42. getWorld().destroy();
  43. Sound::clear(); // Remove all previously loaded sounds
  44. SoundManager::clear();
  45. getTextureManager().clear();
  46. }
  47. bool Game::isLoaded() {
  48. return mLoaded;
  49. }
  50. int Game::loadLevel(const char* level) {
  51. destroy();
  52. levelName = level;
  53. getLog() << "Loading " << levelName << Log::endl;
  54. auto loader = Loader::createLoader(level);
  55. if (loader) {
  56. // First Loader test
  57. getLog() << "Trying to load using new loader..." << Log::endl;
  58. int error = loader->load(level);
  59. if (error != 0) {
  60. getLog() << "Error while trying new loader (" << error << ")..." << Log::endl;
  61. destroy();
  62. return -2;
  63. } else {
  64. SoundManager::prepareSources();
  65. if (mLara == -1) {
  66. getLog() << "Can't find Lara entity in level?!" << Log::endl;
  67. } else {
  68. mLoaded = true;
  69. //Render::setMode(RenderMode::Texture);
  70. }
  71. UI::setVisible(true);
  72. return 0;
  73. }
  74. } else {
  75. getLog() << "No suitable loader for this level!" << Log::endl;
  76. return -1;
  77. }
  78. }
  79. void Game::handleAction(ActionEvents action, bool isFinished) {
  80. if (mLoaded && (!isFinished)) {
  81. if (action == forwardAction) {
  82. getLara().move('f');
  83. } else if (action == backwardAction) {
  84. getLara().move('b');
  85. } else if (action == leftAction) {
  86. getLara().move('l');
  87. } else if (action == rightAction) {
  88. getLara().move('r');
  89. } else if (action == jumpAction) {
  90. } else if (action == crouchAction) {
  91. } else if (action == useAction) {
  92. } else if (action == holsterAction) {
  93. } else if (action == walkAction) {
  94. }
  95. }
  96. }
  97. void Game::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
  98. if (mLoaded) {
  99. Camera::handleMouseMotion(xrel, yrel);
  100. /*
  101. float angles[3] = { 0.0f, getCamera().getRadianYaw(), getCamera().getRadianPitch() };
  102. getLara().setAngles(angles);
  103. */
  104. }
  105. }
  106. Entity& Game::getLara() {
  107. assert(mLara >= 0);
  108. assert(mLara < (int)getWorld().sizeEntity());
  109. return getWorld().getEntity(mLara);
  110. }
  111. void Game::setLara(long lara) {
  112. assert(lara >= 0);
  113. assert(lara < getWorld().sizeEntity());
  114. mLara = lara;
  115. }