diff --git a/C++ProjectTemplate b/C++ProjectTemplate index 6bfa16d..f26ea0b 100755 Binary files a/C++ProjectTemplate and b/C++ProjectTemplate differ diff --git a/Meteos.h b/Meteos.h index 767ab43..291e09e 100644 --- a/Meteos.h +++ b/Meteos.h @@ -10,11 +10,14 @@ class Meteos : public olc::PixelGameEngine{ sAppName="Meteos"; } float lastBlockSpawn=0.0f; + float accumulatedTime=0.0f; std::uniform_int_distribution<> randBlockPos,coinFlip; std::mt19937 gen; Board gameBoard; std::map SPRITES; bool OnUserCreate()override; + void updateGame(float fElapsedTime); + void drawGame(); bool OnUserUpdate(float fElapsedTime)override; }; #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index ff810ba..f4b4c6f 100644 --- a/main.cpp +++ b/main.cpp @@ -12,36 +12,37 @@ 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(rd()); //Standard mersenne_twister_engine seeded with rd() + gen=std::mt19937(0); + //Seed 0 causes a stacked block in the middle row followed by a giant stack up soon after. randBlockPos=std::uniform_int_distribution<>(0, 9); coinFlip=std::uniform_int_distribution<>(0, 1); - gameBoard=Board({10,14},20.f,100.f,-60.f,1.0f,{3,0,0,0,0,0,0,0,0,0},SPRITES["blocks_test.png"]); + gameBoard=Board({10,14},0.2f,1.f,-6.f,1.0f,{3,0,0,0,0,0,0,0,0,0},SPRITES["blocks_test.png"]); return true; } bool Meteos::OnUserUpdate(float fElapsedTime) { - fElapsedTime=std::min(fElapsedTime,1/60.f); + accumulatedTime+=fElapsedTime; + while (accumulatedTime>=1/60.0f) { + updateGame(accumulatedTime); + accumulatedTime-=1/60.0f; + } + drawGame(); + return true; +} + +void Meteos::updateGame(float fElapsedTime){ lastBlockSpawn+=fElapsedTime; if (lastBlockSpawn>=gameBoard.spawnRate){ lastBlockSpawn-=gameBoard.spawnRate; gameBoard.spawnBlock(randBlockPos(gen)); } - Clear(Pixel(32,32,255)); - for (int x=-1;x<=gameBoard.boardSize.x;x++){ - for (int y=0;y<=gameBoard.boardSize.y;y++){ - if (x==-1||x==10||y==14){ - FillRectDecal({(float)(gameBoard.drawOffset.x+x*12),(float)(gameBoard.drawOffset.y+y*12)},{12,12},Pixel(0,0,0,255)); - } else { - DrawRectDecal({(float)(gameBoard.drawOffset.x+x*12),(float)(gameBoard.drawOffset.y+y*12)},{12,12},Pixel(255,255,255,64)); - } - } - } for (int i=0;igameBoard.maxGravity){ c.vspeed=gameBoard.maxGravity; } @@ -98,11 +99,7 @@ bool Meteos::OnUserUpdate(float fElapsedTime) goto nextClump; } } - c.y+=c.vspeed*fElapsedTime; - for (int j=0;j>matchedBlockIDs; //Col followed by index for (int i=0;i>matchedBlockIDs; //Col followed by index for (Block&b:gameBoard.getBlocks(i)) { b.addedToLaunchList=false; } @@ -311,35 +308,33 @@ bool Meteos::OnUserUpdate(float fElapsedTime) } } } - if (matchedBlockIDs.size()>0) { - BlockClump c; - bool firstBlock=true; - int baseBlockPos; - for (std::pair&info:matchedBlockIDs) { - Block&b=gameBoard.getBlocks(info.first)[info.second]; - if (firstBlock) { - baseBlockPos=b.pos.y; - c.y=baseBlockPos-1; - firstBlock=false; - } - b.col=BlockColor::LAUNCHED; - c.vspeed=gameBoard.launchSpd; + } + if (matchedBlockIDs.size()>0) { + BlockClump c; + bool firstBlock=true; + int baseBlockPos; + for (std::pair&info:matchedBlockIDs) { + Block&b=gameBoard.getBlocks(info.first)[info.second]; + if (firstBlock) { + baseBlockPos=b.pos.y; + c.y=baseBlockPos-1; + firstBlock=false; } - for (std::pair&info:matchedBlockIDs) { - Block&b=gameBoard.getBlocks(info.first)[info.second]; - for (Block&b2:gameBoard.getBlocks(b.pos.x/12)) { - if (!b2.markedForRemoval&&b2.pos.y<=b.pos.y) { - if (b.pos.x==b2.pos.x&&b.pos.y==b2.pos.y){ - c.addBlockOnTopOf(b.pos.x/12,b2.col,b2.pos.y-baseBlockPos); - } else { - c.addBlockOnTopOf(b.pos.x/12,b2.col,0); - } - b2.markedForRemoval=true; - } + b.col=BlockColor::LAUNCHED; + c.vspeed=gameBoard.launchSpd; + } + for (std::pair&info:matchedBlockIDs) { + Block&b=gameBoard.getBlocks(info.first)[info.second]; + for (Block&b2:gameBoard.getBlocks(b.pos.x/12)) { + if (!b2.markedForRemoval&&b2.pos.y<=b.pos.y) { + c.addBlock(b.pos.x/12,(b2.pos.y-baseBlockPos)/12,b2.col); + b2.markedForRemoval=true; } } - gameBoard.addClump(c); } + gameBoard.addClump(c); + } + for (int i=0;i