World.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. if (!v["crystal"].isNull())
  98. {
  99. std::string filled = "unknown";
  100. std::string empty = "unknown";
  101. if(!v["crystal"]["full texture"].isNull())
  102. filled = v["crystal"]["full texture"].asString();
  103. if(!v["crystal"]["empty texture"].isNull())
  104. empty = v["crystal"]["empty texture"].asString();
  105. if (!v["crystal"]["instances"].isNull())
  106. {
  107. for (auto instance : v["crystal"]["instances"])
  108. {
  109. Vec3f position(0, 0, 0);
  110. Vec3f rotation(0, 0, 0);
  111. float scale = 1.0f;
  112. if (!instance["pos"].isNull())
  113. {
  114. position.x = instance["pos"][0];
  115. position.y = instance["pos"][1];
  116. position.z = instance["pos"][2];
  117. }
  118. if (!instance["rot"].isNull())
  119. {
  120. rotation.x = instance["rot"][0];
  121. rotation.y = instance["rot"][1];
  122. rotation.z = instance["rot"][2];
  123. }
  124. if (!instance["scale"].isNull())
  125. scale = instance["scale"].asFloat();
  126. position.y = getHeight(position.x, position.z);
  127. Crystal *c = new Crystal(filled, empty, position, rotation, scale);
  128. crystals.push_back(c);
  129. //entities.push_back(c);
  130. }
  131. }
  132. }
  133. }
  134. World::~World()
  135. {
  136. //delete heightmap;
  137. }
  138. std::pair<std::string, bool> World::getObjectFromValue(int val)
  139. {
  140. for (auto i : objecttemplates)
  141. {
  142. if (i.first == val)
  143. return i.second;
  144. }
  145. return objecttemplates[0].second;
  146. }
  147. float World::getHeight(float x, float y)
  148. {
  149. return heightmap->GetHeight(x, y);
  150. }
  151. void World::draw()
  152. {
  153. player->setCamera();
  154. float lightPosition[4] = { 0, 2, 1, 0 };
  155. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  156. float lightAmbient[4] = { 0.2, 0.2, 0.2, 1 };
  157. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  158. heightmap->Draw();
  159. for (auto &enemy : enemies)
  160. enemy->draw();
  161. for (auto &entity : entities)
  162. entity->draw();
  163. for (auto &crystal : crystals)
  164. crystal->draw();
  165. interface->draw();
  166. }
  167. void World::update(float elapsedTime)
  168. {
  169. for (auto &entity : entities)
  170. entity->update(elapsedTime);
  171. for (auto &enemy : enemies)
  172. {
  173. //Al deze code zou in enemy moeten staan
  174. enemy->inEyeSight(player->position);
  175. enemy->update(elapsedTime);
  176. if (enemy->hasTarget)
  177. {
  178. for (auto e : entities)
  179. {
  180. if (e->canCollide && e->inObject(enemy->position))
  181. {
  182. enemy->collide(e);
  183. break;
  184. }
  185. }
  186. }
  187. enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f;
  188. }
  189. }
  190. void World::addLevelObject(LevelObject* obj)
  191. {
  192. entities.push_back(obj);
  193. }
  194. bool World::isPlayerPositionValid()
  195. {
  196. for (auto &e : entities)
  197. {
  198. if (e->canCollide && e->inObject(player->position))
  199. return false;
  200. }
  201. for (auto & c : crystals)
  202. {
  203. if (c->canCollide && c->inObject(player->position))
  204. {
  205. if (c->isFilled)
  206. {
  207. c->pickUp();
  208. player->crystals++;
  209. }
  210. return false;
  211. }
  212. }
  213. return true;
  214. }