World.cpp 4.8 KB

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