World.cpp 8.8 KB

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