From e9c193f4da946e071598c7b3fe34aa70d00c2c9f Mon Sep 17 00:00:00 2001 From: Alfred Burgess <aburgess@ucc.gu.uwa.edu.au> Date: Wed, 29 Nov 2023 14:48:27 +0800 Subject: [PATCH] Created ability to move into a chunk location and visualise it as well --- include/chunk.h | 1 + include/world-hex-2d.h | 2 ++ makefile | 3 ++- src/chunk.cpp | 1 + src/main.cpp | 4 +++- src/world-hex-2d.cpp | 50 ++++++++++++++++++++++++++++++++---------- 6 files changed, 48 insertions(+), 13 deletions(-) diff --git a/include/chunk.h b/include/chunk.h index 13a78b8..087019f 100644 --- a/include/chunk.h +++ b/include/chunk.h @@ -20,6 +20,7 @@ class Chunk Chunk( int x, int y, int z ); ~Chunk(); void generate(); + void visualise() const; std::string getString() const; std::string getString( ChunkQuery format ) const; std::string getLocationString() const; diff --git a/include/world-hex-2d.h b/include/world-hex-2d.h index c14482d..e64c98e 100644 --- a/include/world-hex-2d.h +++ b/include/world-hex-2d.h @@ -20,7 +20,9 @@ class WorldHex2D ~WorldHex2D(); void generateWorld(); + Chunk* createChunkAtPoint( int s, int q, int r ); Chunk* getChunk( int chunkS, int chunkQ, int chunkR ); + Chunk* moveInto( int chunkS, int chunkQ, int chunkR ); Chunk* getMidChunk(); int getMidS(); diff --git a/makefile b/makefile index 9320ff9..0a14e43 100644 --- a/makefile +++ b/makefile @@ -4,7 +4,7 @@ CXX = g++ # Compiler flags -CXXFLAGS = -std=c++17 -Wall -Wreorder +CXXFLAGS = -std=c++17 -Wall -Wreorder -g # Source directory SRC_DIR = src @@ -50,6 +50,7 @@ clean: run_tests: $(TEST_OBJ_FILES) $(CC) $(CFLAGS) $(INC_DIRS) -o test_runner $^ ./test_runner + $(BUILD_DIR)/%.o: $(TEST_DIR)/%.test.cpp @mkdir -p $(@D) diff --git a/src/chunk.cpp b/src/chunk.cpp index a663096..b13330b 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -48,6 +48,7 @@ std::string Chunk::getAgeString() const { return std::to_string(this->age); } std::string Chunk::getString( ChunkQuery format ) const {return "rest";} +void Chunk::visualise() const { std::cout << this->getString() << std::endl; } std::string Chunk::getString() const { std::string str = "Chunk Details:" diff --git a/src/main.cpp b/src/main.cpp index 953c27a..8dd269c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,9 @@ int main() { // Chunk* chunk = map.getMidChunk(); // std::cout << chunk->getString() << std::endl; map.showChunk( 0, 0, 0 ); - map.showChunk( -2, 1, 1 ); + map.moveInto( -2, 1, 1 ); + map.moveInto( -3, 2, 1 ); + map.moveInto( 5, -2, -3 )->visualise(); map.visualise(); // Menu Loop diff --git a/src/world-hex-2d.cpp b/src/world-hex-2d.cpp index eb279e5..2e7ced3 100644 --- a/src/world-hex-2d.cpp +++ b/src/world-hex-2d.cpp @@ -14,6 +14,7 @@ WorldHex2D::WorldHex2D( uint8_t width, uint8_t height, uint8_t depth) this->_logger.info( "Creating Hex Map" ); this->generateWorld(); } + WorldHex2D::~WorldHex2D() { this->_logger.info( "Cleaning world" ); @@ -46,15 +47,28 @@ void WorldHex2D::generateWorld() for( int r = this->_minR; r < this->_maxR; r++ ) { if( s + q + r == 0 ) - { - Chunk* chunk = new Chunk( s, q, r ); - this->chunks_[s][q][r] = chunk; - } + { this->createChunkAtPoint( s, q, r ); } } } } } +Chunk* WorldHex2D::createChunkAtPoint( int s, int q, int r ) +{ + Chunk* chunk = new Chunk( s, q, r ); + this->chunks_[s][q][r] = chunk; + + if( s < this->_minS ) this->_minS = s; + if( q < this->_minQ ) this->_minQ = q; + if( r < this->_minR ) this->_minR = r; + if( s > this->_maxS ) this->_maxS = s; + if( q > this->_maxQ ) this->_maxQ = q; + if( r > this->_maxR ) this->_maxR = r; + + return chunk; +} + + Chunk* WorldHex2D::showChunk( int s, int q, int r ) { Chunk* chunk = this->getChunk( s, q, r ); @@ -124,6 +138,27 @@ Chunk* WorldHex2D::getChunk( int chunkS, int chunkQ, int chunkR ) return nullptr; } +Chunk* WorldHex2D::moveInto( int s, int q, int r ) +{ + Chunk * core = this->getChunk( s, q, r ); + Chunk * tr = this->getChunk( s+1, q, r-1 ); + Chunk * xr = this->getChunk( s, q+1, r-1 ); + Chunk * br = this->getChunk( s-1, q+1, r ); + Chunk * bl = this->getChunk( s-1, q, r+1 ); + Chunk * xl = this->getChunk( s, q-1, r+1 ); + Chunk * tl = this->getChunk( s+1, q-1, r ); + + if( !core ) core = this->createChunkAtPoint( s, q, r ); + if( !tr )tr = this->createChunkAtPoint( s+1, q, r-1 ); + if( !xr ) xr = this->createChunkAtPoint( s, q+1, r-1 ); + if( !br ) br = this->createChunkAtPoint( s-1, q+1, r ); + if( !bl ) bl = this->createChunkAtPoint( s-1, q, r+1 ); + if( !xl ) xl = this->createChunkAtPoint( s, q-1, r+1 ); + if( !tl ) tl = this->createChunkAtPoint( s+1, q-1, r ); + + return core; +} + int WorldHex2D::getMidS(){ return this->_width / 2; } int WorldHex2D::getMidQ(){ return this->_height / 2; } int WorldHex2D::getMidR(){ return this->_depth / 2; } @@ -137,13 +172,6 @@ Chunk* WorldHex2D::getMidChunk(){ void WorldHex2D::visualise() { - this->_minS = this->_width / -2; // s - this->_minQ = this->_height / -2; // q - this->_minR = this->_depth / -2; // r - this->_maxS = ( this->_width / 2 ) + ( this->_width % 2 == 0 ? 0 : 1 ); - this->_maxQ = ( this->_height / 2 ) + ( this->_height % 2 == 0 ? 0 : 1 ); - this->_maxR = ( this->_depth / 2 ) + ( this->_depth % 2 == 0 ? 0 : 1 ); - for( int s = this->_maxS; s >= this->_minS; s-- ) { for( int i = abs(s); i > 0; i-- ) -- GitLab