World.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. {
  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. }
  96. World::~World()
  97. {
  98. //delete heightmap;
  99. }
  100. std::pair<std::string, bool> World::getObjectFromValue(int val)
  101. {
  102. for (auto i : objecttemplates)
  103. {
  104. if (i.first == val)
  105. return i.second;
  106. }
  107. return objecttemplates[0].second;
  108. }
  109. float World::getHeight(float x, float y)
  110. {
  111. return heightmap->GetHeight(x, y);
  112. }
  113. void World::draw()
  114. {
  115. player->setCamera();
  116. float lightPosition[4] = { 0, 2, 1, 0 };
  117. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  118. float lightAmbient[4] = { 0.2, 0.2, 0.2, 1 };
  119. glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  120. heightmap->Draw();
  121. for (auto &enemy : enemies)
  122. enemy->draw();
  123. for (auto &entity : entities)
  124. entity->draw();
  125. interface->draw();
  126. }
  127. void World::update(float elapsedTime)
  128. {
  129. for (auto &entity : entities)
  130. entity->update(elapsedTime);
  131. for (auto &enemy : enemies)
  132. {
  133. //Al deze code zou in enemy moeten staan
  134. enemy->inEyeSight(player->position);
  135. enemy->update(elapsedTime);
  136. if (enemy->hasTarget)
  137. {
  138. for (auto e : entities)
  139. {
  140. if (e->canCollide && e->inObject(enemy->position))
  141. {
  142. enemy->collide(e);
  143. break;
  144. }
  145. }
  146. }
  147. WorldHandler* worldhandler = WorldHandler::getInstance();
  148. enemy->position.y = worldhandler->getHeight(enemy->position.x, enemy->position.z) + 2.0f;
  149. //tot hier
  150. }
  151. }
  152. void World::addLevelObject(LevelObject* obj)
  153. {
  154. entities.push_back(obj);
  155. }
  156. bool World::isPlayerPositionValid()
  157. {
  158. for (auto e : entities)
  159. {
  160. if (e->canCollide && e->inObject(player->position))
  161. return false;
  162. }
  163. return true;
  164. }