World.cpp 8.9 KB

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