World.cpp 7.8 KB

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