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. 111
      main.cpp

Binary file not shown.

@ -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<std::string,Renderable> SPRITES;
bool OnUserCreate()override;
void updateGame(float fElapsedTime);
void drawGame();
bool OnUserUpdate(float fElapsedTime)override;
};
#endif

@ -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;i<gameBoard.getBlockClumps().size();i++){
BlockClump&c=gameBoard.getBlockClumps()[i];
c.vspeed+=gameBoard.gravity*fElapsedTime;
c.vspeed+=gameBoard.gravity;
if (c.vspeed>gameBoard.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<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});
}
c.y+=c.vspeed;
nextClump:;
}
for (BlockClump&c:gameBoard.getBlockClumps()){
@ -210,8 +207,8 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
c.vspeed=gameBoard.launchSpd;
}
}
std::vector<std::pair<int,int>>matchedBlockIDs; //Col followed by index
for (int i=0;i<gameBoard.boardSize.x;i++){
std::vector<std::pair<int,int>>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<int,int>&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<int,int>&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<int,int>&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<int,int>&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<gameBoard.boardSize.x;i++){
for (int y=0;y<gameBoard.getBlocks(i).size();y++){
Block&b=gameBoard.getBlocks(i)[y];
if (b.markedForRemoval){
@ -347,10 +342,34 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
continue;
}
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});
}
}
return true;
}
int main()

Loading…
Cancel
Save