Dimension.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "Dimension.h"
  2. #include "Constants.h"
  3. #include "Datei.h"
  4. #include "Game.h"
  5. #include "Globals.h"
  6. using namespace Framework;
  7. Dimension::Dimension( int id )
  8. : dimensionId( id ),
  9. chunks( new Trie<Chunk>() ),
  10. entities( new RCArray<Entity>() )
  11. {}
  12. Dimension::~Dimension()
  13. {
  14. entities->release();
  15. chunks->release();
  16. }
  17. void Dimension::updateVisibility()
  18. {
  19. bool changed = true;
  20. while( changed )
  21. {
  22. changed = false;
  23. for( auto chunk : chunkList )
  24. {
  25. if( chunk )
  26. changed |= chunk->updateVisibility();
  27. }
  28. }
  29. }
  30. void Dimension::getAddrOf( Punkt cPos, char* addr ) const
  31. {
  32. *(int*)addr = cPos.x;
  33. *((int*)addr + 1) = cPos.y;
  34. }
  35. void Dimension::getAddrOfWorld( Punkt wPos, char* addr ) const
  36. {
  37. if( wPos.x < 0 )
  38. wPos.x -= CHUNK_SIZE;
  39. if( wPos.y < 0 ) // needed because otherwise would (-8, -8) have the same adress as (8, 8)
  40. wPos.y -= CHUNK_SIZE;
  41. wPos /= CHUNK_SIZE;
  42. getAddrOf( wPos, addr );
  43. }
  44. Chunk* Dimension::zChunk( Punkt wPos ) const
  45. {
  46. char addr[ 8 ];
  47. getAddrOfWorld( wPos, addr );
  48. return chunks->z( addr, 8 );
  49. }
  50. Framework::Either<Block*, int> Dimension::zBlock( Vec3<int> location )
  51. {
  52. Chunk* c = zChunk( currentGame->getChunkCenter( location.x, location.y ) );
  53. if( c )
  54. {
  55. int x = location.x % CHUNK_SIZE;
  56. int y = location.y % CHUNK_SIZE;
  57. if( x < 0 )
  58. x += CHUNK_SIZE;
  59. if( y < 0 )
  60. y += CHUNK_SIZE;
  61. return c->zBlockAt( Vec3<int>( x, y, location.z ) );
  62. }
  63. return 0;
  64. }
  65. void Dimension::addEntity( Entity* entity )
  66. {
  67. entities->add( entity );
  68. }
  69. void Dimension::addChunk( Chunk* chunk )
  70. {
  71. char addr[ 8 ];
  72. getAddrOfWorld( chunk->getCenter(), addr );
  73. Chunk *old = chunks->z( addr, 8 );
  74. if( old )
  75. {
  76. for( int i = 0; i < chunkList.getEintragAnzahl(); i++ )
  77. {
  78. if( chunkList.get( i ) == old )
  79. {
  80. chunkList.remove( i );
  81. break;
  82. }
  83. }
  84. }
  85. chunks->set( addr, 8, chunk );
  86. chunkList.add( chunk );
  87. getAddrOfWorld( chunk->getCenter() + Punkt( CHUNK_SIZE, 0 ), addr );
  88. Chunk* zChunk = chunks->z( addr, 8 );
  89. if( zChunk )
  90. {
  91. zChunk->setNeighbor( WEST, chunk );
  92. chunk->setNeighbor( EAST, chunk );
  93. }
  94. getAddrOfWorld( chunk->getCenter() + Punkt( -CHUNK_SIZE, 0 ), addr );
  95. zChunk = chunks->z( addr, 8 );
  96. if( zChunk )
  97. {
  98. zChunk->setNeighbor( EAST, chunk );
  99. chunk->setNeighbor( WEST, chunk );
  100. }
  101. getAddrOfWorld( chunk->getCenter() + Punkt( 0, CHUNK_SIZE ), addr );
  102. zChunk = chunks->z( addr, 8 );
  103. if( zChunk )
  104. {
  105. zChunk->setNeighbor( NORTH, chunk );
  106. chunk->setNeighbor( SOUTH, chunk );
  107. }
  108. getAddrOfWorld( chunk->getCenter() + Punkt( 0, -CHUNK_SIZE ), addr );
  109. zChunk = chunks->z( addr, 8 );
  110. if( zChunk )
  111. {
  112. zChunk->setNeighbor( SOUTH, chunk );
  113. chunk->setNeighbor( NORTH, chunk );
  114. }
  115. }
  116. int Dimension::getDimensionId() const
  117. {
  118. return dimensionId;
  119. }
  120. bool Dimension::hasChunck( int x, int y ) const
  121. {
  122. return zChunk( Punkt( x, y ) );
  123. }