Attempt #7?!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Meteos/main.cpp

386 lines
11 KiB

2 years ago
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include <random>
#include "gameDefines.h"
2 years ago
Meteos*game;
bool Meteos::OnUserCreate()
2 years ago
{
game=this;
2 years ago
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()
//Seed 0 causes a stacked block in the middle row followed by a giant stack up soon after.
randBlockPos=std::uniform_int_distribution<>(0, 9);
coinFlip=std::uniform_int_distribution<>(0, 1);
gameBoard=Board({10,14},0.04f,0.7f,-1.f,1.7f,1.0f,{3,0,0,0,0,0,0,0,0,0},SPRITES["blocks_test.png"]);
return true;
}
2 years ago
bool Meteos::OnUserUpdate(float fElapsedTime)
{
accumulatedTime+=fElapsedTime;
while (accumulatedTime>=1/60.0f) {
updateGame(accumulatedTime);
accumulatedTime-=1/60.0f;
}
drawGame();
return true;
}
void Meteos::updateGame(float fElapsedTime){
lastBlockSpawn+=fElapsedTime;
if (lastBlockSpawn>=gameBoard.spawnRate){
lastBlockSpawn-=gameBoard.spawnRate;
gameBoard.spawnBlock(randBlockPos(gen));
}
for (int i=0;i<gameBoard.getBlockClumps().size();i++){
BlockClump&c=gameBoard.getBlockClumps()[i];
if (c.launchTime>0) {
c.launchTime-=fElapsedTime;
} else {
c.vspeed+=gameBoard.gravity;
}
if (c.vspeed>gameBoard.maxGravity){
c.vspeed=gameBoard.maxGravity;
}
for (int j=0;j<gameBoard.getBlockClumps().size();j++) {
if (i==j) continue;
BlockClump&c2=gameBoard.getBlockClumps()[j];
for (int k=0;k<c2.getBlocks().size();k++) {
Block&b2=c2.getBlocks()[k];
for (int l=0;l<c.getBlocks().size();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) {
float yDiff=c2.getBlockPosition(b2).y;
float snapYPos=c.getBlockPosition(b3).y;
yDiff-=snapYPos;
c2.y+=yDiff;
float influence=(float)c.getBlocks().size()/(c.getBlocks().size()+c2.getBlocks().size());
//Copy every block from one clump to the other
for (int m=0;m<c2.getBlocks().size();m++) {
Block&b4=c2.getBlocks()[m];
c.addBlock(b4.pos.x/12,(c2.getBlockPosition(b4).y-c.y)/12,b4.col);
}
if (c.vspeed>0) {
c.vspeed/=4;
}
if (c2.vspeed>0) {
c2.vspeed/=4;
}
c.vspeed=c.vspeed*influence+c2.vspeed*(1-influence);
gameBoard.removeClump(j--);
goto nextClumpCollisionCheck;
}
}
}
}
nextClumpCollisionCheck:;
for (int j=0;j<c.getBlocks().size();j++){
Block&b=c.getBlocks()[j];
int col=b.pos.x/12;
for (int k=0;k<gameBoard.getBlocks(col).size();k++){
Block&b2=gameBoard.getBlocks(col)[k];
if (c.getBlockPosition(b).y+12>=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::vector<int>matchedBlockIDs;
for (Block&b:c.getBlocks()) {
b.addedToLaunchList=false;
}
for (int i=0;i<c.getBlocks().size();i++) {
Block&b=c.getBlocks()[i];
std::vector<int>tempMatchIDsX;
std::vector<int>tempMatchIDsY;
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;j<c.getBlocks().size();j++){
Block&b2=c.getBlocks()[j];
if (i==j)continue;
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==checkX&&b2.pos.y==checkY) {
found=true;
checkX+=12;
tempMatchIDsX.push_back(j);
break;
}
}
}while(found);
leftCheck:
checkX=targetX-12;
checkY=targetY;
do{
found=false;
for (int j=0;j<c.getBlocks().size();j++){
Block&b2=c.getBlocks()[j];
if (i==j)continue;
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==checkX&&b2.pos.y==checkY) {
found=true;
checkX-=12;
tempMatchIDsX.push_back(j);
break;
}
}
}while(found);
upCheck:
checkX=targetX;
checkY=targetY-12;
do{
found=false;
for (int j=0;j<c.getBlocks().size();j++){
Block&b2=c.getBlocks()[j];
if (i==j)continue;
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==checkX&&b2.pos.y==checkY) {
found=true;
checkY-=12;
tempMatchIDsY.push_back(j);
break;
}
}
}while(found);
downCheck:
checkX=targetX;
checkY=targetY+12;
do{
found=false;
for (int j=0;j<c.getBlocks().size();j++){
Block&b2=c.getBlocks()[j];
if (i==j)continue;
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==checkX&&b2.pos.y==checkY) {
found=true;
checkY+=12;
tempMatchIDsY.push_back(j);
break;
}
}
}while(found);
if (tempMatchIDsX.size()>2||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<std::pair<int,int>>matchedBlockIDs; //Col followed by index
for (int i=0;i<gameBoard.boardSize.x;i++){
for (Block&b:gameBoard.getBlocks(i)) {
b.addedToLaunchList=false;
}
for (int j=0;j<gameBoard.getBlocks(i).size();j++) {
Block&b=gameBoard.getBlocks(i)[j];
std::vector<std::pair<int,int>>tempMatchIDsX; //Col followed by index
std::vector<std::pair<int,int>>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<gameBoard.boardSize.x) {
for (int k=0;k<gameBoard.getBlocks(i+checkX).size();k++){
Block&b2=gameBoard.getBlocks(i+checkX)[k];
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==b.pos.x+checkX*12&&b2.pos.y==checkY) {
found=true;
tempMatchIDsX.push_back({i+checkX,k});
checkX++;
break;
}
}
}
}while(found);
leftBoardCheck:
checkX=-1;
checkY=targetY;
do{
found=false;
if (i+checkX>=0) {
for (int k=0;k<gameBoard.getBlocks(i+checkX).size();k++){
Block&b2=gameBoard.getBlocks(i+checkX)[k];
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==b.pos.x+checkX*12&&b2.pos.y==checkY) {
found=true;
tempMatchIDsX.push_back({i+checkX,k});
checkX--;
break;
}
}
}
}while(found);
upBoardCheck:
checkX=targetX;
checkY=targetY-12;
do{
found=false;
for (int k=0;k<gameBoard.getBlocks(i).size();k++){
Block&b2=gameBoard.getBlocks(i)[k];
if (j==k)continue;
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==checkX&&b2.pos.y==checkY) {
found=true;
checkY-=12;
tempMatchIDsY.push_back({i,k});
break;
}
}
}while(found);
downBoardCheck:
checkX=targetX;
checkY=targetY+12;
do{
found=false;
for (int k=0;k<gameBoard.getBlocks(i).size();k++){
Block&b2=gameBoard.getBlocks(i)[k];
if (j==k)continue;
if (b.col==b2.col&&b2.col!=BlockColor::LAUNCHED&&b2.pos.x==checkX&&b2.pos.y==checkY) {
found=true;
checkY+=12;
tempMatchIDsY.push_back({i,k});
break;
}
}
}while(found);
if (tempMatchIDsX.size()>2||tempMatchIDsY.size()>2) {
if (tempMatchIDsX.size()>2) {
for (std::pair<int,int>&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<int,int>&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<int,int>&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<int,int>&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;i<gameBoard.boardSize.x;i++){
for (int y=0;y<gameBoard.getBlocks(i).size();y++){
Block&b=gameBoard.getBlocks(i)[y];
if (b.markedForRemoval){
gameBoard.removeBlock(i,y--);
continue;
}
b.addedToLaunchList=false;
}
}
}
void Meteos::drawGame(){
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));
}
}
}
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});
}
}
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});
}
2 years ago
}
}
2 years ago
int main()
{
Meteos instance;
if (instance.Construct(256, 240, 4, 4))
instance.Start();
2 years ago
return 0;
}