World.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. for (auto e : v["crystals"])
  99. {
  100. //Rotation
  101. Vec3f rotation(0, 0, 0);
  102. if (!e["rot"].isNull())
  103. rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
  104. //Scale
  105. float scale = 1.0f;
  106. if (!e["scale"].isNull())
  107. scale = e["scale"].asFloat();
  108. //Position
  109. if (e["pos"].isNull())
  110. std::cout << "Invalid world file: enemies pos - " << fileName << "\n";
  111. //File
  112. if (e["file"].isNull())
  113. std::cout << "Invalid world file: enemies file - " << fileName << "\n";
  114. //Create
  115. Vec3f position(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
  116. position.y = getHeight(position.x, position.z) + 2.0f;
  117. crystals.push_back(new Crystal(e["file"].asString(), position, rotation, scale));
  118. }
  119. if (!v["world"]["music"].isNull())
  120. {
  121. music_id = CrystalPoint::GetSoundSystem().LoadSound(v["world"]["music"].asString().c_str(), true);
  122. Sound* music = CrystalPoint::GetSoundSystem().GetSound(music_id);
  123. music->SetPos(Vec3f(), Vec3f());
  124. music->Play();
  125. }
  126. }
  127. World::~World()
  128. {
  129. //delete heightmap;
  130. }
  131. std::pair<std::string, bool> World::getObjectFromValue(int val)
  132. {
  133. for (auto i : objecttemplates)
  134. {
  135. if (i.first == val)
  136. return i.second;
  137. }
  138. return objecttemplates[0].second;
  139. }
  140. float World::getHeight(float x, float y)
  141. {
  142. return heightmap->GetHeight(x, y);
  143. }
  144. void World::draw()
  145. {
  146. player->setCamera();
  147. float lightPosition[4] = { 0, 2, 1, 0 };
  148. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  149. float lightAmbient[4] = { 0.2, 0.2, 0.2, 1 };
  150. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  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. }