|
@@ -0,0 +1,124 @@
|
|
|
+#include "Dimension.h"
|
|
|
+#include "Constants.h"
|
|
|
+#include "Datei.h"
|
|
|
+#include "Game.h"
|
|
|
+#include "Globals.h"
|
|
|
+
|
|
|
+using namespace Framework;
|
|
|
+
|
|
|
+
|
|
|
+Dimension::Dimension( int id )
|
|
|
+ : dimensionId( id ),
|
|
|
+ chunks( new Trie<Chunk>() ),
|
|
|
+ entities( new RCArray<Entity>() )
|
|
|
+{}
|
|
|
+
|
|
|
+Dimension::~Dimension()
|
|
|
+{
|
|
|
+ entities->release();
|
|
|
+ chunks->release();
|
|
|
+}
|
|
|
+
|
|
|
+void Dimension::updateVisibility()
|
|
|
+{
|
|
|
+ bool changed = true;
|
|
|
+ while( changed )
|
|
|
+ {
|
|
|
+ changed = false;
|
|
|
+ for( auto chunk : chunkList )
|
|
|
+ {
|
|
|
+ if( chunk )
|
|
|
+ changed |= chunk->updateVisibility();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void Dimension::getAddrOf( Punkt cPos, char* addr ) const
|
|
|
+{
|
|
|
+ *(int*)addr = cPos.x;
|
|
|
+ *((int*)addr + 1) = cPos.y;
|
|
|
+}
|
|
|
+
|
|
|
+void Dimension::getAddrOfWorld( Punkt wPos, char* addr ) const
|
|
|
+{
|
|
|
+ if( wPos.x < 0 )
|
|
|
+ wPos.x -= CHUNK_SIZE;
|
|
|
+ if( wPos.y < 0 ) // needed because otherwise would (-8, -8) have the same adress as (8, 8)
|
|
|
+ wPos.y -= CHUNK_SIZE;
|
|
|
+ wPos /= CHUNK_SIZE;
|
|
|
+ getAddrOf( wPos, addr );
|
|
|
+}
|
|
|
+
|
|
|
+Chunk* Dimension::zChunk( Punkt wPos ) const
|
|
|
+{
|
|
|
+ char addr[ 8 ];
|
|
|
+ getAddrOfWorld( wPos, addr );
|
|
|
+ return chunks->z( addr, 8 );
|
|
|
+}
|
|
|
+
|
|
|
+Framework::Either<Block*, int> Dimension::zBlock( Vec3<int> location )
|
|
|
+{
|
|
|
+ Chunk* c = zChunk( currentGame->getChunkCenter( location.x, location.y ) );
|
|
|
+ if( c )
|
|
|
+ {
|
|
|
+ int x = location.x % CHUNK_SIZE;
|
|
|
+ int y = location.y % CHUNK_SIZE;
|
|
|
+ if( x < 0 )
|
|
|
+ x += CHUNK_SIZE;
|
|
|
+ if( y < 0 )
|
|
|
+ y += CHUNK_SIZE;
|
|
|
+ return c->zBlockAt( Vec3<int>( x, y, location.z ) );
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+void Dimension::addEntity( Entity* entity )
|
|
|
+{
|
|
|
+ entities->add( entity );
|
|
|
+}
|
|
|
+
|
|
|
+void Dimension::addChunk( Chunk* chunk )
|
|
|
+{
|
|
|
+ char addr[ 8 ];
|
|
|
+ getAddrOfWorld( chunk->getCenter(), addr );
|
|
|
+ chunks->set( addr, 8, chunk );
|
|
|
+ chunkList.add( chunk );
|
|
|
+ getAddrOfWorld( chunk->getCenter() + Punkt( CHUNK_SIZE, 0 ), addr );
|
|
|
+ Chunk* zChunk = chunks->z( addr, 8 );
|
|
|
+ if( zChunk )
|
|
|
+ {
|
|
|
+ zChunk->setNeighbor( WEST, chunk );
|
|
|
+ chunk->setNeighbor( EAST, chunk );
|
|
|
+ }
|
|
|
+ getAddrOfWorld( chunk->getCenter() + Punkt( -CHUNK_SIZE, 0 ), addr );
|
|
|
+ zChunk = chunks->z( addr, 8 );
|
|
|
+ if( zChunk )
|
|
|
+ {
|
|
|
+ zChunk->setNeighbor( EAST, chunk );
|
|
|
+ chunk->setNeighbor( WEST, chunk );
|
|
|
+ }
|
|
|
+ getAddrOfWorld( chunk->getCenter() + Punkt( 0, CHUNK_SIZE ), addr );
|
|
|
+ zChunk = chunks->z( addr, 8 );
|
|
|
+ if( zChunk )
|
|
|
+ {
|
|
|
+ zChunk->setNeighbor( NORTH, chunk );
|
|
|
+ chunk->setNeighbor( SOUTH, chunk );
|
|
|
+ }
|
|
|
+ getAddrOfWorld( chunk->getCenter() + Punkt( 0, -CHUNK_SIZE ), addr );
|
|
|
+ zChunk = chunks->z( addr, 8 );
|
|
|
+ if( zChunk )
|
|
|
+ {
|
|
|
+ zChunk->setNeighbor( SOUTH, chunk );
|
|
|
+ chunk->setNeighbor( NORTH, chunk );
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+int Dimension::getDimensionId() const
|
|
|
+{
|
|
|
+ return dimensionId;
|
|
|
+}
|
|
|
+
|
|
|
+bool Dimension::hasChunck( int x, int y ) const
|
|
|
+{
|
|
|
+ return zChunk( Punkt( x, y ) );
|
|
|
+}
|