Fixed the block column block overlapping issues!

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 2bb304ccec
commit c3dc478839
  1. 6
      BlockClump.cpp
  2. 2
      BlockClump.h
  3. 2
      Board.cpp
  4. BIN
      C++ProjectTemplate
  5. 3
      Meteos.h
  6. 107
      main.cpp

@ -1,5 +1,11 @@
#include "gameDefines.h"
extern Meteos*game;
BlockClump::BlockClump(){
blocks.reserve(game->gameBoard.boardSize.x*game->gameBoard.boardSize.y);
}
std::vector<Block>&BlockClump::getBlocks(){
return blocks;
}

@ -12,7 +12,7 @@ class BlockClump{
float landTime=0.f;
float launchTime=0.f;
float y=0;
BlockClump(){}
BlockClump();
std::vector<Block>&getBlocks();
void addBlock(int col,BlockColor color);
void addBlock(int col,int row,BlockColor color);

@ -11,7 +11,9 @@ Board::Board(vi2d boardSize,float gravity,float maxGravity,float launchSpd,float
yBottom=(boardSize.y-1)*12;
for (int i=0;i<boardSize.x;i++) {
cols.push_back(std::vector<Block>());
cols[i].reserve(boardSize.y+10);
}
clumps.reserve(100);
}
void Board::spawnBlock(int col){

Binary file not shown.

@ -14,11 +14,12 @@ class Meteos : public olc::PixelGameEngine{
std::uniform_int_distribution<> randBlockPos,coinFlip;
std::mt19937 gen;
Board gameBoard;
bool gameCanRun=true;
std::map<std::string,Renderable> SPRITES;
bool OnUserCreate()override;
void updateGame(float fElapsedTime);
void handleInput();
void drawGame(float fElapsedTime);
void drawGame(float fElapsedTime,bool debugView);
bool OnUserUpdate(float fElapsedTime)override;
};
#endif

@ -19,7 +19,7 @@ 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(0); //Standard mersenne_twister_engine seeded with rd()
randBlockPos=std::uniform_int_distribution<>(0, 9);
coinFlip=std::uniform_int_distribution<>(0, 1);
@ -30,13 +30,15 @@ bool Meteos::OnUserCreate()
bool Meteos::OnUserUpdate(float fElapsedTime)
{
accumulatedTime+=fElapsedTime;
if (accumulatedTime>=1/60.0f) {
updateGame(1/60.0f);
accumulatedTime=0;
if (gameCanRun){
accumulatedTime+=fElapsedTime;
if (accumulatedTime>=1/60.0f) {
updateGame(1/60.0f);
accumulatedTime=0;
}
handleInput();
}
handleInput();
drawGame(fElapsedTime);
drawGame(fElapsedTime,!gameCanRun);
return true;
}
@ -52,7 +54,7 @@ void Meteos::updateGame(float fElapsedTime){
lastBlockSpawn-=gameBoard.spawnRate;
gameBoard.spawnBlock(randBlockPos(gen));
}
for (int i=0;i<gameBoard.getBlockClumps().size();i++){
for (int i=gameBoard.getBlockClumps().size()-1;i>=0;i--){
BlockClump&c=gameBoard.getBlockClumps()[i];
if (c.launchTime>0) {
c.launchTime-=fElapsedTime;
@ -62,12 +64,12 @@ void Meteos::updateGame(float fElapsedTime){
if (c.vspeed>gameBoard.maxGravity){
c.vspeed=gameBoard.maxGravity;
}
for (int j=0;j<gameBoard.getBlockClumps().size();j++) {
for (int j=gameBoard.getBlockClumps().size()-1;j>=0;j--) {
if (i==j) continue;
BlockClump&c2=gameBoard.getBlockClumps()[j];
for (int k=0;k<c2.getBlocks().size();k++) {
for (int k=c2.getBlocks().size()-1;k>=0;k--) {
Block&b2=c2.getBlocks()[k];
for (int l=0;l<c.getBlocks().size();l++) {
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&&
@ -96,6 +98,14 @@ void Meteos::updateGame(float fElapsedTime){
for (int m=0;m<c2.getBlocks().size();m++) {
Block&b4=c2.getBlocks()[m];
bool exists=false;
for (int n=0;n<c.getBlocks().size();n++){
if ((c.getBlocks()[n].pos.x)/12==(b4.pos.x)/12&&(c.getBlocks()[n].pos.y)/12==(c2.getBlockPosition(b4).y-c.y)/12){
std::cout<<"WARNING! Block overlapping detected at "<<c.getBlocks()[n].pos<<"!"<<std::endl;
exists=true;
blockCount--;
break;
}
}
if (!exists){
c.addBlock(b4.pos.x/12,(c2.getBlockPosition(b4).y-c.y)/12,b4.col);
}
@ -114,7 +124,10 @@ void Meteos::updateGame(float fElapsedTime){
}
}
}
assert(!overlappingBlocks);
if (overlappingBlocks){
gameCanRun=false;
return;
}
if (overlappingBlocks){
std::cout<<"Block b3 "<<b3.pos<<"!"<<std::endl;
}
@ -129,7 +142,7 @@ void Meteos::updateGame(float fElapsedTime){
if (i>=j){
i--;
}
gameBoard.removeClump(j--);
gameBoard.removeClump(j);
goto nextClumpCollisionCheck;
}
}
@ -145,7 +158,7 @@ void Meteos::updateGame(float fElapsedTime){
c.y-=c.getBlockPosition(b).y+12-b2.pos.y;
c.vspeed=0;
if (c.landTime>=gameBoard.landTime) {
gameBoard.convertClump(i--);
gameBoard.convertClump(i);
} else {
c.landTime+=fElapsedTime;
}
@ -156,7 +169,7 @@ void Meteos::updateGame(float fElapsedTime){
c.y-=c.getBlockPosition(b).y-gameBoard.yBottom;
c.vspeed=0;
if (c.landTime>=gameBoard.landTime) {
gameBoard.convertClump(i--);
gameBoard.convertClump(i);
} else {
c.landTime+=fElapsedTime;
}
@ -308,7 +321,7 @@ void Meteos::updateGame(float fElapsedTime){
Block&b2=c.getBlocks()[l];
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==b.pos.x+checkX*12&&c.getBlockPosition(b2).y==checkY){
found=true;
tempMatchIDsX.push_back({-1,l,k});
tempMatchIDsX.push_back({(int)(b2.pos.x/12),l,k});
checkX++;
goto outercheck2;
}
@ -340,7 +353,7 @@ void Meteos::updateGame(float fElapsedTime){
Block&b2=c.getBlocks()[l];
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==b.pos.x+checkX*12&&c.getBlockPosition(b2).y==checkY){
found=true;
tempMatchIDsX.push_back({-1,l,k});
tempMatchIDsX.push_back({(int)(b2.pos.x/12),l,k});
checkX--;
goto outercheck3;
}
@ -373,7 +386,7 @@ void Meteos::updateGame(float fElapsedTime){
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&c.getBlockPosition(b2).x==checkX&&c.getBlockPosition(b2).y==checkY){
found=true;
checkY-=12;
tempMatchIDsY.push_back({-1,l,k});
tempMatchIDsY.push_back({(int)(b2.pos.x/12),l,k});
goto outercheck4;
}
}
@ -404,7 +417,7 @@ void Meteos::updateGame(float fElapsedTime){
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&c.getBlockPosition(b2).x==checkX&&c.getBlockPosition(b2).y==checkY){
found=true;
checkY+=12;
tempMatchIDsY.push_back({-1,l,k});
tempMatchIDsY.push_back({(int)(b2.pos.x/12),l,k});
goto outercheck1;
}
}
@ -477,51 +490,66 @@ void Meteos::updateGame(float fElapsedTime){
}
for (BlockMatchingInfo&info:matchedBlockIDs) {
if (info.c!=-1){
Block&b=gameBoard.getBlockClumps()[info.c].getBlocks()[info.ind];
for (Block&b2:gameBoard.getBlockClumps()[info.c].getBlocks()){
if (!b2.markedForRemoval&&b.pos.x==b2.pos.x&&gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y<=gameBoard.getBlockClumps()[info.c].getBlockPosition(b).y){
c.addBlock(b.pos.x/12,(gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y-baseBlockPos)/12,b2.col);
b2.markedForRemoval=true;
if (gameBoard.getBlockClumps()[info.c].getBlocks().size()>0){
for (int i=gameBoard.getBlockClumps()[info.c].getBlocks().size()-1;i>=0;i--) {
Block&b2=gameBoard.getBlockClumps()[info.c].getBlocks()[i];
if ((int)fmod(std::abs((int)gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y-(int)baseBlockPos),12)!=0){
std::cout<<"Difference is not equal to 0! Value:"<<(int)fmod(std::abs((int)gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y-(int)baseBlockPos),12)<<" "<<gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y<<"//"<<baseBlockPos<<std::endl;
assert(false);
}
c.addBlock(b2.pos.x/12,((int)gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y-(int)baseBlockPos)/12,b2.col);
std::cout<<"Added block @"<<b2.pos.x/12<<((int)gameBoard.getBlockClumps()[info.c].getBlockPosition(b2).y-(int)baseBlockPos)/12<<std::endl;
gameBoard.getBlockClumps()[info.c].removeBlock(i);
}
} else {
std::cout<<"Blocks already added elsewhere"<<std::endl;
}
b.markedForRemoval=true;
} else {
Block&b=gameBoard.getBlocks(info.col)[info.ind];
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;
int yPos=b.pos.y;
for (int i=gameBoard.getBlocks(info.col).size()-1;i>=0;i--){
Block&b2=gameBoard.getBlocks(info.col)[i];
if (b2.pos.y<=yPos){
if ((int)fmod((int)b2.pos.y-(int)baseBlockPos,12)!=0){
std::cout<<"Difference is not equal to 0! Value:"<<(int)fmod((int)b2.pos.y-(int)baseBlockPos,12)<<" "<<b2.pos.y<<"//"<<baseBlockPos<<std::endl;
assert(false);
}
c.addBlock(info.col,((int)b2.pos.y-(int)baseBlockPos)/12,b2.col);
std::cout<<"Added block @"<<info.col<<((int)b2.pos.y-(int)baseBlockPos)/12<<std::endl;
//b2.markedForRemoval=true;
gameBoard.removeBlock(info.col,i);
}
}
b.markedForRemoval=true;
}
}
gameBoard.addClump(c);
gameBoard.getBlockClumps()[gameBoard.getBlockClumps().size()-1].vspeed=gameBoard.launchSpd;
gameBoard.getBlockClumps()[gameBoard.getBlockClumps().size()-1].launchTime=gameBoard.launchTime;
}
for (int i=0;i<gameBoard.getBlockClumps().size();i++){ //Resolve BlockClump movements.
for (int i=gameBoard.getBlockClumps().size()-1;i>=0;i--){ //Resolve BlockClump movements.
BlockClump&c=gameBoard.getBlockClumps()[i];
for (int j=0;j<c.getBlocks().size();j++){
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){
c.removeBlock(j--);
std::cout<<"Removed block @"<<b.pos<<" from clump "<<i<<std::endl;
c.removeBlock(j);
break;
}
}
c.y+=c.vspeed;
if (c.getBlocks().size()<=0) {
gameBoard.removeClump(i--);
std::cout<<"Block Clump "<<i<<" is empty. Removing."<<std::endl;
gameBoard.removeClump(i);
continue;
}
}
for (int i=0;i<gameBoard.boardSize.x;i++){
for (int y=0;y<gameBoard.getBlocks(i).size();y++){
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--);
gameBoard.removeBlock(i,y);
continue;
}
}
@ -597,7 +625,7 @@ void Meteos::updateGame(float fElapsedTime){
}
}
void Meteos::drawGame(float fElapsedTime){
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++){
@ -608,17 +636,18 @@ void Meteos::drawGame(float fElapsedTime){
}
}
}
int alpha=(debugView)?32:255;
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},{1,1},gameBoard.selectedBlock.col!=-1&&gameBoard.selectedBlock.ind==j&&gameBoard.selectedBlock.c==i?GREY:WHITE);
DrawPartialDecal(c.getBlockPosition(b)+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12},{1,1},gameBoard.selectedBlock.col!=-1&&gameBoard.selectedBlock.ind==j&&gameBoard.selectedBlock.c==i?Pixel(GREY.r,GREY.g,GREY.b,alpha):Pixel(WHITE.r,WHITE.g,WHITE.b,alpha));
}
}
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},{1,1},gameBoard.selectedBlock.col!=-1&&gameBoard.selectedBlock.c==-1&&gameBoard.selectedBlock.col==i&&gameBoard.selectedBlock.ind==y?GREY:WHITE);
DrawPartialDecal(b.pos+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12},{1,1},gameBoard.selectedBlock.col!=-1&&gameBoard.selectedBlock.c==-1&&gameBoard.selectedBlock.col==i&&gameBoard.selectedBlock.ind==y?Pixel(GREY.r,GREY.g,GREY.b,alpha):Pixel(WHITE.r,WHITE.g,WHITE.b,alpha));
}
}
}

Loading…
Cancel
Save