World.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include <Network.h>
  2. #include <Welt3D.h>
  3. #include <GraphicsApi.h>
  4. #include <iostream>
  5. #include "World.h"
  6. #include "Globals.h"
  7. #include "WorldUpdate.h"
  8. #include "Constants.h"
  9. #include "Registries.h"
  10. #include "Game.h"
  11. #include <AsynchronCall.h>
  12. using namespace Network;
  13. using namespace Framework;
  14. World::World(Bildschirm3D* zScreen)
  15. : Thread()
  16. {
  17. renderedWorld = new Welt3D();
  18. renderedWorld->addDiffuseLight(DiffuseLight{ Vec3<float>(0.5f, 0.5f, -1.f), Vec3<float>(1.f, 1.f, 1.f) });
  19. currentDimension = new Dimension();
  20. zScreenPtr = zScreen;
  21. kam = new PlayerKam(zScreen);
  22. kam->setWelt(renderedWorld);
  23. zScreen->addKamera(kam);
  24. firstMessage = 1;
  25. hasTarget = 0;
  26. entityTarget = -1;
  27. ownEntityId = -1;
  28. currentTarget = 0;
  29. start();
  30. }
  31. World::~World()
  32. {
  33. zScreenPtr->removeKamera(kam);
  34. currentDimension->release();
  35. if (currentTarget)
  36. currentTarget->release();
  37. }
  38. void World::update(bool background)
  39. {
  40. NetworkReader* serverMessageReader = 0;
  41. unsigned char type = 0;
  42. while (background ? serverMessageReader = network->zFactoryClient()->getNextBackgroundMessage() : serverMessageReader = network->zFactoryClient()->getNextForegroundMessage())
  43. {
  44. serverMessageReader->lese((char*)&type, 1);
  45. if (type == 2) // WORLD UPDATE
  46. {
  47. int id = 0;
  48. serverMessageReader->lese((char*)&id, 4);
  49. STATIC_REGISTRY(WorldUpdateType).zElement(id)->applyUpdateAndCheck(serverMessageReader);
  50. }
  51. if (type == 3) // API MESSAGE
  52. {
  53. int length;
  54. serverMessageReader->lese((char*)&length, 4);
  55. char* data = new char[length];
  56. serverMessageReader->lese(data, length);
  57. switch (data[0])
  58. {
  59. case 1: // dimension message
  60. {
  61. currentDimension->api(data + 1);
  62. break;
  63. }
  64. case 2: // gui message
  65. ((Game*)(Menu*)menuRegister->get("game"))->api(data + 1);
  66. break;
  67. }
  68. delete[] data;
  69. // TODO: process messages
  70. }
  71. if (type == 4) // POSITION UPDATE
  72. {
  73. serverMessageReader->lese((char*)&ownEntityId, 4);
  74. kam->setEntityId(ownEntityId);
  75. }
  76. network->zFactoryClient()->endMessageReading(background);
  77. }
  78. network->zFactoryClient()->endMessageReading(background);
  79. Entity* player = getCurrentPlayerEntity();
  80. if (player)
  81. {
  82. renderedWorld->lock();
  83. currentDimension->removeDistantChunks({ (int)player->getPos().x, (int)player->getPos().y }, this);
  84. renderedWorld->unlock();
  85. }
  86. }
  87. void World::setChunk(Chunk* chunk)
  88. {
  89. zScreenPtr->lock();
  90. renderedWorld->lock();
  91. currentDimension->setChunk(chunk, chunk->getCenter(), this);
  92. renderedWorld->unlock();
  93. zScreenPtr->unlock();
  94. }
  95. void World::thread()
  96. {
  97. new AsynchronCall("World Update", [this]()
  98. {
  99. while (true)
  100. {
  101. zScreenPtr->lock();
  102. if (currentGame != this)
  103. {
  104. zScreenPtr->unlock();
  105. return;
  106. }
  107. zScreenPtr->unlock();
  108. update(0);
  109. Sleep(10);
  110. }
  111. });
  112. while (true)
  113. {
  114. zScreenPtr->lock();
  115. if (currentGame != this)
  116. {
  117. zScreenPtr->unlock();
  118. return;
  119. }
  120. zScreenPtr->unlock();
  121. update(1);
  122. Sleep(10);
  123. }
  124. }
  125. Block* World::zBlockAt(Framework::Vec3<int> location) const
  126. {
  127. return currentDimension->zBlock(location);
  128. return 0;
  129. }
  130. Block* World::getBlockAt(Framework::Vec3<int> location) const
  131. {
  132. return currentDimension->getBlock(location);
  133. return 0;
  134. }
  135. Dimension* World::zDimension() const
  136. {
  137. return currentDimension;
  138. }
  139. void World::setVisibility(Chunk* zChunk, bool visible)
  140. {
  141. renderedWorld->lock();
  142. if (visible)
  143. renderedWorld->addCollection(dynamic_cast<Framework::Model3DCollection*>(zChunk->getThis()));
  144. else
  145. renderedWorld->removeCollection(zChunk);
  146. renderedWorld->unlock();
  147. }
  148. void World::setVisibility(Entity* zEntity, bool visible)
  149. {
  150. renderedWorld->lock();
  151. if (visible)
  152. renderedWorld->addZeichnung(dynamic_cast<Framework::Model3D*>(zEntity->getThis()));
  153. else
  154. renderedWorld->removeZeichnung(zEntity);
  155. renderedWorld->unlock();
  156. }
  157. Framework::Punkt World::getChunkCenter(int x, int y) const
  158. {
  159. return Punkt(((x < 0 ? x + 1 : x) / CHUNK_SIZE) * CHUNK_SIZE + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2, ((y < 0 ? y + 1 : y) / CHUNK_SIZE) * CHUNK_SIZE + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2);
  160. }
  161. Entity* World::zEntity(int id) const
  162. {
  163. Entity* e = currentDimension->zEntity(id);
  164. if (e)
  165. return e;
  166. return 0;
  167. }
  168. Entity* World::getEntity(int id) const
  169. {
  170. Entity* e = currentDimension->getEntity(id);
  171. if (e)
  172. return e;
  173. return 0;
  174. }
  175. void World::removeEntity(int id)
  176. {
  177. currentDimension->removeEntity(id);
  178. }
  179. PlayerKam* World::zKamera() const
  180. {
  181. return kam;
  182. }
  183. int World::getCurrentPlayerId() const
  184. {
  185. return ownEntityId;
  186. }
  187. Entity* World::getCurrentPlayerEntity() const
  188. {
  189. return zEntity(ownEntityId);
  190. }
  191. void World::setTarget(Framework::Model3D* zTarget)
  192. {
  193. if (zTarget != currentTarget)
  194. {
  195. if (currentTarget)
  196. {
  197. currentTarget->setAmbientFactor(currentTarget->getAmbientFactor() - 0.2f);
  198. currentTarget->release();
  199. currentTarget = 0;
  200. }
  201. if (zTarget)
  202. {
  203. currentTarget = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
  204. if (currentTarget)
  205. currentTarget->setAmbientFactor(currentTarget->getAmbientFactor() + 0.2f);
  206. }
  207. }
  208. }
  209. void World::lockWorld()
  210. {
  211. renderedWorld->lock();
  212. }
  213. void World::unlockWorld()
  214. {
  215. renderedWorld->unlock();
  216. }