Blocks were converted to block clump structure

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent fe96cb8df0
commit 95a110808c
  1. 1
      Block.h
  2. 13
      BlockClump.cpp
  3. 19
      BlockClump.h
  4. 12
      Board.cpp
  5. 6
      Board.h
  6. BIN
      C++ProjectTemplate
  7. 4
      gameDefines.h
  8. 58
      main.cpp

@ -4,7 +4,6 @@
class Block{ class Block{
public: public:
float vspeed;
vf2d pos; vf2d pos;
Block(vf2d pos) Block(vf2d pos)
:pos(pos){} :pos(pos){}

@ -0,0 +1,13 @@
#include "gameDefines.h"
std::vector<Block>&BlockClump::getBlocks(){
return blocks;
}
void BlockClump::addBlock(int col){
blocks.push_back(Block({(float)col*12,0}));
}
vf2d BlockClump::getBlockPosition(Block&b){
return {b.pos.x,b.pos.y+y};
}

@ -0,0 +1,19 @@
#ifndef BLOCKCLUMP_H
#define BLOCKCLUMP_H
#include "pixelGameEngine.h"
class Block;
class BlockClump{
std::vector<Block>blocks;
public:
float vspeed=0;
float landTime=0.f;
float y=0;
BlockClump(){}
std::vector<Block>&getBlocks();
void addBlock(int col);
//Get a block's position relative to this block clump.
vf2d getBlockPosition(Block&b);
};
#endif

@ -1,5 +1,4 @@
#include "Board.h" #include "gameDefines.h"
#include "Meteos.h"
extern Meteos*game; extern Meteos*game;
@ -13,10 +12,15 @@ Board::Board(vi2d boardSize,float gravity,float spawnRate)
} }
void Board::spawnBlock(int col){ void Board::spawnBlock(int col){
Block newBlock=Block({(float)col*12,-120}); BlockClump c=BlockClump();
cols[col].push_back(newBlock); c.addBlock(col);
clumps.push_back(c);
} }
std::vector<Block>&Board::getBlocks(int col){ std::vector<Block>&Board::getBlocks(int col){
return cols[col]; return cols[col];
}
std::vector<BlockClump>&Board::getBlockClumps(){
return clumps;
} }

@ -1,10 +1,13 @@
#ifndef BOARD_H #ifndef BOARD_H
#define BOARD_H #define BOARD_H
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "Block.h"
class Block;
class BlockClump;
class Board{ class Board{
std::vector<std::vector<Block>> cols; std::vector<std::vector<Block>> cols;
std::vector<BlockClump> clumps;
public: public:
float yBottom; float yBottom;
vf2d drawOffset; vf2d drawOffset;
@ -15,5 +18,6 @@ class Board{
Board(vi2d boardSize,float gravity,float spawnRate); Board(vi2d boardSize,float gravity,float spawnRate);
void spawnBlock(int col); void spawnBlock(int col);
std::vector<Block>&getBlocks(int col); std::vector<Block>&getBlocks(int col);
std::vector<BlockClump>&getBlockClumps();
}; };
#endif #endif

Binary file not shown.

@ -0,0 +1,4 @@
#include "pixelGameEngine.h"
#include "Meteos.h"
#include "BlockClump.h"
#include "Block.h"

@ -1,7 +1,7 @@
#define OLC_PGE_APPLICATION #define OLC_PGE_APPLICATION
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "Meteos.h"
#include <random> #include <random>
#include "gameDefines.h"
Meteos*game; Meteos*game;
@ -25,37 +25,47 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
gameBoard.spawnBlock(randBlockPos(gen)); gameBoard.spawnBlock(randBlockPos(gen));
} }
Clear(Pixel(32,32,255)); Clear(Pixel(32,32,255));
for (int x=-1;x<=gameBoard.boardSize.x;x++) { for (int x=-1;x<=gameBoard.boardSize.x;x++){
for (int y=0;y<=gameBoard.boardSize.y;y++) { for (int y=0;y<=gameBoard.boardSize.y;y++){
if (x==-1||x==10||y==14) { 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)); FillRectDecal({(float)(gameBoard.drawOffset.x+x*12),(float)(gameBoard.drawOffset.y+y*12)},{12,12},Pixel(0,0,0,255));
} else { } else {
DrawRectDecal({(float)(gameBoard.drawOffset.x+x*12),(float)(gameBoard.drawOffset.y+y*12)},{12,12},Pixel(255,255,255,64)); 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.boardSize.x;i++) { for (int i=0;i<gameBoard.getBlockClumps().size();i++){
for (int y=0;y<gameBoard.getBlocks(i).size();y++) { BlockClump&c=gameBoard.getBlockClumps()[i];
Block&b=gameBoard.getBlocks(i)[y]; c.vspeed+=gameBoard.gravity*fElapsedTime;
if (b.pos.y<gameBoard.yBottom) { for (int j=0;j<c.getBlocks().size();j++){
b.vspeed+=gameBoard.gravity*fElapsedTime; Block&b=c.getBlocks()[j];
bool collisionDetected=false; int col=b.pos.x/12;
for (int yy=y-1;yy>=0;yy--) { for (int k=0;k<gameBoard.getBlocks(j).size();k++){
Block&b2=gameBoard.getBlocks(i)[yy]; Block&b2=gameBoard.getBlocks(j)[k];
if (b.pos.y+12>b2.pos.y) { if (c.getBlockPosition(b).y+12>=b2.pos.y&&c.getBlockPosition(b).y<=b2.pos.y+12){
collisionDetected=true; c.y=b2.pos.y-12;
b.vspeed=0; c.vspeed=0;
b.pos.y=b2.pos.y-12; gameBoard.removeClump(c);
break; i--;
}
}
if (!collisionDetected) {
b.pos.y+=b.vspeed*fElapsedTime;
} }
} else {
b.vspeed=0;
b.pos.y=gameBoard.yBottom;
} }
if (c.getBlockPosition(b).y>=gameBoard.yBottom) {
c.y=gameBoard.yBottom;
c.vspeed=0;
gameBoard.removeClump(c);
i--;
}
}
applyVelocity:
for (int j=0;j<c.getBlocks().size();j++){
Block&b=c.getBlocks()[j];
c.y+=c.vspeed*fElapsedTime;
FillRectDecal(c.getBlockPosition(b)+gameBoard.drawOffset,{12,12},DARK_GREEN);
}
}
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];
FillRectDecal(b.pos+gameBoard.drawOffset,{12,12},GREEN); FillRectDecal(b.pos+gameBoard.drawOffset,{12,12},GREEN);
} }
} }

Loading…
Cancel
Save