Fixed timesteps was the key

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 4745003c88
commit b87fbcce50
  1. BIN
      C++ProjectTemplate
  2. 3
      Meteos.h
  3. 71
      main.cpp

Binary file not shown.

@ -10,11 +10,14 @@ class Meteos : public olc::PixelGameEngine{
sAppName="Meteos"; sAppName="Meteos";
} }
float lastBlockSpawn=0.0f; float lastBlockSpawn=0.0f;
float accumulatedTime=0.0f;
std::uniform_int_distribution<> randBlockPos,coinFlip; std::uniform_int_distribution<> randBlockPos,coinFlip;
std::mt19937 gen; std::mt19937 gen;
Board gameBoard; Board gameBoard;
std::map<std::string,Renderable> SPRITES; std::map<std::string,Renderable> SPRITES;
bool OnUserCreate()override; bool OnUserCreate()override;
void updateGame(float fElapsedTime);
void drawGame();
bool OnUserUpdate(float fElapsedTime)override; bool OnUserUpdate(float fElapsedTime)override;
}; };
#endif #endif

@ -12,36 +12,37 @@ bool Meteos::OnUserCreate()
SPRITES["blocks_test.png"].Load("assets/blocks_test.png"); 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 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); randBlockPos=std::uniform_int_distribution<>(0, 9);
coinFlip=std::uniform_int_distribution<>(0, 1); 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; return true;
} }
bool Meteos::OnUserUpdate(float fElapsedTime) 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; lastBlockSpawn+=fElapsedTime;
if (lastBlockSpawn>=gameBoard.spawnRate){ if (lastBlockSpawn>=gameBoard.spawnRate){
lastBlockSpawn-=gameBoard.spawnRate; lastBlockSpawn-=gameBoard.spawnRate;
gameBoard.spawnBlock(randBlockPos(gen)); 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;i<gameBoard.getBlockClumps().size();i++){ for (int i=0;i<gameBoard.getBlockClumps().size();i++){
BlockClump&c=gameBoard.getBlockClumps()[i]; BlockClump&c=gameBoard.getBlockClumps()[i];
c.vspeed+=gameBoard.gravity*fElapsedTime; c.vspeed+=gameBoard.gravity;
if (c.vspeed>gameBoard.maxGravity){ if (c.vspeed>gameBoard.maxGravity){
c.vspeed=gameBoard.maxGravity; c.vspeed=gameBoard.maxGravity;
} }
@ -98,11 +99,7 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
goto nextClump; goto nextClump;
} }
} }
c.y+=c.vspeed*fElapsedTime; c.y+=c.vspeed;
for (int j=0;j<c.getBlocks().size();j++){
Block&b=c.getBlocks()[j];
DrawPartialDecal(c.getBlockPosition(b)+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12});
}
nextClump:; nextClump:;
} }
for (BlockClump&c:gameBoard.getBlockClumps()){ for (BlockClump&c:gameBoard.getBlockClumps()){
@ -210,8 +207,8 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
c.vspeed=gameBoard.launchSpd; c.vspeed=gameBoard.launchSpd;
} }
} }
for (int i=0;i<gameBoard.boardSize.x;i++){
std::vector<std::pair<int,int>>matchedBlockIDs; //Col followed by index std::vector<std::pair<int,int>>matchedBlockIDs; //Col followed by index
for (int i=0;i<gameBoard.boardSize.x;i++){
for (Block&b:gameBoard.getBlocks(i)) { for (Block&b:gameBoard.getBlocks(i)) {
b.addedToLaunchList=false; b.addedToLaunchList=false;
} }
@ -311,6 +308,7 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
} }
} }
} }
}
if (matchedBlockIDs.size()>0) { if (matchedBlockIDs.size()>0) {
BlockClump c; BlockClump c;
bool firstBlock=true; bool firstBlock=true;
@ -329,17 +327,14 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
Block&b=gameBoard.getBlocks(info.first)[info.second]; Block&b=gameBoard.getBlocks(info.first)[info.second];
for (Block&b2:gameBoard.getBlocks(b.pos.x/12)) { for (Block&b2:gameBoard.getBlocks(b.pos.x/12)) {
if (!b2.markedForRemoval&&b2.pos.y<=b.pos.y) { if (!b2.markedForRemoval&&b2.pos.y<=b.pos.y) {
if (b.pos.x==b2.pos.x&&b.pos.y==b2.pos.y){ c.addBlock(b.pos.x/12,(b2.pos.y-baseBlockPos)/12,b2.col);
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; b2.markedForRemoval=true;
} }
} }
} }
gameBoard.addClump(c); gameBoard.addClump(c);
} }
for (int i=0;i<gameBoard.boardSize.x;i++){
for (int y=0;y<gameBoard.getBlocks(i).size();y++){ for (int y=0;y<gameBoard.getBlocks(i).size();y++){
Block&b=gameBoard.getBlocks(i)[y]; Block&b=gameBoard.getBlocks(i)[y];
if (b.markedForRemoval){ if (b.markedForRemoval){
@ -347,10 +342,34 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
continue; continue;
} }
b.addedToLaunchList=false; b.addedToLaunchList=false;
}
}
}
void Meteos::drawGame(){
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;i<gameBoard.getBlockClumps().size();i++){
BlockClump&c=gameBoard.getBlockClumps()[i];
for (int j=0;j<c.getBlocks().size();j++){
Block&b=c.getBlocks()[j];
DrawPartialDecal(c.getBlockPosition(b)+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12});
}
}
for (int i=0;i<gameBoard.boardSize.x;i++){
for (int y=0;y<gameBoard.getBlocks(i).size();y++){
Block&b=gameBoard.getBlocks(i)[y];
DrawPartialDecal(b.pos+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12}); DrawPartialDecal(b.pos+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12});
} }
} }
return true;
} }
int main() int main()

Loading…
Cancel
Save