World.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "BasicBlocks.h"
  11. #include "Game.h"
  12. #include <AsynchronCall.h>
  13. using namespace Network;
  14. using namespace Framework;
  15. World::World( Bildschirm3D* zScreen )
  16. : Thread()
  17. {
  18. renderedWorld = new Welt3D();
  19. renderedWorld->addDiffuseLight( DiffuseLight{ Vec3<float>( 0.5f, 0.5f, -1.f ), Vec3<float>( 1.f, 1.f, 1.f ) } );
  20. dimensions = new RCArray<Dimension>();
  21. currentPlayer = new CurrentPlayer();
  22. zScreenPtr = zScreen;
  23. kam = new PlayerKam( zScreen );
  24. kam->setWelt( renderedWorld );
  25. zScreen->addKamera( kam );
  26. firstMessage = 1;
  27. hasTarget = 0;
  28. entityTarget = -1;
  29. ownEntityId = -1;
  30. currentTarget = 0;
  31. start();
  32. }
  33. World::~World()
  34. {
  35. zScreenPtr->removeKamera( kam );
  36. dimensions->release();
  37. currentPlayer->release();
  38. }
  39. void World::update( bool background )
  40. {
  41. NetworkReader* serverMessageReader = 0;
  42. unsigned char type = 0;
  43. while( background ? serverMessageReader = network->zFactoryClient()->getNextBackgroundMessage() : serverMessageReader = network->zFactoryClient()->getNextForegroundMessage() )
  44. {
  45. serverMessageReader->lese( (char*)&type, 1 );
  46. if( type == 2 ) // WORLD UPDATE
  47. {
  48. int id = 0;
  49. serverMessageReader->lese( (char*)&id, 4 );
  50. STATIC_REGISTRY( WorldUpdateType ).zElement( id )->applyUpdateAndCheck( serverMessageReader );
  51. }
  52. if( type == 3 ) // API MESSAGE
  53. {
  54. // TODO: process messages
  55. }
  56. if( type == 4 ) // POSITION UPDATE
  57. {
  58. serverMessageReader->lese( (char*)&ownEntityId, 4 );
  59. }
  60. network->zFactoryClient()->endMessageReading( background );
  61. }
  62. network->zFactoryClient()->endMessageReading( background );
  63. Entity* player = getCurrentPlayerEntity();
  64. if( player )
  65. {
  66. for( Dimension* dim : *dimensions )
  67. dim->removeDistantChunks( { (int)player->getPos().x, (int)player->getPos().y }, this );
  68. }
  69. }
  70. void World::setChunk( Chunk* chunk, int dimensionId )
  71. {
  72. zScreenPtr->lock();
  73. Dimension* zDim = zDimension( dimensionId );
  74. if( !zDim )
  75. {
  76. zDim = new Dimension( dimensionId );
  77. dimensions->add( zDim );
  78. }
  79. zDim->setChunk( chunk, chunk->getCenter(), this );
  80. zScreenPtr->unlock();
  81. }
  82. void World::thread()
  83. {
  84. new AsynchronCall( [this]() {
  85. while( true )
  86. {
  87. zScreenPtr->lock();
  88. if( currentGame != this )
  89. {
  90. zScreenPtr->unlock();
  91. return;
  92. }
  93. zScreenPtr->unlock();
  94. update( 0 );
  95. Sleep( 10 );
  96. }
  97. } );
  98. while( true )
  99. {
  100. zScreenPtr->lock();
  101. if( currentGame != this )
  102. {
  103. zScreenPtr->unlock();
  104. return;
  105. }
  106. zScreenPtr->unlock();
  107. update( 1 );
  108. Sleep( 10 );
  109. }
  110. }
  111. Block* World::zBlockAt( Framework::Vec3<int> location, int dimension ) const
  112. {
  113. Dimension* dim = zDimension( dimension );
  114. if( dim )
  115. return dim->zBlock( location );
  116. return 0;
  117. }
  118. Dimension* World::zDimension( int id ) const
  119. {
  120. for( auto dim : *dimensions )
  121. {
  122. if( dim->getDimensionId() == id )
  123. return dim;
  124. }
  125. return 0;
  126. }
  127. Dimension* World::zDimensionOrCreate( int id )
  128. {
  129. zScreenPtr->lock();
  130. Dimension* d = zDimension( id );
  131. if( !d )
  132. {
  133. d = new Dimension( id );
  134. dimensions->add( d );
  135. }
  136. zScreenPtr->unlock();
  137. return d;
  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::Bildschirm3D* World::zScreen() const
  158. {
  159. return zScreenPtr;
  160. }
  161. Framework::Punkt World::getChunkCenter( int x, int y ) const
  162. {
  163. 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 );
  164. }
  165. Entity* World::zEntity( int id ) const
  166. {
  167. for( Dimension* d : *dimensions )
  168. {
  169. Entity* e = d->zEntity( id );
  170. if( e )
  171. return e;
  172. }
  173. return 0;
  174. }
  175. void World::removeEntity( int id )
  176. {
  177. for( Dimension* d : *dimensions )
  178. d->removeEntity( id );
  179. }
  180. PlayerKam* World::zKamera() const
  181. {
  182. return kam;
  183. }
  184. int World::getCurrentPlayerId() const
  185. {
  186. return ownEntityId;
  187. }
  188. Entity* World::getCurrentPlayerEntity() const
  189. {
  190. for( Dimension* d : *dimensions )
  191. {
  192. Entity* e = d->zEntity( ownEntityId );
  193. if( e )
  194. return e;
  195. }
  196. return 0;
  197. }
  198. void World::setTarget( Framework::Model3D* zTarget )
  199. {
  200. if( zTarget != currentTarget )
  201. {
  202. if( currentTarget )
  203. currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() - 0.2f );
  204. currentTarget = zTarget;
  205. if( currentTarget )
  206. currentTarget->setAmbientFactor( currentTarget->getAmbientFactor() + 0.2f );
  207. }
  208. }