World.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "World.h"
  2. #include <GL/freeglut.h>
  3. #include "Entity.h"
  4. #include "json.h"
  5. #include "Model.h"
  6. #include <fstream>
  7. #include <iostream>
  8. #include <algorithm>
  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);
  81. entities.push_back(new LevelObject(object["file"].asString(), position, rotation, scale, hasCollision));
  82. }
  83. maxEnemies = 0;
  84. //Load and place enemies into world
  85. for (auto e : v["enemies"])
  86. {
  87. //Rotation
  88. Vec3f rotation(0, 0, 0);
  89. if (!e["rot"].isNull())
  90. rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
  91. //Scale
  92. float scale = 1.0f;
  93. if (!e["scale"].isNull())
  94. scale = e["scale"].asFloat();
  95. //Position
  96. if (e["pos"].isNull())
  97. std::cout << "Invalid world file: enemies pos - " << fileName << "\n";
  98. //File
  99. if (e["file"].isNull())
  100. std::cout << "Invalid world file: enemies file - " << fileName << "\n";
  101. //Create
  102. Vec3f position(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
  103. position.y = getHeight(position.x, position.z) + 2.0f;
  104. maxEnemies++;
  105. enemies.push_back(new Enemy(e["file"].asString(), position, rotation, scale));
  106. }
  107. maxCrystals = 0;
  108. if (!v["crystal"].isNull())
  109. {
  110. std::string filled = "unknown";
  111. std::string empty = "unknown";
  112. if(!v["crystal"]["full texture"].isNull())
  113. filled = v["crystal"]["full texture"].asString();
  114. if(!v["crystal"]["empty texture"].isNull())
  115. empty = v["crystal"]["empty texture"].asString();
  116. if (!v["crystal"]["instances"].isNull())
  117. {
  118. for (auto instance : v["crystal"]["instances"])
  119. {
  120. Vec3f position(0, 0, 0);
  121. Vec3f rotation(0, 0, 0);
  122. float scale = 1.0f;
  123. if (!instance["pos"].isNull())
  124. {
  125. position.x = instance["pos"][0];
  126. position.y = instance["pos"][1];
  127. position.z = instance["pos"][2];
  128. }
  129. if (!instance["rot"].isNull())
  130. {
  131. rotation.x = instance["rot"][0];
  132. rotation.y = instance["rot"][1];
  133. rotation.z = instance["rot"][2];
  134. }
  135. if (!instance["scale"].isNull())
  136. scale = instance["scale"].asFloat();
  137. position.y = getHeight(position.x, position.z);
  138. maxCrystals++;
  139. Crystal *c = new Crystal(filled, empty, position, rotation, scale);
  140. entities.push_back(c);
  141. }
  142. interface->maxCrystals = maxCrystals;
  143. }
  144. }
  145. if (!v["world"]["music"].isNull())
  146. {
  147. music_id = CrystalPoint::GetSoundSystem().LoadSound(v["world"]["music"].asString().c_str(), true);
  148. music = CrystalPoint::GetSoundSystem().GetSound(music_id);
  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. music->Stop();
  175. delete music;
  176. delete skybox;
  177. delete portal;
  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. music->SetPos(player->position, Vec3f());
  210. if (music->IsPlaying() == false)
  211. {
  212. music->Play();
  213. }
  214. for (auto &entity : entities)
  215. entity->update(elapsedTime);
  216. int count = 0;
  217. int remove = false;
  218. for (auto &enemy : enemies)
  219. {
  220. //Al deze code zou in enemy moeten staan
  221. enemy->inEyeSight(player->position);
  222. enemy->update(elapsedTime);
  223. if (enemy->hasTarget)
  224. {
  225. for (auto e : entities)
  226. {
  227. if (e->canCollide && e->inObject(enemy->position))
  228. {
  229. enemy->collide(e);
  230. break;
  231. }
  232. }
  233. if (enemy->attack)
  234. {
  235. remove = true;
  236. continue;
  237. }
  238. }
  239. enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f;
  240. if(!remove)
  241. count++;
  242. }
  243. if(remove)
  244. enemies.erase(enemies.begin() + count);
  245. skybox->update(elapsedTime, maxEnemies - enemies.size(), maxEnemies);
  246. if (portal->mayEnter)
  247. WorldHandler::getInstance()->NextWorld();
  248. }
  249. void World::addLevelObject(LevelObject* obj)
  250. {
  251. entities.push_back(obj);
  252. }
  253. bool World::isPlayerPositionValid()
  254. {
  255. for (auto &e : entities)
  256. {
  257. if (e->canCollide && e->inObject(player->position))
  258. {
  259. e->collide();
  260. return false;
  261. }
  262. }
  263. return true;
  264. }