World.cpp 6.3 KB

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