Cleaned up update functions

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 4010b52c5b
commit 9dc1ec3517
  1. BIN
      C++ProjectTemplate
  2. 10
      Meteos.h
  3. 53
      main.cpp

Binary file not shown.

@ -23,5 +23,15 @@ class Meteos : public olc::PixelGameEngine{
void handleInput(); void handleInput();
void drawGame(float fElapsedTime,bool debugView); void drawGame(float fElapsedTime,bool debugView);
bool OnUserUpdate(float fElapsedTime)override; bool OnUserUpdate(float fElapsedTime)override;
void spawnBlocks(float fElapsedTime);
void handleBlockGravity(float fElapsedTime);
void matchBlocks();
void matchFlyingBlockClumps();
void cleanupBlockClumps();
void matchGroundedBlocks();
void cleanupBlocks();
void handleWarningLevels(float fElapsedTime);
void validateSelectedBlock();
void handleComboAnimations();
}; };
#endif #endif

@ -25,7 +25,7 @@ bool Meteos::OnUserCreate()
coinFlip=std::uniform_int_distribution<>(0, 1); coinFlip=std::uniform_int_distribution<>(0, 1);
comboOverlayOffset=std::uniform_int_distribution<>(-16,16); comboOverlayOffset=std::uniform_int_distribution<>(-16,16);
gameBoard=Board({10,14},0.02f,0.04f,1.2f,0.04f,0.4f,{-0.2f,-1.5f,-2.f,-2.5f,-4.f},1.7f,2.0f,0.2f,{3,3,1,2,3,0,0,0,0,0}, gameBoard=Board({10,14},0.02f,0.04f,1.2f,0.04f,0.4f,{-1.f,-1.5f,-2.f,-2.5f,-4.f},1.7f,2.0f,0.2f,{3,3,1,2,3,0,0,0,0,0},
60*10,SPRITES["blocks_test.png"]); 60*10,SPRITES["blocks_test.png"]);
return true; return true;
@ -118,12 +118,15 @@ void Meteos::handleInput(){
} }
} }
void Meteos::updateGame(float fElapsedTime){ void Meteos::spawnBlocks(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));
} }
}
void Meteos::handleBlockGravity(float fElapsedTime){
for (int i=gameBoard.getBlockClumps().size()-1;i>=0;i--){ for (int i=gameBoard.getBlockClumps().size()-1;i>=0;i--){
BlockClump&c=gameBoard.getBlockClumps()[i]; BlockClump&c=gameBoard.getBlockClumps()[i];
if (c.launchTime>0) { if (c.launchTime>0) {
@ -262,6 +265,9 @@ void Meteos::updateGame(float fElapsedTime){
c.landTime=0; c.landTime=0;
nextClump:; nextClump:;
} }
}
void Meteos::matchFlyingBlockClumps(){
for (int g=0;g<gameBoard.getBlockClumps().size();g++){ for (int g=0;g<gameBoard.getBlockClumps().size();g++){
BlockClump&c=gameBoard.getBlockClumps()[g]; BlockClump&c=gameBoard.getBlockClumps()[g];
std::vector<BlockMatchingInfo>matchedBlockIDs; std::vector<BlockMatchingInfo>matchedBlockIDs;
@ -505,6 +511,9 @@ void Meteos::updateGame(float fElapsedTime){
//std::cout<<"Combo value is "<<c.combo<<std::endl; //std::cout<<"Combo value is "<<c.combo<<std::endl;
} }
} }
}
void Meteos::cleanupBlockClumps(){
for (int g=0;g<gameBoard.getBlockClumps().size();g++){ for (int g=0;g<gameBoard.getBlockClumps().size();g++){
BlockClump&c=gameBoard.getBlockClumps()[g]; BlockClump&c=gameBoard.getBlockClumps()[g];
for (int i=0;i<c.getBlocks().size();i++){ for (int i=0;i<c.getBlocks().size();i++){
@ -514,6 +523,9 @@ void Meteos::updateGame(float fElapsedTime){
} }
} }
} }
}
void Meteos::matchGroundedBlocks(){
std::vector<BlockMatchingInfo>matchedBlockIDs; //Col followed by index std::vector<BlockMatchingInfo>matchedBlockIDs; //Col followed by index
for (int i=0;i<gameBoard.boardSize.x;i++){ for (int i=0;i<gameBoard.boardSize.x;i++){
for (Block&b:gameBoard.getBlocks(i)) { for (Block&b:gameBoard.getBlocks(i)) {
@ -762,6 +774,15 @@ void Meteos::updateGame(float fElapsedTime){
comboDisplayList.push_back({baseBlockPos+randomOffset,gameBoard.getBlockClumps()[gameBoard.getBlockClumps().size()-1].combo}); comboDisplayList.push_back({baseBlockPos+randomOffset,gameBoard.getBlockClumps()[gameBoard.getBlockClumps().size()-1].combo});
//std::cout<<"Combo value is "<<gameBoard.getBlockClumps()[gameBoard.getBlockClumps().size()-1].combo<<std::endl; //std::cout<<"Combo value is "<<gameBoard.getBlockClumps()[gameBoard.getBlockClumps().size()-1].combo<<std::endl;
} }
}
void Meteos::matchBlocks(){
matchFlyingBlockClumps();
cleanupBlockClumps();
matchGroundedBlocks();
}
void Meteos::cleanupBlocks(){
for (int i=gameBoard.getBlockClumps().size()-1;i>=0;i--){ //Resolve BlockClump movements. for (int i=gameBoard.getBlockClumps().size()-1;i>=0;i--){ //Resolve BlockClump movements.
BlockClump&c=gameBoard.getBlockClumps()[i]; BlockClump&c=gameBoard.getBlockClumps()[i];
for (int j=c.getBlocks().size()-1;j>=0;j--){ for (int j=c.getBlocks().size()-1;j>=0;j--){
@ -770,7 +791,6 @@ void Meteos::updateGame(float fElapsedTime){
if (c.vspeed<0&&c.getBlockPosition(b).y<0||b.markedForRemoval){ if (c.vspeed<0&&c.getBlockPosition(b).y<0||b.markedForRemoval){
std::cout<<"Removed block @"<<b.pos<<" from clump "<<i<<std::endl; std::cout<<"Removed block @"<<b.pos<<" from clump "<<i<<std::endl;
c.removeBlock(j); c.removeBlock(j);
break;
} }
} }
c.y+=c.vspeed; c.y+=c.vspeed;
@ -780,6 +800,9 @@ void Meteos::updateGame(float fElapsedTime){
continue; continue;
} }
} }
}
void Meteos::handleWarningLevels(float fElapsedTime){
bool noWarning=true; bool noWarning=true;
for (int i=0;i<gameBoard.boardSize.x;i++){ for (int i=0;i<gameBoard.boardSize.x;i++){
if (gameBoard.getBlocks(i).size()>=gameBoard.boardSize.y){ if (gameBoard.getBlocks(i).size()>=gameBoard.boardSize.y){
@ -812,23 +835,27 @@ void Meteos::updateGame(float fElapsedTime){
} }
} }
gameBoard.warningLevel=0; gameBoard.warningLevel=0;
goto invalidatedExit;
} }
}
void Meteos::validateSelectedBlock(){
//Invalidate the selected block if something is strange about it. //Invalidate the selected block if something is strange about it.
if (gameBoard.selectedBlock.col!=-1){ if (gameBoard.selectedBlock.col!=-1){
if (gameBoard.selectedBlock.c!=-1){ if (gameBoard.selectedBlock.c!=-1){
if (!(gameBoard.selectedBlock.c<gameBoard.getBlockClumps().size()&&gameBoard.selectedBlock.ind<gameBoard.getBlockClumps()[gameBoard.selectedBlock.c].getBlocks().size()&&gameBoard.getBlockClumps()[gameBoard.selectedBlock.c].getBlocks()[gameBoard.selectedBlock.ind].pos.x/12==gameBoard.selectedBlock.col)){ if (!(gameBoard.selectedBlock.c<gameBoard.getBlockClumps().size()&&gameBoard.selectedBlock.ind<gameBoard.getBlockClumps()[gameBoard.selectedBlock.c].getBlocks().size()&&gameBoard.getBlockClumps()[gameBoard.selectedBlock.c].getBlocks()[gameBoard.selectedBlock.ind].pos.x/12==gameBoard.selectedBlock.col)){
gameBoard.selectedBlock={-1}; gameBoard.selectedBlock={-1};
goto invalidatedExit; return;
} }
} else { } else {
if (!(gameBoard.selectedBlock.ind<gameBoard.getBlocks(gameBoard.selectedBlock.col).size())){ if (!(gameBoard.selectedBlock.ind<gameBoard.getBlocks(gameBoard.selectedBlock.col).size())){
gameBoard.selectedBlock={-1}; gameBoard.selectedBlock={-1};
goto invalidatedExit; return;
} }
} }
} }
invalidatedExit: }
void Meteos::handleComboAnimations(){
for (int i=comboDisplayList.size()-1;i>=0;i--){ for (int i=comboDisplayList.size()-1;i>=0;i--){
ComboOverlay&c=comboDisplayList[i]; ComboOverlay&c=comboDisplayList[i];
c.lifetime++; c.lifetime++;
@ -839,6 +866,16 @@ void Meteos::updateGame(float fElapsedTime){
} }
} }
void Meteos::updateGame(float fElapsedTime){
spawnBlocks(fElapsedTime);
handleBlockGravity(fElapsedTime);
matchBlocks();
cleanupBlocks();
handleWarningLevels(fElapsedTime);
validateSelectedBlock();
handleComboAnimations();
}
void Meteos::drawGame(float fElapsedTime,bool debugView){ void Meteos::drawGame(float fElapsedTime,bool debugView){
Clear(Pixel(32,32,255)); Clear(Pixel(32,32,255));
for (int x=-1;x<=gameBoard.boardSize.x;x++){ for (int x=-1;x<=gameBoard.boardSize.x;x++){

Loading…
Cancel
Save