World.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. case 3: // set target
  68. {
  69. switch (data[1])
  70. {
  71. case 0:
  72. setTarget(0);
  73. break;
  74. case 1:
  75. setTarget(zEntity(*(int*)(data + 2)));
  76. break;
  77. case 2:
  78. setTarget(zBlockAt(Vec3<int>(*(int*)(data + 2), *(int*)(data + 6), *(int*)(data + 10))));
  79. break;
  80. }
  81. break;
  82. }
  83. }
  84. delete[] data;
  85. // TODO: process messages
  86. }
  87. if (type == 4) // POSITION UPDATE
  88. {
  89. serverMessageReader->lese((char*)&ownEntityId, 4);
  90. kam->setEntityId(ownEntityId);
  91. }
  92. network->zFactoryClient()->endMessageReading(background);
  93. }
  94. network->zFactoryClient()->endMessageReading(background);
  95. Entity* player = getCurrentPlayerEntity();
  96. if (player)
  97. {
  98. renderedWorld->lock();
  99. currentDimension->removeDistantChunks({ (int)player->getPos().x, (int)player->getPos().y }, this);
  100. renderedWorld->unlock();
  101. }
  102. }
  103. void World::setChunk(Chunk* chunk)
  104. {
  105. zScreenPtr->lock();
  106. renderedWorld->lock();
  107. currentDimension->setChunk(chunk, chunk->getCenter(), this);
  108. renderedWorld->unlock();
  109. zScreenPtr->unlock();
  110. }
  111. void World::thread()
  112. {
  113. new AsynchronCall("World Update", [this]()
  114. {
  115. while (true)
  116. {
  117. zScreenPtr->lock();
  118. if (currentGame != this)
  119. {
  120. zScreenPtr->unlock();
  121. return;
  122. }
  123. zScreenPtr->unlock();
  124. update(0);
  125. Sleep(10);
  126. }
  127. });
  128. while (true)
  129. {
  130. zScreenPtr->lock();
  131. if (currentGame != this)
  132. {
  133. zScreenPtr->unlock();
  134. return;
  135. }
  136. zScreenPtr->unlock();
  137. update(1);
  138. Sleep(10);
  139. }
  140. }
  141. Block* World::zBlockAt(Framework::Vec3<int> location) const
  142. {
  143. return currentDimension->zBlock(location);
  144. return 0;
  145. }
  146. Block* World::getBlockAt(Framework::Vec3<int> location) const
  147. {
  148. return currentDimension->getBlock(location);
  149. return 0;
  150. }
  151. Dimension* World::zDimension() const
  152. {
  153. return currentDimension;
  154. }
  155. void World::setVisibility(Chunk* zChunk, bool visible)
  156. {
  157. renderedWorld->lock();
  158. if (visible)
  159. renderedWorld->addCollection(dynamic_cast<Framework::Model3DCollection*>(zChunk->getThis()));
  160. else
  161. renderedWorld->removeCollection(zChunk);
  162. renderedWorld->unlock();
  163. }
  164. void World::setVisibility(Entity* zEntity, bool visible)
  165. {
  166. renderedWorld->lock();
  167. if (visible)
  168. renderedWorld->addZeichnung(dynamic_cast<Framework::Model3D*>(zEntity->getThis()));
  169. else
  170. renderedWorld->removeZeichnung(zEntity);
  171. renderedWorld->unlock();
  172. }
  173. Framework::Punkt World::getChunkCenter(int x, int y) const
  174. {
  175. 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);
  176. }
  177. Entity* World::zEntity(int id) const
  178. {
  179. Entity* e = currentDimension->zEntity(id);
  180. if (e)
  181. return e;
  182. return 0;
  183. }
  184. Entity* World::getEntity(int id) const
  185. {
  186. Entity* e = currentDimension->getEntity(id);
  187. if (e)
  188. return e;
  189. return 0;
  190. }
  191. void World::removeEntity(int id)
  192. {
  193. currentDimension->removeEntity(id);
  194. }
  195. PlayerKam* World::zKamera() const
  196. {
  197. return kam;
  198. }
  199. int World::getCurrentPlayerId() const
  200. {
  201. return ownEntityId;
  202. }
  203. Entity* World::getCurrentPlayerEntity() const
  204. {
  205. return zEntity(ownEntityId);
  206. }
  207. void World::setTarget(Framework::Model3D* zTarget)
  208. {
  209. if (zTarget != currentTarget)
  210. {
  211. if (currentTarget)
  212. {
  213. currentTarget->setAmbientFactor(currentTarget->getAmbientFactor() - 0.2f);
  214. currentTarget->release();
  215. currentTarget = 0;
  216. }
  217. if (zTarget)
  218. {
  219. currentTarget = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
  220. if (currentTarget)
  221. currentTarget->setAmbientFactor(currentTarget->getAmbientFactor() + 0.2f);
  222. }
  223. }
  224. }
  225. void World::lockWorld()
  226. {
  227. renderedWorld->lock();
  228. }
  229. void World::unlockWorld()
  230. {
  231. renderedWorld->unlock();
  232. }