Block clumping implementation

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 95a110808c
commit 268d071e9b
  1. 4
      BlockClump.cpp
  2. 1
      BlockClump.h
  3. 19
      Board.cpp
  4. 1
      Board.h
  5. BIN
      C++ProjectTemplate
  6. 2
      Meteos.h
  7. 15
      main.cpp

@ -8,6 +8,10 @@ void BlockClump::addBlock(int col){
blocks.push_back(Block({(float)col*12,0})); blocks.push_back(Block({(float)col*12,0}));
} }
void BlockClump::addBlock(vi2d offset){
blocks.push_back(Block(offset));
}
vf2d BlockClump::getBlockPosition(Block&b){ vf2d BlockClump::getBlockPosition(Block&b){
return {b.pos.x,b.pos.y+y}; return {b.pos.x,b.pos.y+y};
} }

@ -13,6 +13,7 @@ class BlockClump{
BlockClump(){} BlockClump(){}
std::vector<Block>&getBlocks(); std::vector<Block>&getBlocks();
void addBlock(int col); void addBlock(int col);
void addBlock(vi2d offset);
//Get a block's position relative to this block clump. //Get a block's position relative to this block clump.
vf2d getBlockPosition(Block&b); vf2d getBlockPosition(Block&b);
}; };

@ -14,6 +14,16 @@ Board::Board(vi2d boardSize,float gravity,float spawnRate)
void Board::spawnBlock(int col){ void Board::spawnBlock(int col){
BlockClump c=BlockClump(); BlockClump c=BlockClump();
c.addBlock(col); c.addBlock(col);
if (game->coinFlip(game->gen)==0) {
if (col>0) {
c.addBlock(col-1);
}
}
if (game->coinFlip(game->gen)==0) {
if (col<boardSize.x-1) {
c.addBlock(col+1);
}
}
clumps.push_back(c); clumps.push_back(c);
} }
@ -24,3 +34,12 @@ std::vector<Block>&Board::getBlocks(int col){
std::vector<BlockClump>&Board::getBlockClumps(){ std::vector<BlockClump>&Board::getBlockClumps(){
return clumps; return clumps;
} }
void Board::removeClump(int ind){
BlockClump&c=clumps[ind];
for (Block b:c.getBlocks()){
cols[b.pos.x/12].push_back(b);
cols[b.pos.x/12][cols[b.pos.x/12].size()-1].pos=c.getBlockPosition(b);
}
clumps.erase(clumps.begin()+ind);
}

@ -19,5 +19,6 @@ class Board{
void spawnBlock(int col); void spawnBlock(int col);
std::vector<Block>&getBlocks(int col); std::vector<Block>&getBlocks(int col);
std::vector<BlockClump>&getBlockClumps(); std::vector<BlockClump>&getBlockClumps();
void removeClump(int ind);
}; };
#endif #endif

Binary file not shown.

@ -10,7 +10,7 @@ class Meteos : public olc::PixelGameEngine{
sAppName="Meteos"; sAppName="Meteos";
} }
float lastBlockSpawn=0.0f; float lastBlockSpawn=0.0f;
std::uniform_int_distribution<> randBlockPos; std::uniform_int_distribution<> randBlockPos,coinFlip;
std::mt19937 gen; std::mt19937 gen;
Board gameBoard; Board gameBoard;
bool OnUserCreate()override; bool OnUserCreate()override;

@ -12,6 +12,7 @@ bool Meteos::OnUserCreate()
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()
randBlockPos=std::uniform_int_distribution<>(0, 9); randBlockPos=std::uniform_int_distribution<>(0, 9);
coinFlip=std::uniform_int_distribution<>(0, 1);
gameBoard=Board({10,14},12.f,3.0f); gameBoard=Board({10,14},12.f,3.0f);
return true; return true;
@ -40,28 +41,28 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
for (int j=0;j<c.getBlocks().size();j++){ for (int j=0;j<c.getBlocks().size();j++){
Block&b=c.getBlocks()[j]; Block&b=c.getBlocks()[j];
int col=b.pos.x/12; int col=b.pos.x/12;
for (int k=0;k<gameBoard.getBlocks(j).size();k++){ for (int k=0;k<gameBoard.getBlocks(col).size();k++){
Block&b2=gameBoard.getBlocks(j)[k]; Block&b2=gameBoard.getBlocks(col)[k];
if (c.getBlockPosition(b).y+12>=b2.pos.y&&c.getBlockPosition(b).y<=b2.pos.y+12){ if (c.getBlockPosition(b).y+12>=b2.pos.y&&c.getBlockPosition(b).y<=b2.pos.y+12){
c.y=b2.pos.y-12; c.y=b2.pos.y-12;
c.vspeed=0; c.vspeed=0;
gameBoard.removeClump(c); gameBoard.removeClump(i--);
i--; goto nextClump;
} }
} }
if (c.getBlockPosition(b).y>=gameBoard.yBottom) { if (c.getBlockPosition(b).y>=gameBoard.yBottom) {
c.y=gameBoard.yBottom; c.y=gameBoard.yBottom;
c.vspeed=0; c.vspeed=0;
gameBoard.removeClump(c); gameBoard.removeClump(i--);
i--; goto nextClump;
} }
} }
applyVelocity:
for (int j=0;j<c.getBlocks().size();j++){ for (int j=0;j<c.getBlocks().size();j++){
Block&b=c.getBlocks()[j]; Block&b=c.getBlocks()[j];
c.y+=c.vspeed*fElapsedTime; c.y+=c.vspeed*fElapsedTime;
FillRectDecal(c.getBlockPosition(b)+gameBoard.drawOffset,{12,12},DARK_GREEN); FillRectDecal(c.getBlockPosition(b)+gameBoard.drawOffset,{12,12},DARK_GREEN);
} }
nextClump:;
} }
for (int i=0;i<gameBoard.boardSize.x;i++){ 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++){

Loading…
Cancel
Save