diff --git a/BlockClump.cpp b/BlockClump.cpp index b692e52..92d879c 100644 --- a/BlockClump.cpp +++ b/BlockClump.cpp @@ -1,5 +1,11 @@ #include "gameDefines.h" +extern Meteos*game; + +BlockClump::BlockClump(){ + blocks.reserve(game->gameBoard.boardSize.x*game->gameBoard.boardSize.y); +} + std::vector&BlockClump::getBlocks(){ return blocks; } diff --git a/BlockClump.h b/BlockClump.h index fccd233..fc9026b 100644 --- a/BlockClump.h +++ b/BlockClump.h @@ -12,7 +12,7 @@ class BlockClump{ float landTime=0.f; float launchTime=0.f; float y=0; - BlockClump(){} + BlockClump(); std::vector&getBlocks(); void addBlock(int col,BlockColor color); void addBlock(int col,int row,BlockColor color); diff --git a/Board.cpp b/Board.cpp index 870864a..c922f05 100644 --- a/Board.cpp +++ b/Board.cpp @@ -11,7 +11,9 @@ Board::Board(vi2d boardSize,float gravity,float maxGravity,float launchSpd,float yBottom=(boardSize.y-1)*12; for (int i=0;i()); + cols[i].reserve(boardSize.y+10); } + clumps.reserve(100); } void Board::spawnBlock(int col){ diff --git a/C++ProjectTemplate b/C++ProjectTemplate index 74bf3e4..7dfa586 100755 Binary files a/C++ProjectTemplate and b/C++ProjectTemplate differ diff --git a/Meteos.h b/Meteos.h index 4807d86..42ab81a 100644 --- a/Meteos.h +++ b/Meteos.h @@ -14,11 +14,12 @@ class Meteos : public olc::PixelGameEngine{ std::uniform_int_distribution<> randBlockPos,coinFlip; std::mt19937 gen; Board gameBoard; + bool gameCanRun=true; std::map SPRITES; bool OnUserCreate()override; void updateGame(float fElapsedTime); void handleInput(); - void drawGame(float fElapsedTime); + void drawGame(float fElapsedTime,bool debugView); bool OnUserUpdate(float fElapsedTime)override; }; #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 36026f4..2510515 100644 --- a/main.cpp +++ b/main.cpp @@ -19,7 +19,7 @@ bool Meteos::OnUserCreate() 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() + gen=std::mt19937(0); //Standard mersenne_twister_engine seeded with rd() randBlockPos=std::uniform_int_distribution<>(0, 9); coinFlip=std::uniform_int_distribution<>(0, 1); @@ -30,13 +30,15 @@ bool Meteos::OnUserCreate() bool Meteos::OnUserUpdate(float fElapsedTime) { - accumulatedTime+=fElapsedTime; - if (accumulatedTime>=1/60.0f) { - updateGame(1/60.0f); - accumulatedTime=0; + if (gameCanRun){ + accumulatedTime+=fElapsedTime; + if (accumulatedTime>=1/60.0f) { + updateGame(1/60.0f); + accumulatedTime=0; + } + handleInput(); } - handleInput(); - drawGame(fElapsedTime); + drawGame(fElapsedTime,!gameCanRun); return true; } @@ -52,7 +54,7 @@ void Meteos::updateGame(float fElapsedTime){ lastBlockSpawn-=gameBoard.spawnRate; gameBoard.spawnBlock(randBlockPos(gen)); } - for (int i=0;i=0;i--){ BlockClump&c=gameBoard.getBlockClumps()[i]; if (c.launchTime>0) { c.launchTime-=fElapsedTime; @@ -62,12 +64,12 @@ void Meteos::updateGame(float fElapsedTime){ if (c.vspeed>gameBoard.maxGravity){ c.vspeed=gameBoard.maxGravity; } - for (int j=0;j=0;j--) { if (i==j) continue; BlockClump&c2=gameBoard.getBlockClumps()[j]; - for (int k=0;k=0;k--) { Block&b2=c2.getBlocks()[k]; - for (int l=0;l=0;l--) { Block&b3=c.getBlocks()[l]; if (c2.getBlockPosition(b2).x==c.getBlockPosition(b3).x&& c2.getBlockPosition(b2).y+12>=c.getBlockPosition(b3).y&& @@ -96,6 +98,14 @@ void Meteos::updateGame(float fElapsedTime){ for (int m=0;m=j){ i--; } - gameBoard.removeClump(j--); + gameBoard.removeClump(j); goto nextClumpCollisionCheck; } } @@ -145,7 +158,7 @@ void Meteos::updateGame(float fElapsedTime){ c.y-=c.getBlockPosition(b).y+12-b2.pos.y; c.vspeed=0; if (c.landTime>=gameBoard.landTime) { - gameBoard.convertClump(i--); + gameBoard.convertClump(i); } else { c.landTime+=fElapsedTime; } @@ -156,7 +169,7 @@ void Meteos::updateGame(float fElapsedTime){ c.y-=c.getBlockPosition(b).y-gameBoard.yBottom; c.vspeed=0; if (c.landTime>=gameBoard.landTime) { - gameBoard.convertClump(i--); + gameBoard.convertClump(i); } else { c.landTime+=fElapsedTime; } @@ -308,7 +321,7 @@ void Meteos::updateGame(float fElapsedTime){ Block&b2=c.getBlocks()[l]; if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==b.pos.x+checkX*12&&c.getBlockPosition(b2).y==checkY){ found=true; - tempMatchIDsX.push_back({-1,l,k}); + tempMatchIDsX.push_back({(int)(b2.pos.x/12),l,k}); checkX++; goto outercheck2; } @@ -340,7 +353,7 @@ void Meteos::updateGame(float fElapsedTime){ Block&b2=c.getBlocks()[l]; if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==b.pos.x+checkX*12&&c.getBlockPosition(b2).y==checkY){ found=true; - tempMatchIDsX.push_back({-1,l,k}); + tempMatchIDsX.push_back({(int)(b2.pos.x/12),l,k}); checkX--; goto outercheck3; } @@ -373,7 +386,7 @@ void Meteos::updateGame(float fElapsedTime){ if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&c.getBlockPosition(b2).x==checkX&&c.getBlockPosition(b2).y==checkY){ found=true; checkY-=12; - tempMatchIDsY.push_back({-1,l,k}); + tempMatchIDsY.push_back({(int)(b2.pos.x/12),l,k}); goto outercheck4; } } @@ -404,7 +417,7 @@ void Meteos::updateGame(float fElapsedTime){ if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&c.getBlockPosition(b2).x==checkX&&c.getBlockPosition(b2).y==checkY){ found=true; checkY+=12; - tempMatchIDsY.push_back({-1,l,k}); + tempMatchIDsY.push_back({(int)(b2.pos.x/12),l,k}); goto outercheck1; } } @@ -477,51 +490,66 @@ void Meteos::updateGame(float fElapsedTime){ } for (BlockMatchingInfo&info:matchedBlockIDs) { if (info.c!=-1){ - Block&b=gameBoard.getBlockClumps()[info.c].getBlocks()[info.ind]; - for (Block&b2:gameBoard.getBlockClumps()[info.c].getBlocks()){ - if (!b2.markedForRemoval&&b.pos.x==b2.pos.x&&gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y<=gameBoard.getBlockClumps()[info.c].getBlockPosition(b).y){ - c.addBlock(b.pos.x/12,(gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y-baseBlockPos)/12,b2.col); - b2.markedForRemoval=true; + if (gameBoard.getBlockClumps()[info.c].getBlocks().size()>0){ + for (int i=gameBoard.getBlockClumps()[info.c].getBlocks().size()-1;i>=0;i--) { + Block&b2=gameBoard.getBlockClumps()[info.c].getBlocks()[i]; + if ((int)fmod(std::abs((int)gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y-(int)baseBlockPos),12)!=0){ + std::cout<<"Difference is not equal to 0! Value:"<<(int)fmod(std::abs((int)gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y-(int)baseBlockPos),12)<<" "<=0;i--){ + Block&b2=gameBoard.getBlocks(info.col)[i]; + if (b2.pos.y<=yPos){ + if ((int)fmod((int)b2.pos.y-(int)baseBlockPos,12)!=0){ + std::cout<<"Difference is not equal to 0! Value:"<<(int)fmod((int)b2.pos.y-(int)baseBlockPos,12)<<" "<=0;i--){ //Resolve BlockClump movements. BlockClump&c=gameBoard.getBlockClumps()[i]; - for (int j=0;j=0;j--){ Block&b=c.getBlocks()[j]; b.addedToLaunchList=false; if (c.vspeed<0&&c.getBlockPosition(b).y<0||b.markedForRemoval){ - c.removeBlock(j--); + std::cout<<"Removed block @"<=0;y--){ Block&b=gameBoard.getBlocks(i)[y]; b.addedToLaunchList=false; if (b.markedForRemoval){ - gameBoard.removeBlock(i,y--); + gameBoard.removeBlock(i,y); continue; } } @@ -597,7 +625,7 @@ void Meteos::updateGame(float fElapsedTime){ } } -void Meteos::drawGame(float fElapsedTime){ +void Meteos::drawGame(float fElapsedTime,bool debugView){ Clear(Pixel(32,32,255)); for (int x=-1;x<=gameBoard.boardSize.x;x++){ for (int y=0;y<=gameBoard.boardSize.y;y++){ @@ -608,17 +636,18 @@ void Meteos::drawGame(float fElapsedTime){ } } } + int alpha=(debugView)?32:255; for (int i=0;i