World.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "World.h"
  2. #include <GL/freeglut.h>
  3. #include "Entity.h"
  4. #include "json.h"
  5. #include "Model.h"
  6. #include "CrystalPoint.h"
  7. #include <fstream>
  8. #include <iostream>
  9. #include "WorldHandler.h"
  10. World::World(const std::string &fileName)
  11. {
  12. //Store player instance
  13. player = Player::getInstance();
  14. //Create the interface
  15. interface = new Interface();
  16. //Open world json file
  17. std::ifstream file(fileName);
  18. if(!file.is_open())
  19. std::cout<<"Error, can't open world file - " << fileName << "\n";
  20. json::Value v = json::readJson(file);
  21. file.close();
  22. //Check file
  23. if(v["world"].isNull() || v["world"]["heightmap"].isNull() || v["world"]["skybox"].isNull())
  24. std::cout << "Invalid world file: world - " << fileName << "\n";
  25. if (v["world"]["object-templates"].isNull())
  26. std::cout << "Invalid world file: object templates - " << fileName << "\n";
  27. if (v["player"].isNull() || v["player"]["startposition"].isNull())
  28. std::cout << "Invalid world file: player - " << fileName << "\n";
  29. if (v["objects"].isNull())
  30. std::cout << "Invalid world file: objects - " << fileName << "\n";
  31. if (v["enemies"].isNull())
  32. std::cout << "Invalid world file: enemies - " << fileName << "\n";
  33. if (v["crystals"].isNull())
  34. std::cout << "Invalid world file: crystals - " << fileName << "\n";
  35. //Load object templates
  36. for (auto objt : v["world"]["object-templates"])
  37. {
  38. //collision
  39. bool cancollide = true;
  40. if (!objt["collision"].isNull())
  41. cancollide = objt["collision"].asBool();
  42. objecttemplates.push_back(std::pair<int, std::pair<std::string, bool>>(objt["color"], std::pair<std::string, bool>(objt["file"], cancollide)));
  43. }
  44. //Generate heightmap for this world
  45. heightmap = new HeightMap(v["world"]["heightmap"].asString(), this);
  46. //Load skybox
  47. skybox = new Skybox(15000.0f, v["world"]["skybox"].asString());
  48. skybox->init();
  49. //Map different texture to heightmap if available
  50. if(!v["world"]["texture"].isNull())
  51. heightmap->SetTexture(v["world"]["texture"].asString());
  52. //Set player starting position
  53. player->position.x = v["player"]["startposition"][0].asFloat();
  54. player->position.y = v["player"]["startposition"][1].asFloat();
  55. player->position.z = v["player"]["startposition"][2].asFloat();
  56. //Load and place objects into world
  57. for (auto object : v["objects"])
  58. {
  59. //Collision
  60. bool hasCollision = true;
  61. if (!object["collide"].isNull())
  62. hasCollision = object["collide"].asBool();
  63. //Rotation
  64. Vec3f rotation(0, 0, 0);
  65. if(!object["rot"].isNull())
  66. rotation = Vec3f(object["rot"][0].asFloat(), object["rot"][1].asFloat(), object["rot"][2].asFloat());
  67. //Scale
  68. float scale = 1;
  69. if (!object["scale"].isNull())
  70. scale = object["scale"].asFloat();
  71. //Position
  72. if (object["pos"].isNull())
  73. std::cout << "Invalid world file: objects pos - " << fileName << "\n";
  74. //File
  75. if (object["file"].isNull())
  76. std::cout << "Invalid world file: objects file - " << fileName << "\n";
  77. //Create
  78. Vec3f position(object["pos"][0].asFloat(), object["pos"][1].asFloat(), object["pos"][2].asFloat());
  79. position.y = getHeight(position.x, position.z) + 2.0f;
  80. entities.push_back(new LevelObject(object["file"].asString(), position, rotation, scale, hasCollision));
  81. }
  82. //Load and place enemies into world
  83. for (auto e : v["enemies"])
  84. {
  85. //Rotation
  86. Vec3f rotation(0, 0, 0);
  87. if (!e["rot"].isNull())
  88. rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
  89. //Scale
  90. float scale = 1.0f;
  91. if (!e["scale"].isNull())
  92. scale = e["scale"].asFloat();
  93. //Position
  94. if (e["pos"].isNull())
  95. std::cout << "Invalid world file: enemies pos - " << fileName << "\n";
  96. //File
  97. if (e["file"].isNull())
  98. std::cout << "Invalid world file: enemies file - " << fileName << "\n";
  99. //Create
  100. Vec3f position(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
  101. position.y = getHeight(position.x, position.z) + 2.0f;
  102. enemies.push_back(new Enemy(e["file"].asString(), position, rotation, scale));
  103. }
  104. for (auto e : v["crystals"])
  105. {
  106. //Rotation
  107. Vec3f rotation(0, 0, 0);
  108. if (!e["rot"].isNull())
  109. rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
  110. //Scale
  111. float scale = 1.0f;
  112. if (!e["scale"].isNull())
  113. scale = e["scale"].asFloat();
  114. //Position
  115. if (e["pos"].isNull())
  116. std::cout << "Invalid world file: enemies pos - " << fileName << "\n";
  117. //File
  118. if (e["file"].isNull())
  119. std::cout << "Invalid world file: enemies file - " << fileName << "\n";
  120. //Create
  121. Vec3f position(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
  122. position.y = getHeight(position.x, position.z) + 2.0f;
  123. crystals.push_back(new Crystal(e["file"].asString(), position, rotation, scale));
  124. }
  125. }
  126. World::~World()
  127. {
  128. delete heightmap;
  129. delete skybox;
  130. }
  131. std::pair<std::string, bool> World::getObjectFromValue(int val)
  132. {
  133. for (auto i : objecttemplates)
  134. {
  135. if (i.first == val)
  136. return i.second;
  137. }
  138. return objecttemplates[0].second;
  139. }
  140. float World::getHeight(float x, float y)
  141. {
  142. return heightmap->GetHeight(x, y);
  143. }
  144. void World::draw()
  145. {
  146. player->setCamera();
  147. float lightPosition[4] = { 0, 2, 1, 0 };
  148. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  149. float lightAmbient[4] = { 0.2, 0.2, 0.2, 1 };
  150. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  151. skybox->draw();
  152. heightmap->Draw();
  153. for (auto &enemy : enemies)
  154. enemy->draw();
  155. for (auto &entity : entities)
  156. entity->draw();
  157. for (auto &crystal : crystals)
  158. crystal->draw();
  159. interface->draw();
  160. }
  161. void World::update(float elapsedTime)
  162. {
  163. for (auto &entity : entities)
  164. entity->update(elapsedTime);
  165. for (auto &enemy : enemies)
  166. {
  167. //Al deze code zou in enemy moeten staan
  168. enemy->inEyeSight(player->position);
  169. enemy->update(elapsedTime);
  170. if (enemy->hasTarget)
  171. {
  172. for (auto e : entities)
  173. {
  174. if (e->canCollide && e->inObject(enemy->position))
  175. {
  176. enemy->collide(e);
  177. break;
  178. }
  179. }
  180. }
  181. enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f;
  182. //tot hier
  183. }
  184. for(auto &crystal : crystals)
  185. {
  186. if (crystal->canCollide && crystal->inObject(player->position) && crystal->filled)
  187. {
  188. crystal->filled = false;
  189. player->crystals++;
  190. break;
  191. }
  192. }
  193. }
  194. void World::addLevelObject(LevelObject* obj)
  195. {
  196. entities.push_back(obj);
  197. }
  198. bool World::isPlayerPositionValid()
  199. {
  200. for (auto e : entities)
  201. {
  202. if (e->canCollide && e->inObject(player->position))
  203. return false;
  204. }
  205. return true;
  206. }