World.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. nextworld = false;
  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. //Music
  103. if (e["music"].isNull())
  104. std::cout << "Invalid world file: enemies music - " << fileName << "\n";
  105. //Create
  106. Vec3f position(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
  107. position.y = getHeight(position.x, position.z) + 2.0f;
  108. maxEnemies++;
  109. enemies.push_back(new Enemy(e["file"].asString(), e["music"].asString(), position, rotation, scale));
  110. }
  111. maxCrystals = 0;
  112. if (!v["crystal"].isNull())
  113. {
  114. std::string filled = "unknown";
  115. std::string empty = "unknown";
  116. if(!v["crystal"]["full texture"].isNull())
  117. filled = v["crystal"]["full texture"].asString();
  118. if(!v["crystal"]["empty texture"].isNull())
  119. empty = v["crystal"]["empty texture"].asString();
  120. if (!v["crystal"]["instances"].isNull())
  121. {
  122. for (auto instance : v["crystal"]["instances"])
  123. {
  124. Vec3f position(0, 0, 0);
  125. Vec3f rotation(0, 0, 0);
  126. float scale = 1.0f;
  127. if (!instance["pos"].isNull())
  128. {
  129. position.x = instance["pos"][0];
  130. position.y = instance["pos"][1];
  131. position.z = instance["pos"][2];
  132. }
  133. if (!instance["rot"].isNull())
  134. {
  135. rotation.x = instance["rot"][0];
  136. rotation.y = instance["rot"][1];
  137. rotation.z = instance["rot"][2];
  138. }
  139. if (!instance["scale"].isNull())
  140. scale = instance["scale"].asFloat();
  141. position.y = getHeight(position.x, position.z);
  142. maxCrystals++;
  143. Crystal *c = new Crystal(filled, empty, position, rotation, scale);
  144. entities.push_back(c);
  145. }
  146. interface->maxCrystals = maxCrystals;
  147. }
  148. }
  149. if (!v["world"]["music"].isNull())
  150. {
  151. music_id = CrystalPoint::GetSoundSystem().LoadSound(v["world"]["music"].asString().c_str(), true);
  152. music = CrystalPoint::GetSoundSystem().GetSound(music_id);
  153. }
  154. if (!v["portal"].isNull())
  155. {
  156. Vec3f pos(0, 0, 0);
  157. if (!v["portal"]["pos"].isNull())
  158. pos = Vec3f(v["portal"]["pos"][0].asFloat(),
  159. v["portal"]["pos"][1].asFloat(),
  160. v["portal"]["pos"][0].asFloat());
  161. pos.y = getHeight(pos.x, pos.z);
  162. Vec3f rot(0, 0, 0);
  163. if (!v["portal"]["rot"].isNull())
  164. pos = Vec3f(v["portal"]["rot"][0].asFloat(),
  165. v["portal"]["rot"][1].asFloat(),
  166. v["portal"]["rot"][0].asFloat());
  167. float scale = 1.0f;
  168. if (!v["portal"]["scale"].isNull())
  169. scale = !v["portal"]["scale"].asFloat();
  170. portal = new Portal(v["portal"]["file"], pos, rot, scale);
  171. entities.push_back(portal);
  172. portal->maxCrystals = maxCrystals;
  173. }
  174. }
  175. World::~World()
  176. {
  177. delete heightmap;
  178. music->Stop();
  179. delete music;
  180. delete skybox;
  181. delete portal;
  182. }
  183. std::pair<std::string, bool> World::getObjectFromValue(int val)
  184. {
  185. for (auto i : objecttemplates)
  186. {
  187. if (i.first == val)
  188. return i.second;
  189. }
  190. return objecttemplates[0].second;
  191. }
  192. float World::getHeight(float x, float y)
  193. {
  194. return heightmap->GetHeight(x, y);
  195. }
  196. void World::draw()
  197. {
  198. float lightPosition[4] = { 0, 2, 1, 0 };
  199. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  200. GLfloat lightAmbient[] = { 0.05, 0.05, 0.05, 0 };
  201. GLfloat light_diffuse[] = { 0.9, 0.9, 0.9, 0 };
  202. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  203. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  204. skybox->draw();
  205. player->setCamera();
  206. player->draw();
  207. heightmap->Draw();
  208. for (auto &enemy : enemies)
  209. enemy->draw();
  210. for (auto &entity : entities)
  211. entity->draw();
  212. interface->draw();
  213. }
  214. void World::update(float elapsedTime)
  215. {
  216. if (nextworld)
  217. {
  218. WorldHandler::getInstance()->NextWorld();
  219. return;
  220. }
  221. music->SetPos(player->position, Vec3f());
  222. if (music->IsPlaying() == false)
  223. {
  224. music->Play();
  225. }
  226. for (auto &entity : entities)
  227. entity->update(elapsedTime);
  228. int count = 0;
  229. int remove = false;
  230. for (auto &enemy : enemies)
  231. {
  232. //Al deze code zou in enemy moeten staan
  233. enemy->inEyeSight(player->position);
  234. enemy->update(elapsedTime);
  235. if (enemy->hasTarget)
  236. {
  237. for (auto e : entities)
  238. {
  239. if (e->canCollide && e->inObject(enemy->position))
  240. {
  241. enemy->collide(e);
  242. break;
  243. }
  244. }
  245. if (enemy->attack)
  246. {
  247. remove = true;
  248. continue;
  249. }
  250. }
  251. enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f;
  252. if(!remove)
  253. count++;
  254. }
  255. if (remove)
  256. {
  257. player->XpUp(enemies[count]->xp);
  258. enemies.erase(enemies.begin() + count);
  259. player->HpUp(10);
  260. }
  261. skybox->update(elapsedTime, maxEnemies - enemies.size(), maxEnemies);
  262. if (portal->mayEnter)
  263. {
  264. if (portal->enter(elapsedTime))
  265. nextworld = true;
  266. }
  267. }
  268. void World::addLevelObject(LevelObject* obj)
  269. {
  270. entities.push_back(obj);
  271. }
  272. bool World::isPlayerPositionValid()
  273. {
  274. for (auto &e : entities)
  275. {
  276. if (e->canCollide && e->inObject(player->position))
  277. {
  278. e->collide();
  279. return false;
  280. }
  281. }
  282. return true;
  283. }