Player input is now working

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 1daf3810c7
commit cc284ef32c
  1. 13
      Board.cpp
  2. 8
      Board.h
  3. BIN
      C++ProjectTemplate
  4. 97
      main.cpp

@ -12,11 +12,6 @@ Board::Board(vi2d boardSize,float gravity,float maxGravity,float launchSpd,float
for (int i=0;i<boardSize.x;i++) { for (int i=0;i<boardSize.x;i++) {
cols.push_back(std::vector<Block>()); cols.push_back(std::vector<Block>());
} }
cols[3].push_back({{3*12,6*12},BlockColor::RED});
cols[4].push_back({{4*12,8*12},BlockColor::RED});
cols[4].push_back({{4*12,7*12},BlockColor::RED});
cols[4].push_back({{4*12,6*12},BlockColor::RED});
cols[5].push_back({{5*12,6*12},BlockColor::RED});
} }
void Board::spawnBlock(int col){ void Board::spawnBlock(int col){
@ -113,4 +108,12 @@ void Board::removeBlock(int col,int ind){
void Board::assignRandomColor(Block&b){ void Board::assignRandomColor(Block&b){
b.col=colorHandler.getRandomColor(); b.col=colorHandler.getRandomColor();
}
Block&Board::getBlock(SelectedBlockData&blockData){
if (blockData.c!=-1){
return clumps[blockData.c].getBlocks()[blockData.ind];
} else {
return cols[blockData.col][blockData.ind];
}
} }

@ -28,6 +28,12 @@ class ColorDistributor{
BlockColor getRandomColor(); BlockColor getRandomColor();
}; };
struct SelectedBlockData{
int col=-1;
int ind=-1;
int c=-1;
};
class Board{ class Board{
std::vector<std::vector<Block>> cols; std::vector<std::vector<Block>> cols;
std::vector<BlockClump> clumps; std::vector<BlockClump> clumps;
@ -43,6 +49,7 @@ class Board{
float launchSpd; float launchSpd;
float launchTime; //How much time a stack rises before beginning descent. float launchTime; //How much time a stack rises before beginning descent.
float landTime; float landTime;
SelectedBlockData selectedBlock;
Board(); Board();
Board(vi2d boardSize,float gravity,float maxGravity,float launchSpd,float launchTime,float landTime,float spawnRate,std::array<int,10> colorRates,Renderable&tileset); Board(vi2d boardSize,float gravity,float maxGravity,float launchSpd,float launchTime,float landTime,float spawnRate,std::array<int,10> colorRates,Renderable&tileset);
void spawnBlock(int col); void spawnBlock(int col);
@ -53,5 +60,6 @@ class Board{
void removeClump(int ind); void removeClump(int ind);
void removeBlock(int col,int ind); void removeBlock(int col,int ind);
void assignRandomColor(Block&b); void assignRandomColor(Block&b);
Block&getBlock(SelectedBlockData&blockData);
}; };
#endif #endif

Binary file not shown.

@ -19,11 +19,11 @@ bool Meteos::OnUserCreate()
SPRITES["blocks_test.png"].Load("assets/blocks_test.png"); 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 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() 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); 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"]); gameBoard=Board({10,14},0.04f,1.2f,-1.f,1.7f,2.0f,1.0f,{3,3,1,2,3,0,0,0,0,0},SPRITES["blocks_test.png"]);
return true; return true;
} }
@ -418,25 +418,23 @@ void Meteos::updateGame(float fElapsedTime){
bool firstBlock=true; bool firstBlock=true;
int baseBlockPos; int baseBlockPos;
for (BlockMatchingInfo&info:matchedBlockIDs) { for (BlockMatchingInfo&info:matchedBlockIDs) {
Block*b;
if (info.c!=-1){ if (info.c!=-1){
b=&gameBoard.getBlockClumps()[info.c].getBlocks()[info.ind]; Block&b=gameBoard.getBlockClumps()[info.c].getBlocks()[info.ind];
if (firstBlock) { if (firstBlock) {
baseBlockPos=gameBoard.getBlockClumps()[info.c].getBlockPosition(*b).y; baseBlockPos=gameBoard.getBlockClumps()[info.c].getBlockPosition(b).y;
c.y=baseBlockPos-1; c.y=baseBlockPos-1;
firstBlock=false; firstBlock=false;
} }
b.col=BlockColor::LAUNCHED;
} else { } else {
b=&gameBoard.getBlocks(info.col)[info.ind]; Block&b=gameBoard.getBlocks(info.col)[info.ind];
if (firstBlock) { if (firstBlock) {
baseBlockPos=b->pos.y; baseBlockPos=b.pos.y;
c.y=baseBlockPos-1; c.y=baseBlockPos-1;
firstBlock=false; firstBlock=false;
} }
b.col=BlockColor::LAUNCHED;
} }
b->col=BlockColor::LAUNCHED;
c.vspeed=gameBoard.launchSpd;
c.launchTime=gameBoard.launchTime;
} }
for (BlockMatchingInfo&info:matchedBlockIDs) { for (BlockMatchingInfo&info:matchedBlockIDs) {
if (info.c!=-1){ if (info.c!=-1){
@ -458,11 +456,14 @@ void Meteos::updateGame(float fElapsedTime){
} }
} }
gameBoard.addClump(c); 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=0;i<gameBoard.getBlockClumps().size();i++){ //Resolve BlockClump movements.
BlockClump&c=gameBoard.getBlockClumps()[i]; BlockClump&c=gameBoard.getBlockClumps()[i];
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];
b.addedToLaunchList=false;
if (c.vspeed<0&&c.getBlockPosition(b).y<0||b.markedForRemoval){ if (c.vspeed<0&&c.getBlockPosition(b).y<0||b.markedForRemoval){
c.removeBlock(j--); c.removeBlock(j--);
break; break;
@ -484,6 +485,78 @@ void Meteos::updateGame(float fElapsedTime){
b.addedToLaunchList=false; b.addedToLaunchList=false;
} }
} }
//Invalidate the selected block if something is strange about it.
if (gameBoard.selectedBlock.col!=-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)){
gameBoard.selectedBlock={-1};
goto invalidatedExit;
}
} else {
if (!(gameBoard.selectedBlock.ind<gameBoard.getBlocks(gameBoard.selectedBlock.col).size())){
gameBoard.selectedBlock={-1};
goto invalidatedExit;
}
}
}
invalidatedExit:
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<gameBoard.boardSize.x) {
for (int y=0;y<gameBoard.getBlocks(mouseCol).size();y++){
Block&b=gameBoard.getBlocks(mouseCol)[y];
if (b.pos.y/12==mouseRow){
SelectedBlockData newData={mouseCol,y,-1};
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;
}
}
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];
if (b.pos.x/12==mouseCol&&c.getBlockPosition(b).y<=mouseY&&c.getBlockPosition(b).y+12>=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 (GetMouse(0).bReleased){
gameBoard.selectedBlock={-1};
}
} }
void Meteos::drawGame(float fElapsedTime){ void Meteos::drawGame(float fElapsedTime){
@ -501,13 +574,13 @@ void Meteos::drawGame(float fElapsedTime){
BlockClump&c=gameBoard.getBlockClumps()[i]; BlockClump&c=gameBoard.getBlockClumps()[i];
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];
DrawPartialDecal(c.getBlockPosition(b)+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12}); 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);
} }
} }
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++){
Block&b=gameBoard.getBlocks(i)[y]; Block&b=gameBoard.getBlocks(i)[y];
DrawPartialDecal(b.pos+gameBoard.drawOffset,gameBoard.tileset,{(float)(int)b.col*12,0},{12,12}); 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);
} }
} }
} }

Loading…
Cancel
Save