#define OLC_PGE_APPLICATION #include "pixelGameEngine.h" #include #include "gameDefines.h" #include Meteos*game; 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(0); //Standard mersenne_twister_engine seeded with rd() randBlockPos=std::uniform_int_distribution<>(0, 9); coinFlip=std::uniform_int_distribution<>(0, 1); gameBoard=Board({10,14},0.04f,1.2f,-1.f,1.7f,2.0f,1.0f,{3,3,0,0,0,0,0,0,0,0},SPRITES["blocks_test.png"]); return true; } bool Meteos::OnUserUpdate(float fElapsedTime) { accumulatedTime+=fElapsedTime; if (accumulatedTime>=1/60.0f) { updateGame(1/60.0f); accumulatedTime=0; } drawGame(fElapsedTime); return true; } void Meteos::updateGame(float fElapsedTime){ lastBlockSpawn+=fElapsedTime; if (lastBlockSpawn>=gameBoard.spawnRate){ lastBlockSpawn-=gameBoard.spawnRate; gameBoard.spawnBlock(randBlockPos(gen)); } for (int i=0;i0) { c.launchTime-=fElapsedTime; } else if (c.landTime>0) { c.landTime-=fElapsedTime; if (c.landTime<=0) { c.landTime=0; for (int j=0;jgameBoard.maxGravity){ c.vspeed=gameBoard.maxGravity; } for (int j=0;j=c.getBlockPosition(b3).y&& c2.getBlockPosition(b2).y<=c.getBlockPosition(b3).y+12) { float yDiff=(c2.getBlockPosition(b2).y>c.getBlockPosition(b3).y)? c2.getBlockPosition(b2).y-c.getBlockPosition(b3).y: c.getBlockPosition(b3).y-c2.getBlockPosition(b2).y; c2.y+=(c2.getBlockPosition(b2).y>c.getBlockPosition(b3).y)? yDiff:-yDiff; float influence=(float)c.getBlocks().size()/(c.getBlocks().size()+c2.getBlocks().size()); int blockCount=c.getBlocks().size(); //Copy every block from one clump to the other for (int m=0;m0) { c.vspeed/=4; } if (c2.vspeed>0) { c2.vspeed/=4; } c.vspeed=c.vspeed*influence+c2.vspeed*(1-influence); c.sortBlocks(); 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; gameBoard.convertClump(i--); goto nextClump; } } if (c.getBlockPosition(b).y>=gameBoard.yBottom) { c.y-=c.getBlockPosition(b).y-gameBoard.yBottom; c.vspeed=0; gameBoard.convertClump(i--); goto nextClump; } } c.y+=c.vspeed; 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; c.vspeed=gameBoard.launchSpd; c.launchTime=gameBoard.launchTime; } } std::vector>matchedBlockIDs; //Col followed by index for (int i=0;i>tempMatchIDsX; //Col followed by index std::vector>tempMatchIDsY; //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+checkX=0) { for (int k=0;k2||tempMatchIDsY.size()>2) { if (tempMatchIDsX.size()>2) { for (std::pair&info:tempMatchIDsX) { Block&bb=gameBoard.getBlocks(info.first)[info.second]; if (!bb.addedToLaunchList) { bb.addedToLaunchList=true; matchedBlockIDs.push_back(info); } } } if (tempMatchIDsY.size()>2) { for (std::pair&info:tempMatchIDsY) { Block&bb=gameBoard.getBlocks(info.first)[info.second]; if (!bb.addedToLaunchList) { bb.addedToLaunchList=true; matchedBlockIDs.push_back(info); } } } } } } 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; c.launchTime=gameBoard.launchTime; } 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); } for (int i=0;i0) { b.resetTime-=fElapsedTime; if (b.resetTime<0) { b.resetTime=0; if (b.col==BlockColor::LAUNCHED) { gameBoard.assignRandomColor(b); } } } DrawPartialDecal(c.getBlockPosition(b)+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12}); } } for (int i=0;i0) { b.resetTime-=fElapsedTime; if (b.resetTime<0) { b.resetTime=0; if (b.col==BlockColor::LAUNCHED) { gameBoard.assignRandomColor(b); } } } DrawPartialDecal(b.pos+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12}); } } } int main() { Meteos instance; if (instance.Construct(256, 240, 4, 4)) instance.Start(); return 0; }