World.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. {
  12. nextworld = false;
  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["crystal"].isNull())
  35. std::cout << "Invalid world file: crystals - " << fileName << "\n";
  36. if (!v["world"]["loadingscreen"].isNull())
  37. {
  38. ls.setTexture(v["world"]["loadingscreen"].asString());
  39. ls.draw();
  40. }
  41. //Load object templates
  42. for (auto objt : v["world"]["object-templates"])
  43. {
  44. //collision
  45. bool cancollide = true;
  46. if (!objt["collision"].isNull())
  47. cancollide = objt["collision"].asBool();
  48. objecttemplates.push_back(std::pair<int, std::pair<std::string, bool>>(objt["color"], std::pair<std::string, bool>(objt["file"], cancollide)));
  49. }
  50. //Generate heightmap for this world
  51. heightmap = new HeightMap(v["world"]["heightmap"].asString(), this);
  52. //Load skybox
  53. skybox = new Skybox(15000.0f, v["world"]["skybox"].asString());
  54. skybox->init();
  55. //Map different texture to heightmap if available
  56. if(!v["world"]["texture"].isNull())
  57. heightmap->SetTexture(v["world"]["texture"].asString());
  58. //Set player starting position
  59. player->position.x = v["player"]["startposition"][0].asFloat();
  60. player->position.z = v["player"]["startposition"][2].asFloat();
  61. player->position.y = heightmap->GetHeight(player->position.x, player->position.z);
  62. //Load and place objects into world
  63. for (auto object : v["objects"])
  64. {
  65. //Collision
  66. bool hasCollision = true;
  67. if (!object["collide"].isNull())
  68. hasCollision = object["collide"].asBool();
  69. //Rotation
  70. Vec3f rotation(0, 0, 0);
  71. if(!object["rot"].isNull())
  72. rotation = Vec3f(object["rot"][0].asFloat(), object["rot"][1].asFloat(), object["rot"][2].asFloat());
  73. //Scale
  74. float scale = 1;
  75. if (!object["scale"].isNull())
  76. scale = object["scale"].asFloat();
  77. //Position
  78. if (object["pos"].isNull())
  79. std::cout << "Invalid world file: objects pos - " << fileName << "\n";
  80. //File
  81. if (object["file"].isNull())
  82. std::cout << "Invalid world file: objects file - " << fileName << "\n";
  83. //Create
  84. Vec3f position(object["pos"][0].asFloat(), object["pos"][1].asFloat(), object["pos"][2].asFloat());
  85. position.y = getHeight(position.x, position.z);
  86. entities.push_back(new LevelObject(object["file"].asString(), position, rotation, scale, hasCollision));
  87. }
  88. maxEnemies = 0;
  89. //Load and place enemies into world
  90. for (auto e : v["enemies"])
  91. {
  92. //Rotation
  93. Vec3f rotation(0, 0, 0);
  94. if (!e["rot"].isNull())
  95. rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
  96. //Scale
  97. float scale = 1.0f;
  98. if (!e["scale"].isNull())
  99. scale = e["scale"].asFloat();
  100. //Position
  101. if (e["pos"].isNull())
  102. std::cout << "Invalid world file: enemies pos - " << fileName << "\n";
  103. //File
  104. if (e["file"].isNull())
  105. std::cout << "Invalid world file: enemies file - " << fileName << "\n";
  106. //Music
  107. if (e["music"].isNull())
  108. std::cout << "Invalid world file: enemies music - " << fileName << "\n";
  109. //Damage
  110. if (e["damage"].isNull())
  111. std::cout << "Invalid world file: enemies damage - " << fileName << "\n";
  112. //Health
  113. if (e["health"].isNull())
  114. std::cout << "Invalid world file: enemies health - " << fileName << "\n";
  115. //Create
  116. Vec3f position(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
  117. position.y = getHeight(position.x, position.z) + 2.0f;
  118. maxEnemies++;
  119. enemies.push_back(new Enemy(e["file"].asString(), e["music"].asString(), e["damage"].asFloat(), e["health"].asFloat(), position, rotation, scale));
  120. }
  121. maxCrystals = 0;
  122. if (!v["crystal"].isNull())
  123. {
  124. std::string filled = "unknown";
  125. std::string empty = "unknown";
  126. if(!v["crystal"]["full texture"].isNull())
  127. filled = v["crystal"]["full texture"].asString();
  128. if(!v["crystal"]["empty texture"].isNull())
  129. empty = v["crystal"]["empty texture"].asString();
  130. if (!v["crystal"]["instances"].isNull())
  131. {
  132. for (auto instance : v["crystal"]["instances"])
  133. {
  134. Vec3f position(0, 0, 0);
  135. Vec3f rotation(0, 0, 0);
  136. float scale = 1.0f;
  137. if (!instance["pos"].isNull())
  138. {
  139. position.x = instance["pos"][0];
  140. position.y = instance["pos"][1];
  141. position.z = instance["pos"][2];
  142. }
  143. if (!instance["rot"].isNull())
  144. {
  145. rotation.x = instance["rot"][0];
  146. rotation.y = instance["rot"][1];
  147. rotation.z = instance["rot"][2];
  148. }
  149. if (!instance["scale"].isNull())
  150. scale = instance["scale"].asFloat();
  151. position.y = getHeight(position.x, position.z);
  152. maxCrystals++;
  153. Crystal *c = new Crystal(filled, empty, position, rotation, scale);
  154. entities.push_back(c);
  155. }
  156. interface->maxCrystals = maxCrystals;
  157. }
  158. }
  159. if (!v["world"]["music"].isNull())
  160. {
  161. sound_id = CrystalPoint::GetSoundSystem().LoadSound(v["world"]["music"].asString().c_str(), true);
  162. music = CrystalPoint::GetSoundSystem().GetSound(sound_id);
  163. }
  164. if (!v["portal"].isNull())
  165. {
  166. Vec3f pos(0, 0, 0);
  167. if (!v["portal"]["pos"].isNull())
  168. pos = Vec3f(v["portal"]["pos"][0].asFloat(),
  169. v["portal"]["pos"][1].asFloat(),
  170. v["portal"]["pos"][2].asFloat());
  171. pos.y = getHeight(pos.x, pos.z);
  172. Vec3f rot(0, 0, 0);
  173. if (!v["portal"]["rot"].isNull())
  174. pos = Vec3f(v["portal"]["rot"][0].asFloat(),
  175. v["portal"]["rot"][1].asFloat(),
  176. v["portal"]["rot"][2].asFloat());
  177. float scale = 1.0f;
  178. if (!v["portal"]["scale"].isNull())
  179. scale = !v["portal"]["scale"].asFloat();
  180. portal = new Portal(v["portal"]["file"], pos, rot, scale);
  181. entities.push_back(portal);
  182. portal->maxCrystals = maxCrystals;
  183. }
  184. }
  185. World::~World()
  186. {
  187. delete heightmap;
  188. music->Stop();
  189. CrystalPoint::GetSoundSystem().UnloadSound(sound_id);
  190. delete skybox;
  191. delete portal;
  192. }
  193. std::pair<std::string, bool> World::getObjectFromValue(int val)
  194. {
  195. for (auto i : objecttemplates)
  196. {
  197. if (i.first == val)
  198. return i.second;
  199. }
  200. return objecttemplates[0].second;
  201. }
  202. float World::getHeight(float x, float y)
  203. {
  204. return heightmap->GetHeight(x, y);
  205. }
  206. void World::draw()
  207. {
  208. player->setCamera();
  209. float lightPosition[4] = { 0, 2, 1, 0 };
  210. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  211. GLfloat lightAmbient[] = { 0.05, 0.05, 0.05, 0 };
  212. GLfloat light_diffuse[] = { 0.9, 0.9, 0.9, 0 };
  213. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  214. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  215. GLfloat mat_specular[] = { 0.15, 0.15, 0.15, 0 };
  216. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  217. skybox->draw();
  218. player->draw();
  219. heightmap->Draw();
  220. for (auto &enemy : enemies)
  221. enemy->draw();
  222. for (auto &entity : entities)
  223. entity->draw();
  224. interface->draw();
  225. }
  226. void World::update(float elapsedTime)
  227. {
  228. if (nextworld)
  229. {
  230. WorldHandler::getInstance()->NextWorld();
  231. return;
  232. }
  233. music->SetPos(player->position, Vec3f());
  234. if (music->IsPlaying() == false)
  235. {
  236. music->Play();
  237. }
  238. for (auto &entity : entities)
  239. entity->update(elapsedTime);
  240. int count = 0;
  241. int remove = false;
  242. for (auto &enemy : enemies)
  243. {
  244. //Al deze code zou in enemy moeten staan
  245. enemy->inEyeSight(player->position);
  246. enemy->update(elapsedTime);
  247. if (enemy->hasTarget)
  248. {
  249. for (auto e : entities)
  250. {
  251. if (e->canCollide && e->inObject(enemy->position))
  252. {
  253. enemy->collide(e);
  254. break;
  255. }
  256. }
  257. }
  258. if (enemy->attack)
  259. {
  260. player->HpDown(enemy->damage);
  261. continue;
  262. }
  263. enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f;
  264. if(!remove)
  265. count++;
  266. }
  267. if (remove)
  268. {
  269. player->XpUp(enemies[count]->xp);
  270. delete enemies[count];
  271. enemies.erase(enemies.begin() + count);
  272. player->HpUp(10);
  273. }
  274. skybox->update(elapsedTime, maxEnemies - enemies.size(), maxEnemies);
  275. if (portal->mayEnter)
  276. {
  277. if (portal->enter(elapsedTime))
  278. nextworld = true;
  279. }
  280. }
  281. void World::addLevelObject(LevelObject* obj)
  282. {
  283. entities.push_back(obj);
  284. }
  285. bool World::isPlayerPositionValid()
  286. {
  287. for (auto &e : entities)
  288. {
  289. if (e->canCollide && e->inObject(player->position))
  290. {
  291. e->collide();
  292. return false;
  293. }
  294. }
  295. return true;
  296. }