diff --git a/Block.h b/Block.h index 434040b..adc6a68 100644 --- a/Block.h +++ b/Block.h @@ -2,11 +2,25 @@ #define BLOCK_H #include "pixelGameEngine.h" +enum class BlockColor{ + WHITE, + GREEN, + RED, + ORANGE, + BLUE, + PINK, + LIGHT, + DARK, + SOUL, + TIME +}; + class Block{ public: vf2d pos; + BlockColor col; bool markedForDeletion=false; - Block(vf2d pos) - :pos(pos){} + Block(vf2d pos,BlockColor col) + :pos(pos),col(col){} }; #endif \ No newline at end of file diff --git a/BlockClump.cpp b/BlockClump.cpp index a56e20d..eb1e662 100644 --- a/BlockClump.cpp +++ b/BlockClump.cpp @@ -4,16 +4,16 @@ std::vector&BlockClump::getBlocks(){ return blocks; } -void BlockClump::addBlock(int col){ - blocks.push_back(Block({(float)col*12,0})); +void BlockClump::addBlock(int col,BlockColor color){ + blocks.push_back(Block({(float)col*12,0},color)); } -void BlockClump::addBlock(int col,int row){ - blocks.push_back(Block({(float)col*12,(float)row*12})); +void BlockClump::addBlock(int col,int row,BlockColor color){ + blocks.push_back(Block({(float)col*12,(float)row*12},color)); } -void BlockClump::addBlock(vf2d offset){ - blocks.push_back(Block(offset)); +void BlockClump::addBlock(vf2d offset,BlockColor color){ + blocks.push_back(Block(offset,color)); } vf2d BlockClump::getBlockPosition(Block&b){ diff --git a/BlockClump.h b/BlockClump.h index 311d074..86ff460 100644 --- a/BlockClump.h +++ b/BlockClump.h @@ -3,6 +3,7 @@ #include "pixelGameEngine.h" class Block; +enum class BlockColor; class BlockClump{ std::vectorblocks; @@ -12,9 +13,9 @@ class BlockClump{ float y=0; BlockClump(){} std::vector&getBlocks(); - void addBlock(int col); - void addBlock(int col,int row); - void addBlock(vf2d offset); + void addBlock(int col,BlockColor color); + void addBlock(int col,int row,BlockColor color); + void addBlock(vf2d offset,BlockColor color); //Get a block's position relative to this block clump. vf2d getBlockPosition(Block&b); }; diff --git a/Board.cpp b/Board.cpp index a082fdd..09de8e4 100644 --- a/Board.cpp +++ b/Board.cpp @@ -2,49 +2,37 @@ extern Meteos*game; -Board::Board(vi2d boardSize,float gravity,float spawnRate) -:boardSize(boardSize),gravity(gravity),spawnRate(spawnRate){ +Board::Board() +:colorHandler({}){} + +Board::Board(vi2d boardSize,float gravity,float spawnRate,std::array colorRates,Renderable&tileset) +:boardSize(boardSize),gravity(gravity),spawnRate(spawnRate),colorHandler(colorRates),tileset(tileset.Decal()){ drawOffset={(float)game->ScreenWidth()/2-boardSize.x/2*12,(float)game->ScreenHeight()/2-boardSize.y/2*12}; yBottom=(boardSize.y-1)*12; for (int i=0;i()); } - BlockClump c=BlockClump(); - c.addBlock(0,0); - c.addBlock(1,0); - c.addBlock(2,0); - c.vspeed=-20; - c.y=yBottom-12; - BlockClump c2=BlockClump(); - c2.addBlock(0,0); - c2.addBlock(1,0); - c2.addBlock(1,-1); - c2.addBlock(2,0); - c2.vspeed=20; - c2.y=24; - clumps.push_back(c); - clumps.push_back(c2); } void Board::spawnBlock(int col){ BlockClump c=BlockClump(); - c.addBlock(col); + c.addBlock(col,colorHandler.getRandomColor()); for(int counter=1;game->coinFlip(game->gen)==0;counter++) { - c.addBlock(col,-counter); + c.addBlock(col,-counter,colorHandler.getRandomColor()); } if (game->coinFlip(game->gen)==0) { if (col>0) { - c.addBlock(col-1); + c.addBlock(col-1,colorHandler.getRandomColor()); for(int counter=1;game->coinFlip(game->gen)==0;counter++) { - c.addBlock(col-1,-counter); + c.addBlock(col-1,-counter,colorHandler.getRandomColor()); } } } if (game->coinFlip(game->gen)==0) { if (colcoinFlip(game->gen)==0;counter++) { - c.addBlock(col+1,-counter); + c.addBlock(col+1,-counter,colorHandler.getRandomColor()); } } } @@ -84,7 +72,7 @@ void Board::convertClump(int ind){ Block&b2=c.getBlocks()[i]; if (c.getBlockPosition(b).x==b2.pos.x) { b2.markedForDeletion=true; - c2.addBlock(b2.pos); + c2.addBlock(b2.pos,b2.col); } c2.y=c.y; } @@ -95,4 +83,16 @@ void Board::convertClump(int ind){ } } removeClump(ind); +} + +BlockColor ColorDistributor::getRandomColor(){ + int val=range(game->gen); + for (int i=0;i<10;i++) { + if (rangeTable[i].first!=-1&& + rangeTable[i].first<=val&& + rangeTable[i].second>val) { + return (BlockColor)i; + } + } + return BlockColor::WHITE; } \ No newline at end of file diff --git a/Board.h b/Board.h index 68e0413..765c067 100644 --- a/Board.h +++ b/Board.h @@ -5,17 +5,42 @@ class Block; class BlockClump; +enum class BlockColor; + +class ColorDistributor{ + std::array,10>rangeTable; + int maxRange=0; + std::uniform_int_distribution<>range; + public: + ColorDistributor(std::arraycolorRates){ + int sum=0; + for (int i=0;i0) { + rangeTable[i]={sum,sum+colorRates[i]}; + sum+=colorRates[i]; + maxRange=sum; + } else { + rangeTable[i]={-1,-1}; + } + } + range=std::uniform_int_distribution<>(0, maxRange); + } + BlockColor getRandomColor(); +}; + class Board{ std::vector> cols; std::vector clumps; public: + ColorDistributor colorHandler; float yBottom; + Decal*tileset; vf2d drawOffset; vi2d boardSize; float gravity; float spawnRate; - Board(){} - Board(vi2d boardSize,float gravity,float spawnRate); + Board(); + Board(vi2d boardSize,float gravity,float spawnRate,std::array colorRates,Renderable&tileset); void spawnBlock(int col); std::vector&getBlocks(int col); std::vector&getBlockClumps(); diff --git a/C++/scripts/build.sh b/C++/scripts/build.sh index fe86185..bbdb7f6 100755 --- a/C++/scripts/build.sh +++ b/C++/scripts/build.sh @@ -1,6 +1,11 @@ #Compiles the entire program then runs it, producing an executable. If the "test" argument is included, will try and run tests too (in the test folder) #C++ printf "Running program...\n\n\n" +output=$(dpkg -l | grep libx11-dev) +if [[ -z $output ]] +then + sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev +fi if [ "$1" = "test" ] then printf "Running tests...\n" diff --git a/C++/scripts/debug.sh b/C++/scripts/debug.sh index eca8844..2854ad6 100755 --- a/C++/scripts/debug.sh +++ b/C++/scripts/debug.sh @@ -1,6 +1,11 @@ #Compiles the entire program with debug flags then runs it in gdb. If the "test" argument is included, will try and run tests too (in the test folder) #C++ printf "Running program...\n\n\n" +output=$(dpkg -l | grep libx11-dev) +if [[ -z $output ]] +then + sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev +fi if [ "$1" = "test" ] then printf "Running tests...\n" diff --git a/C++/scripts/md5 b/C++/scripts/md5 index 47f3924..45f0dae 100644 --- a/C++/scripts/md5 +++ b/C++/scripts/md5 @@ -1,7 +1,7 @@ -build.sh:6fbec04ed16be42bab846f1eed603133 - +build.sh:d5e4e98a6f677aa0a9759ba0afb2fb77 - commit.sh:d03a46e721060c22ccb146e19d27e70a - -debug.sh:7f57c6640be5f262988961c3b45dce97 - +debug.sh:849488515cab075948653c15eec4177b - lines.sh:3b907786f7fc9204025993016c9080de - -release.sh:0a525311cc14b9c8aefc6f2b816129a1 - +release.sh:6a96fb84ba64ed60d31be436ec069f05 - temp:d41d8cd98f00b204e9800998ecf8427e - -web.sh:96f2c316536011a3defac50aecae487d - +web.sh:b982212b88b80a59ad607b47d2ff3e94 - diff --git a/C++/scripts/release.sh b/C++/scripts/release.sh index 807a40b..2f84395 100755 --- a/C++/scripts/release.sh +++ b/C++/scripts/release.sh @@ -1,6 +1,11 @@ #Creates a release build that focuses on high runtime performance. #C++ printf "Running program...\n\n\n" +output=$(dpkg -l | grep libx11-dev) +if [[ -z $output ]] +then + sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev +fi if g++ $(find . -type f -name "*.cpp" -not -path "./test/*") ${CUSTOM_PARAMS} -O3 -s -DNDEBUG -o ${PROJECT_NAME}; then ./${PROJECT_NAME} "$@" fi diff --git a/C++/scripts/web.sh b/C++/scripts/web.sh index 991d44f..c46d605 100755 --- a/C++/scripts/web.sh +++ b/C++/scripts/web.sh @@ -1,5 +1,10 @@ #Compiles emscripten instance of this project for the web. #C++ +output=$(dpkg -l | grep libx11-dev) +if [[ -z $output ]] +then + sudo apt install libx11-dev libpulse-dev mesa-common-dev libpng-dev +fi if [ -d "assets" ]; then em++ -std=c++17 -O2 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s MIN_WEBGL_VERSION=2 -s USE_SDL_MIXER=2 -s USE_LIBPNG=1 $(find . -type f -name "*.cpp" -not -path "./test/*") -o ${PROJECT_NAME}.html -I pixelGameEngine.h --preload-file ./assets else diff --git a/C++ProjectTemplate b/C++ProjectTemplate index 99f018d..7850879 100755 Binary files a/C++ProjectTemplate and b/C++ProjectTemplate differ diff --git a/Meteos.h b/Meteos.h index b378218..767ab43 100644 --- a/Meteos.h +++ b/Meteos.h @@ -13,6 +13,7 @@ class Meteos : public olc::PixelGameEngine{ std::uniform_int_distribution<> randBlockPos,coinFlip; std::mt19937 gen; Board gameBoard; + std::map SPRITES; bool OnUserCreate()override; bool OnUserUpdate(float fElapsedTime)override; }; diff --git a/assets/blocks_test.png b/assets/blocks_test.png new file mode 100644 index 0000000..cdaeed6 Binary files /dev/null and b/assets/blocks_test.png differ diff --git a/main.cpp b/main.cpp index 37f2438..e045e6e 100644 --- a/main.cpp +++ b/main.cpp @@ -9,12 +9,15 @@ bool Meteos::OnUserCreate() { game=this; + SPRITES["blocks_test.png"].Load("assets/blocks_test.png"); + std::random_device rd; //Will be used to obtain a seed for the random number engine gen=std::mt19937(rd()); //Standard mersenne_twister_engine seeded with rd() randBlockPos=std::uniform_int_distribution<>(0, 9); coinFlip=std::uniform_int_distribution<>(0, 1); - gameBoard=Board({10,14},12.f,3.0f); + gameBoard=Board({10,14},12.f,3.0f,{3,3,2,3,2,3,0,0,0,0},SPRITES["blocks_test.png"]); + return true; } @@ -57,7 +60,7 @@ bool Meteos::OnUserUpdate(float fElapsedTime) //Copy every block from one clump to the other for (int m=0;m0) { c.vspeed/=4; @@ -92,17 +95,17 @@ bool Meteos::OnUserUpdate(float fElapsedTime) goto nextClump; } } + c.y+=c.vspeed*fElapsedTime; for (int j=0;j