World.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "World.h"
  2. #include <GL/freeglut.h>
  3. #include "Entity.h"
  4. #include "json.h"
  5. #include "Model.h"
  6. #include "CrystalPoint.h"
  7. #include <fstream>
  8. #include <iostream>
  9. #include "WorldHandler.h"
  10. World::World(const std::string &fileName):
  11. music_id(-1)
  12. {
  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["crystals"].isNull())
  35. std::cout << "Invalid world file: crystals - " << fileName << "\n";
  36. //Load object templates
  37. for (auto objt : v["world"]["object-templates"])
  38. {
  39. //collision
  40. bool cancollide = true;
  41. if (!objt["collision"].isNull())
  42. cancollide = objt["collision"].asBool();
  43. objecttemplates.push_back(std::pair<int, std::pair<std::string, bool>>(objt["color"], std::pair<std::string, bool>(objt["file"], cancollide)));
  44. }
  45. //Generate heightmap for this world
  46. heightmap = new HeightMap(v["world"]["heightmap"].asString(), this);
  47. //Load skybox
  48. skybox = new Skybox(15000.0f, v["world"]["skybox"].asString());
  49. skybox->init();
  50. //Map different texture to heightmap if available
  51. if(!v["world"]["texture"].isNull())
  52. heightmap->SetTexture(v["world"]["texture"].asString());
  53. //Set player starting position
  54. player->position.x = v["player"]["startposition"][0].asFloat();
  55. player->position.y = v["player"]["startposition"][1].asFloat();
  56. player->position.z = v["player"]["startposition"][2].asFloat();
  57. //Load and place objects into world
  58. for (auto object : v["objects"])
  59. {
  60. //Collision
  61. bool hasCollision = true;
  62. if (!object["collide"].isNull())
  63. hasCollision = object["collide"].asBool();
  64. //Rotation
  65. Vec3f rotation(0, 0, 0);
  66. if(!object["rot"].isNull())
  67. rotation = Vec3f(object["rot"][0].asFloat(), object["rot"][1].asFloat(), object["rot"][2].asFloat());
  68. //Scale
  69. float scale = 1;
  70. if (!object["scale"].isNull())
  71. scale = object["scale"].asFloat();
  72. //Position
  73. if (object["pos"].isNull())
  74. std::cout << "Invalid world file: objects pos - " << fileName << "\n";
  75. //File
  76. if (object["file"].isNull())
  77. std::cout << "Invalid world file: objects file - " << fileName << "\n";
  78. //Create
  79. Vec3f position(object["pos"][0].asFloat(), object["pos"][1].asFloat(), object["pos"][2].asFloat());
  80. position.y = getHeight(position.x, position.z);
  81. entities.push_back(new LevelObject(object["file"].asString(), position, rotation, scale, hasCollision));
  82. }
  83. //Load and place enemies into world
  84. for (auto e : v["enemies"])
  85. {
  86. //Rotation
  87. Vec3f rotation(0, 0, 0);
  88. if (!e["rot"].isNull())
  89. rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
  90. //Scale
  91. float scale = 1.0f;
  92. if (!e["scale"].isNull())
  93. scale = e["scale"].asFloat();
  94. //Position
  95. if (e["pos"].isNull())
  96. std::cout << "Invalid world file: enemies pos - " << fileName << "\n";
  97. //File
  98. if (e["file"].isNull())
  99. std::cout << "Invalid world file: enemies file - " << fileName << "\n";
  100. //Create
  101. Vec3f position(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
  102. position.y = getHeight(position.x, position.z) + 2.0f;
  103. enemies.push_back(new Enemy(e["file"].asString(), position, rotation, scale));
  104. }
  105. maxCrystals = 0;
  106. if (!v["crystal"].isNull())
  107. {
  108. std::string filled = "unknown";
  109. std::string empty = "unknown";
  110. if(!v["crystal"]["full texture"].isNull())
  111. filled = v["crystal"]["full texture"].asString();
  112. if(!v["crystal"]["empty texture"].isNull())
  113. empty = v["crystal"]["empty texture"].asString();
  114. if (!v["crystal"]["instances"].isNull())
  115. {
  116. for (auto instance : v["crystal"]["instances"])
  117. {
  118. Vec3f position(0, 0, 0);
  119. Vec3f rotation(0, 0, 0);
  120. float scale = 1.0f;
  121. if (!instance["pos"].isNull())
  122. {
  123. position.x = instance["pos"][0];
  124. position.y = instance["pos"][1];
  125. position.z = instance["pos"][2];
  126. }
  127. if (!instance["rot"].isNull())
  128. {
  129. rotation.x = instance["rot"][0];
  130. rotation.y = instance["rot"][1];
  131. rotation.z = instance["rot"][2];
  132. }
  133. if (!instance["scale"].isNull())
  134. scale = instance["scale"].asFloat();
  135. position.y = getHeight(position.x, position.z);
  136. maxCrystals++;
  137. Crystal *c = new Crystal(filled, empty, position, rotation, scale);
  138. entities.push_back(c);
  139. }
  140. interface->maxCrystals = maxCrystals;
  141. }
  142. }
  143. if (!v["world"]["music"].isNull())
  144. {
  145. music_id = CrystalPoint::GetSoundSystem().LoadSound(v["world"]["music"].asString().c_str(), true);
  146. Sound* music = CrystalPoint::GetSoundSystem().GetSound(music_id);
  147. music->SetPos(Vec3f(), Vec3f());
  148. music->Play();
  149. }
  150. if (!v["portal"].isNull())
  151. {
  152. Vec3f pos(0, 0, 0);
  153. if (!v["portal"]["pos"].isNull())
  154. pos = Vec3f(v["portal"]["pos"][0].asFloat(),
  155. v["portal"]["pos"][1].asFloat(),
  156. v["portal"]["pos"][0].asFloat());
  157. pos.y = getHeight(pos.x, pos.z);
  158. Vec3f rot(0, 0, 0);
  159. if (!v["portal"]["rot"].isNull())
  160. pos = Vec3f(v["portal"]["rot"][0].asFloat(),
  161. v["portal"]["rot"][1].asFloat(),
  162. v["portal"]["rot"][0].asFloat());
  163. float scale = 1.0f;
  164. if (!v["portal"]["scale"].isNull())
  165. scale = !v["portal"]["scale"].asFloat();
  166. portal = new Portal(v["portal"]["file"], pos, rot, scale);
  167. entities.push_back(portal);
  168. portal->maxCrystals = maxCrystals;
  169. }
  170. }
  171. World::~World()
  172. {
  173. delete heightmap;
  174. delete skybox;
  175. }
  176. std::pair<std::string, bool> World::getObjectFromValue(int val)
  177. {
  178. for (auto i : objecttemplates)
  179. {
  180. if (i.first == val)
  181. return i.second;
  182. }
  183. return objecttemplates[0].second;
  184. }
  185. float World::getHeight(float x, float y)
  186. {
  187. return heightmap->GetHeight(x, y);
  188. }
  189. void World::draw()
  190. {
  191. player->setCamera();
  192. float lightPosition[4] = { 0, 2, 1, 0 };
  193. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  194. float lightAmbient[4] = { 0.2, 0.2, 0.2, 1 };
  195. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  196. glColor4f(1, 1, 1, 1);
  197. skybox->draw();
  198. heightmap->Draw();
  199. for (auto &enemy : enemies)
  200. enemy->draw();
  201. for (auto &entity : entities)
  202. entity->draw();
  203. interface->draw();
  204. }
  205. void World::update(float elapsedTime)
  206. {
  207. for (auto &entity : entities)
  208. entity->update(elapsedTime);
  209. for (auto &enemy : enemies)
  210. {
  211. //Al deze code zou in enemy moeten staan
  212. enemy->inEyeSight(player->position);
  213. enemy->update(elapsedTime);
  214. if (enemy->hasTarget)
  215. {
  216. for (auto e : entities)
  217. {
  218. if (e->canCollide && e->inObject(enemy->position))
  219. {
  220. enemy->collide(e);
  221. break;
  222. }
  223. }
  224. }
  225. enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f;
  226. }
  227. if (portal->mayEnter)
  228. WorldHandler::getInstance()->NextWorld();
  229. }
  230. void World::addLevelObject(LevelObject* obj)
  231. {
  232. entities.push_back(obj);
  233. }
  234. bool World::isPlayerPositionValid()
  235. {
  236. for (auto &e : entities)
  237. {
  238. if (e->canCollide && e->inObject(player->position))
  239. {
  240. e->collide();
  241. return false;
  242. }
  243. }
  244. return true;
  245. }