World.cpp 6.1 KB

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