WorldHandler.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "WorldHandler.h"
  2. #include "World.h"
  3. #include "json.h"
  4. #include <fstream>
  5. #include <iostream>
  6. #include <string>
  7. WorldHandler* WorldHandler::instance = nullptr;
  8. void WorldHandler::ChangeWorld(int i)
  9. {
  10. if (i < 0)
  11. i = worldfiles.size() - 1;
  12. else if (i >= worldfiles.size())
  13. i = 0;
  14. if (i != worldIndex)
  15. {
  16. loadingWorld = true;
  17. if(worldIndex != -1)
  18. delete world;
  19. world = new World(worldfiles[i]);
  20. worldIndex = i;
  21. loadingWorld = false;
  22. }
  23. }
  24. WorldHandler::WorldHandler()
  25. {
  26. loadingWorld = true;
  27. worldIndex = -1;
  28. //Find worlds.json
  29. std::ifstream file("worlds/worlds.json");
  30. if (!file.is_open())
  31. std::cout << "Error, can't open worlds overview file\n";
  32. json::Value v = json::readJson(file);
  33. file.close();
  34. //Load file names into vector
  35. if (v["worlds"].isNull() || !v["worlds"].isArray())
  36. std::cout << "Error, no content in worlds overview file\n";
  37. for (auto line : v["worlds"])
  38. {
  39. std::cout << "Found world: " << line << "\n";
  40. worldfiles.push_back(line);
  41. }
  42. if (worldfiles.size() > 0)
  43. {
  44. ChangeWorld(0);
  45. }
  46. }
  47. WorldHandler::~WorldHandler()
  48. {
  49. worldIndex = -1;
  50. delete world;
  51. }
  52. WorldHandler* WorldHandler::getInstance()
  53. {
  54. if (instance == nullptr)
  55. instance = new WorldHandler();
  56. return instance;
  57. }
  58. void WorldHandler::init()
  59. {
  60. instance = new WorldHandler();
  61. }
  62. void WorldHandler::draw(void)
  63. {
  64. if(!loadingWorld)
  65. world->draw();
  66. else
  67. {
  68. //Draw Loading screen
  69. }
  70. }
  71. void WorldHandler::update(float deltaTime)
  72. {
  73. if(!loadingWorld)
  74. world->update(deltaTime);
  75. }
  76. void WorldHandler::teleportRandom(){
  77. Vec2f randomposition = world->randomPosition();
  78. printf("test %d %d", randomposition.x, randomposition.y);
  79. Player::getInstance()->position = Vec3f(randomposition.x, randomposition.y, world->getHeight(randomposition.x, randomposition.y));
  80. Player::getInstance()->setPosition(50,50,true);
  81. }
  82. bool WorldHandler::isPlayerPositionValid(void)
  83. {
  84. if(!loadingWorld)
  85. return world->isPlayerPositionValid();
  86. return false;
  87. }
  88. float WorldHandler::getHeight(float x, float y)
  89. {
  90. if (!loadingWorld)
  91. return world->getHeight(x, y);
  92. else
  93. return 0.0f;
  94. }
  95. void WorldHandler::Navigate(const std::string &fileName)
  96. {
  97. if (!loadingWorld)
  98. {
  99. for (int i = 0; i < worldfiles.size(); i++)
  100. {
  101. if (worldfiles[i] == fileName)
  102. ChangeWorld(i);
  103. }
  104. }
  105. }
  106. void WorldHandler::NextWorld()
  107. {
  108. if (!loadingWorld)
  109. {
  110. ChangeWorld(worldIndex + 1);
  111. Player::getInstance()->crystals = 0;
  112. }
  113. }
  114. void WorldHandler::PreviousWorld()
  115. {
  116. if (!loadingWorld)
  117. {
  118. ChangeWorld(worldIndex - 1);
  119. Player::getInstance()->crystals = 0;
  120. }
  121. }