World.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. Sound* music = CrystalPoint::GetSoundSystem().GetSound(music_id);
  150. music->SetPos(Vec3f(), Vec3f());
  151. music->Play();
  152. }
  153. if (!v["portal"].isNull())
  154. {
  155. Vec3f pos(0, 0, 0);
  156. if (!v["portal"]["pos"].isNull())
  157. pos = Vec3f(v["portal"]["pos"][0].asFloat(),
  158. v["portal"]["pos"][1].asFloat(),
  159. v["portal"]["pos"][0].asFloat());
  160. pos.y = getHeight(pos.x, pos.z);
  161. Vec3f rot(0, 0, 0);
  162. if (!v["portal"]["rot"].isNull())
  163. pos = Vec3f(v["portal"]["rot"][0].asFloat(),
  164. v["portal"]["rot"][1].asFloat(),
  165. v["portal"]["rot"][0].asFloat());
  166. float scale = 1.0f;
  167. if (!v["portal"]["scale"].isNull())
  168. scale = !v["portal"]["scale"].asFloat();
  169. portal = new Portal(v["portal"]["file"], pos, rot, scale);
  170. entities.push_back(portal);
  171. portal->maxCrystals = maxCrystals;
  172. }
  173. }
  174. World::~World()
  175. {
  176. delete heightmap;
  177. delete skybox;
  178. }
  179. std::pair<std::string, bool> World::getObjectFromValue(int val)
  180. {
  181. for (auto i : objecttemplates)
  182. {
  183. if (i.first == val)
  184. return i.second;
  185. }
  186. return objecttemplates[0].second;
  187. }
  188. float World::getHeight(float x, float y)
  189. {
  190. return heightmap->GetHeight(x, y);
  191. }
  192. void World::draw()
  193. {
  194. player->setCamera();
  195. float lightPosition[4] = { 0, 2, 1, 0 };
  196. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  197. float lightAmbient[4] = { 0.2, 0.2, 0.2, 1 };
  198. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  199. skybox->draw();
  200. heightmap->Draw();
  201. for (auto &enemy : enemies)
  202. enemy->draw();
  203. for (auto &entity : entities)
  204. entity->draw();
  205. interface->draw();
  206. }
  207. void World::update(float elapsedTime)
  208. {
  209. for (auto &entity : entities)
  210. entity->update(elapsedTime);
  211. int count = 0;
  212. int remove = false;
  213. for (auto &enemy : enemies)
  214. {
  215. //Al deze code zou in enemy moeten staan
  216. enemy->inEyeSight(player->position);
  217. enemy->update(elapsedTime);
  218. if (enemy->hasTarget)
  219. {
  220. for (auto e : entities)
  221. {
  222. if (e->canCollide && e->inObject(enemy->position))
  223. {
  224. enemy->collide(e);
  225. break;
  226. }
  227. }
  228. if (enemy->attack)
  229. {
  230. remove = true;
  231. continue;
  232. }
  233. }
  234. enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f;
  235. if(!remove)
  236. count++;
  237. }
  238. if(remove)
  239. enemies.erase(enemies.begin() + count);
  240. skybox->update(elapsedTime, maxEnemies - enemies.size(), maxEnemies);
  241. if (portal->mayEnter)
  242. WorldHandler::getInstance()->NextWorld();
  243. }
  244. void World::addLevelObject(LevelObject* obj)
  245. {
  246. entities.push_back(obj);
  247. }
  248. bool World::isPlayerPositionValid()
  249. {
  250. for (auto &e : entities)
  251. {
  252. if (e->canCollide && e->inObject(player->position))
  253. {
  254. e->collide();
  255. return false;
  256. }
  257. }
  258. return true;
  259. }