World.cpp 6.6 KB

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