World.cpp 6.0 KB

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