World.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. World::World(const std::string &fileName):
  10. music_id(-1)
  11. {
  12. //Store player instance
  13. player = Player::getInstance();
  14. //Create the interface
  15. interface = new Interface();
  16. //Open world json file
  17. std::ifstream file(fileName);
  18. if(!file.is_open())
  19. std::cout<<"Error, can't open world file - " << fileName << "\n";
  20. json::Value v = json::readJson(file);
  21. file.close();
  22. //Check file
  23. if(v["world"].isNull() || v["world"]["heightmap"].isNull())
  24. std::cout << "Invalid world file: world - " << fileName << "\n";
  25. if (v["player"].isNull() || v["player"]["startposition"].isNull())
  26. std::cout << "Invalid world file: player - " << fileName << "\n";
  27. if (v["objects"].isNull())
  28. std::cout << "Invalid world file: objects - " << fileName << "\n";
  29. if (v["world"]["object-templates"].isNull())
  30. std::cout << "Invalid world file: object templates - " << fileName << "\n";
  31. //Load object templates
  32. for (auto objt : v["world"]["object-templates"])
  33. {
  34. //collision
  35. bool cancollide = true;
  36. if (!objt["collision"].isNull())
  37. cancollide = objt["collision"].asBool();
  38. objecttemplates.push_back(std::pair<int, std::pair<std::string, bool>>(objt["color"], std::pair<std::string, bool>(objt["file"], cancollide)));
  39. }
  40. //Generate heightmap for this world
  41. heightmap = new HeightMap(v["world"]["heightmap"].asString(), this);
  42. //Map different texture to heightmap if available
  43. if(!v["world"]["texture"].isNull())
  44. heightmap->SetTexture(v["world"]["texture"].asString());
  45. //Set player starting position
  46. player->position.x = v["player"]["startposition"][0].asFloat();
  47. player->position.y = v["player"]["startposition"][1].asFloat();
  48. player->position.z = v["player"]["startposition"][2].asFloat();
  49. //Load and place objects into world
  50. for (auto object : v["objects"])
  51. {
  52. //Collision
  53. bool hasCollision = true;
  54. if (!object["collide"].isNull())
  55. hasCollision = object["collide"].asBool();
  56. //Rotation
  57. Vec3f rotation(0, 0, 0);
  58. if(!object["rot"].isNull())
  59. rotation = Vec3f(object["rot"][0].asFloat(), object["rot"][1].asFloat(), object["rot"][2].asFloat());
  60. //Scale
  61. float scale = 1;
  62. if (!object["scale"].isNull())
  63. scale = object["scale"].asFloat();
  64. //Position
  65. if (object["pos"].isNull())
  66. std::cout << "Invalid world file: objects pos - " << fileName << "\n";
  67. //File
  68. if (object["file"].isNull())
  69. std::cout << "Invalid world file: objects file - " << fileName << "\n";
  70. //Create
  71. Vec3f position(object["pos"][0].asFloat(), object["pos"][1].asFloat(), object["pos"][2].asFloat());
  72. entities.push_back(new LevelObject(object["file"].asString(), position, rotation, scale, hasCollision));
  73. }
  74. //Load and place enemies into world
  75. for (auto e : v["enemies"])
  76. {
  77. //Rotation
  78. Vec3f rotation(0, 0, 0);
  79. if (!e["rot"].isNull())
  80. rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
  81. //Scale
  82. float scale = 1.0f;
  83. if (!e["scale"].isNull())
  84. scale = e["scale"].asFloat();
  85. //Position
  86. if (e["pos"].isNull())
  87. std::cout << "Invalid world file: enemies pos - " << fileName << "\n";
  88. //File
  89. if (e["file"].isNull())
  90. std::cout << "Invalid world file: enemies file - " << fileName << "\n";
  91. //Create
  92. Vec3f position(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
  93. enemies.push_back(new Enemy(e["file"].asString(), position, rotation, scale));
  94. }
  95. if (!v["world"]["music"].isNull())
  96. {
  97. music_id = CrystalPoint::GetSoundSystem().LoadSound(v["world"]["music"].asString().c_str(), true);
  98. Sound* music = CrystalPoint::GetSoundSystem().GetSound(music_id);
  99. music->SetPos(Vec3f(), Vec3f());
  100. music->Play();
  101. }
  102. }
  103. World::~World()
  104. {
  105. //delete heightmap;
  106. }
  107. std::pair<std::string, bool> World::getObjectFromValue(int val)
  108. {
  109. for (auto i : objecttemplates)
  110. {
  111. if (i.first == val)
  112. return i.second;
  113. }
  114. return objecttemplates[0].second;
  115. }
  116. float World::getHeight(float x, float y)
  117. {
  118. return heightmap->GetHeight(x, y);
  119. }
  120. void World::draw()
  121. {
  122. player->setCamera();
  123. float lightPosition[4] = { 0, 2, 1, 0 };
  124. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  125. float lightAmbient[4] = { 0.2, 0.2, 0.2, 1 };
  126. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  127. heightmap->Draw();
  128. for (auto &enemy : enemies)
  129. enemy->draw();
  130. for (auto &entity : entities)
  131. entity->draw();
  132. interface->draw();
  133. }
  134. void World::update(float elapsedTime)
  135. {
  136. for (auto &entity : entities)
  137. entity->update(elapsedTime);
  138. for (auto &enemy : enemies)
  139. {
  140. //Al deze code zou in enemy moeten staan
  141. enemy->inEyeSight(player->position);
  142. enemy->update(elapsedTime);
  143. if (enemy->hasTarget)
  144. {
  145. for (auto e : entities)
  146. {
  147. if (e->canCollide && e->inObject(enemy->position))
  148. {
  149. Vec3f difference = e->position - enemy->position; //zou misschien omgedraait moeten worden
  150. difference.Normalize();
  151. difference = difference * (e->model->radius + 0.01f);
  152. enemy->position = e->position + difference;
  153. break;
  154. }
  155. }
  156. }
  157. //tot hier
  158. }
  159. }
  160. void World::addLevelObject(LevelObject* obj)
  161. {
  162. entities.push_back(obj);
  163. }
  164. bool World::isPlayerPositionValid()
  165. {
  166. for (auto e : entities)
  167. {
  168. if (e->canCollide && e->inObject(player->position))
  169. return false;
  170. }
  171. return true;
  172. }