World.cpp 9.1 KB

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