#define OLC_PGE_APPLICATION #include "pixelGameEngine.h" #include #include "gameDefines.h" #include Meteos*game; struct BlockMatchingInfo{ int col; int ind; int c=-1; }; bool Meteos::OnUserCreate() { game=this; 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() randBlockPos=std::uniform_int_distribution<>(0, 9); coinFlip=std::uniform_int_distribution<>(0, 1); comboOverlayOffset=std::uniform_int_distribution<>(-16,16); 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,1.0f,{3,3,1,2,3,0,0,0,0,0}, 60*10,SPRITES["blocks_test.png"]); return true; } bool Meteos::OnUserUpdate(float fElapsedTime) { if (gameCanRun){ accumulatedTime+=fElapsedTime; if (accumulatedTime>=1/60.0f) { updateGame(1/60.0f); accumulatedTime=0; } handleInput(); } drawGame(fElapsedTime,!gameCanRun); return true; } void Meteos::handleInput(){ if (GetMouse(0).bHeld){ int mouseX=(GetMouseX()-gameBoard.drawOffset.x); int mouseY=(GetMouseY()-gameBoard.drawOffset.y); int mouseCol=mouseX/12; int mouseRow=mouseY/12; if (mouseCol>=0&&mouseCol=mouseY){ SelectedBlockData newData={mouseCol,j,i}; if (gameBoard.selectedBlock.col!=-1&&mouseCol==gameBoard.selectedBlock.col){ //See if these two can be swapped. Block&b1=gameBoard.getBlock(gameBoard.selectedBlock); Block&b2=gameBoard.getBlock(newData); int y1=(gameBoard.selectedBlock.c!=-1)?gameBoard.getBlockClumps()[gameBoard.selectedBlock.c].getBlockPosition(gameBoard.getBlockClumps()[gameBoard.selectedBlock.c].getBlocks()[gameBoard.selectedBlock.ind]).y:gameBoard.getBlocks(gameBoard.selectedBlock.col)[gameBoard.selectedBlock.ind].pos.y; int y2=(newData.c!=-1)?gameBoard.getBlockClumps()[newData.c].getBlockPosition(gameBoard.getBlockClumps()[newData.c].getBlocks()[newData.ind]).y:gameBoard.getBlocks(newData.col)[newData.ind].pos.y; if (b1.pos.x==b2.pos.x&&std::abs(y1-y2)<=12){ //Swap the two! BlockColor tempCol=b1.col; b1.col=b2.col; b2.col=tempCol; } } gameBoard.selectedBlock=newData; goto found; } } } found:; } if (gameBoard.selectedBlock.c==-1&&gameBoard.selectedBlock.col!=-1&&gameBoard.boardSize.y-mouseRow>gameBoard.getBlocks(gameBoard.selectedBlock.col).size()){ //Fling the block upwards. BlockClump newClump; Block&targetBlock=gameBoard.getBlocks(gameBoard.selectedBlock.col)[gameBoard.selectedBlock.ind]; newClump.y=targetBlock.pos.y-1; newClump.launchTime=999999; newClump.vspeed=gameBoard.launchSpd[0]; newClump.flinged=true; newClump.addBlock(gameBoard.selectedBlock.col,targetBlock.col); gameBoard.addClump(newClump); gameBoard.removeBlock(gameBoard.selectedBlock.col,gameBoard.selectedBlock.ind); gameBoard.selectedBlock={-1,-1,-1}; } } if (GetMouse(0).bReleased){ gameBoard.selectedBlock={-1,-1,-1}; } } void Meteos::updateGame(float fElapsedTime){ lastBlockSpawn+=fElapsedTime; if (lastBlockSpawn>=gameBoard.spawnRate){ lastBlockSpawn-=gameBoard.spawnRate; gameBoard.spawnBlock(randBlockPos(gen)); } for (int i=gameBoard.getBlockClumps().size()-1;i>=0;i--){ BlockClump&c=gameBoard.getBlockClumps()[i]; if (c.launchTime>0) { c.launchTime-=fElapsedTime; } else if (c.combo>0) { c.vspeed+=gameBoard.comboGravity; if (c.vspeed>gameBoard.comboMaxGravity){ c.vspeed=gameBoard.comboMaxGravity; } } else { c.vspeed+=gameBoard.gravity; if (c.vspeed>gameBoard.maxGravity){ c.vspeed=gameBoard.maxGravity; } } if (c.flinged&&c.y<12*3){ c.flinged=false; c.launchTime=0; } for (int j=gameBoard.getBlockClumps().size()-1;j>=0;j--) { if (i==j) continue; BlockClump&c2=gameBoard.getBlockClumps()[j]; for (int k=c2.getBlocks().size()-1;k>=0;k--) { Block&b2=c2.getBlocks()[k]; for (int l=c.getBlocks().size()-1;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&& c2.getBlockPosition(b2).y<=c.getBlockPosition(b3).y+12) { c.y=(int)c.y; c2.y=(int)c2.y; if (c2.getBlockPosition(b2).y>c.getBlockPosition(b3).y) { c.y-=12-(c2.getBlockPosition(b2).y-c.getBlockPosition(b3).y); c.y-=fmod(c.y-c2.y,12); if (fmod(c2.y-c.y,12)!=0){ std::cout<<"Difference is not divisible by 12!! Value:"<=j){ i--; } gameBoard.removeClump(j); goto nextClumpCollisionCheck; } } } } nextClumpCollisionCheck:; for (int j=0;j=b2.pos.y&&c.getBlockPosition(b).y<=b2.pos.y+12){ c.y-=c.getBlockPosition(b).y+12-b2.pos.y; c.vspeed=0; if (c.landTime>=gameBoard.landTime) { gameBoard.convertClump(i); } else { c.landTime+=fElapsedTime; } goto nextClump; } } if (c.getBlockPosition(b).y>=gameBoard.yBottom) { c.y-=c.getBlockPosition(b).y-gameBoard.yBottom; c.vspeed=0; if (c.landTime>=gameBoard.landTime) { gameBoard.convertClump(i); } else { c.landTime+=fElapsedTime; } goto nextClump; } } c.landTime=0; nextClump:; } for (BlockClump&c:gameBoard.getBlockClumps()){ std::vectormatchedBlockIDs; for (Block&b:c.getBlocks()) { b.addedToLaunchList=false; } for (int i=0;itempMatchIDsX; std::vectortempMatchIDsY; float targetX=b.pos.x; float targetY=b.pos.y; bool found=false; tempMatchIDsX.push_back(i); tempMatchIDsY.push_back(i); rightCheck: float checkX=targetX+12; float checkY=targetY; do{ found=false; for (int j=0;j2||tempMatchIDsY.size()>2) { if (tempMatchIDsX.size()>2) { for (int i:tempMatchIDsX) { Block&bb=c.getBlocks()[i]; if (!bb.addedToLaunchList) { bb.addedToLaunchList=true; matchedBlockIDs.push_back(i); } } } if (tempMatchIDsY.size()>2) { for (int i:tempMatchIDsY) { Block&bb=c.getBlocks()[i]; if (!bb.addedToLaunchList) { bb.addedToLaunchList=true; matchedBlockIDs.push_back(i); } } } } } for (int i:matchedBlockIDs) { Block&b=c.getBlocks()[i]; b.col=BlockColor::LAUNCHED; } if (matchedBlockIDs.size()>0){ c.vspeed=gameBoard.launchSpd[std::min(c.combo,(int)gameBoard.launchSpd.size()-1)]/(1+(c.getBlocks().size()*gameBoard.blockWeight)); c.launchTime=gameBoard.launchTime; c.combo++; vi2d randomOffset={comboOverlayOffset(gen),comboOverlayOffset(gen)}; comboDisplayList.push_back({c.getBlockPosition(c.getBlocks()[matchedBlockIDs[0]])+randomOffset,c.combo}); //std::cout<<"Combo value is "<matchedBlockIDs; //Col followed by index for (int i=0;itempMatchIDsX; //Col followed by index std::vectortempMatchIDsY; //Col followed by index float targetX=b.pos.x; float targetY=b.pos.y; bool found=false; tempMatchIDsX.push_back({i,j}); tempMatchIDsY.push_back({i,j}); rightBoardCheck: float checkX=1; float checkY=targetY; do{ found=false; if (i+checkX0) { for (int l=0;l=0) { for (int k=0;k0) { for (int l=0;l0) { for (int l=0;l0) { for (int l=0;l2||tempMatchIDsY.size()>2) { if (tempMatchIDsX.size()>2) { for (BlockMatchingInfo&info:tempMatchIDsX) { if (info.c!=-1) { BlockClump&c=gameBoard.getBlockClumps()[info.c]; Block&bb=c.getBlocks()[info.ind]; if (!bb.addedToLaunchList){ bb.addedToLaunchList=true; matchedBlockIDs.push_back(info); } } else { Block&bb=gameBoard.getBlocks(info.col)[info.ind]; if (!bb.addedToLaunchList){ bb.addedToLaunchList=true; matchedBlockIDs.push_back(info); } } } } if (tempMatchIDsY.size()>2) { for (BlockMatchingInfo&info:tempMatchIDsY) { if (info.c!=-1) { BlockClump&c=gameBoard.getBlockClumps()[info.c]; Block&bb=c.getBlocks()[info.ind]; if (!bb.addedToLaunchList){ bb.addedToLaunchList=true; matchedBlockIDs.push_back(info); } } else { Block&bb=gameBoard.getBlocks(info.col)[info.ind]; if (!bb.addedToLaunchList){ bb.addedToLaunchList=true; matchedBlockIDs.push_back(info); } } } } } } } if (matchedBlockIDs.size()>0) { BlockClump c; bool firstBlock=true; vf2d baseBlockPos; for (BlockMatchingInfo&info:matchedBlockIDs) { if (info.c!=-1){ Block&b=gameBoard.getBlockClumps()[info.c].getBlocks()[info.ind]; if (firstBlock) { baseBlockPos=gameBoard.getBlockClumps()[info.c].getBlockPosition(b); c.y=baseBlockPos.y-1; firstBlock=false; } b.col=BlockColor::LAUNCHED; } else { Block&b=gameBoard.getBlocks(info.col)[info.ind]; if (firstBlock) { baseBlockPos=b.pos; c.y=baseBlockPos.y-1; firstBlock=false; } b.col=BlockColor::LAUNCHED; } } for (BlockMatchingInfo&info:matchedBlockIDs) { if (info.c!=-1){ if (gameBoard.getBlockClumps()[info.c].getBlocks().size()>0){ if (c.combo=0;i--) { Block&b2=gameBoard.getBlockClumps()[info.c].getBlocks()[i]; if ((int)fmod(std::abs(std::round(gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y)-std::round(baseBlockPos.y)),12)!=0){ std::cout<<"Difference is not equal to 0! Value:"<<(int)fmod(std::abs(std::round(gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y)-std::round(baseBlockPos.y)),12)<<" "<=0;i--){ Block&b2=gameBoard.getBlocks(info.col)[i]; if (b2.pos.y<=yPos){ if ((int)fmod((int)b2.pos.y-(int)baseBlockPos.y,12)!=0){ std::cout<<"Difference is not equal to 0! Value:"<<(int)fmod((int)b2.pos.y-(int)baseBlockPos.y,12)<<" "<=0;i--){ //Resolve BlockClump movements. BlockClump&c=gameBoard.getBlockClumps()[i]; for (int j=c.getBlocks().size()-1;j>=0;j--){ Block&b=c.getBlocks()[j]; b.addedToLaunchList=false; if (c.vspeed<0&&c.getBlockPosition(b).y<0||b.markedForRemoval){ std::cout<<"Removed block @"<=gameBoard.boardSize.y){ gameBoard.warningFlashingLevel[i]+=fElapsedTime; gameBoard.warningLevel++; noWarning=false; } else { gameBoard.warningFlashingLevel[i]=0; } for (int y=gameBoard.getBlocks(i).size()-1;y>=0;y--){ Block&b=gameBoard.getBlocks(i)[y]; b.addedToLaunchList=false; if (b.markedForRemoval){ gameBoard.removeBlock(i,y); continue; } } } if (noWarning){ gameBoard.warningLevel=0; } else if (gameBoard.warningLevel>=gameBoard.maxWarningLevel){ //Planet crashes! For now we just clear all blocks and clumps on the board. while (gameBoard.getBlockClumps().size()>0){ gameBoard.removeClump(0); } for (int i=0;i0){ gameBoard.removeBlock(i,0); } } gameBoard.warningLevel=0; goto invalidatedExit; } //Invalidate the selected block if something is strange about it. if (gameBoard.selectedBlock.col!=-1){ if (gameBoard.selectedBlock.c!=-1){ if (!(gameBoard.selectedBlock.c=0;i--){ ComboOverlay&c=comboDisplayList[i]; c.lifetime++; c.pos.y-=0.1; if (c.lifetime>60){ comboDisplayList.erase(comboDisplayList.begin()+i); } } } 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++){ 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)); } } } int alpha=(debugView)?32:255; for (int i=0;i0){ FillRectDecal({gameBoard.drawOffset.x+i*12,gameBoard.drawOffset.y},{12,(float)gameBoard.boardSize.y*12},Pixel(255,64,64,std::abs(sin(gameBoard.warningFlashingLevel[i]*M_PI))*128)); } } for (int i=0;i=3)?Pixel{207,103,0}:Pixel{255,255,255,180},{0.8,0.6}); } } int main() { Meteos instance; if (instance.Construct(256, 240, 4, 4)) instance.Start(); return 0; }