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

@ -1,10 +1,13 @@
#ifndef BOARD_H
#define BOARD_H
#include "pixelGameEngine.h"
#include "Block.h"
class Block;
class BlockClump;
class Board{
std::vector<std::vector<Block>> cols;
std::vector<BlockClump> clumps;
public:
float yBottom;
vf2d drawOffset;
@ -15,5 +18,6 @@ class Board{
Board(vi2d boardSize,float gravity,float spawnRate);
void spawnBlock(int col);
std::vector<Block>&getBlocks(int col);
std::vector<BlockClump>&getBlockClumps();
};
#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
#include "pixelGameEngine.h"
#include "Meteos.h"
#include <random>
#include "gameDefines.h"
Meteos*game;
@ -25,37 +25,47 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
gameBoard.spawnBlock(randBlockPos(gen));
}
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) {
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.boardSize.x;i++) {
for (int y=0;y<gameBoard.getBlocks(i).size();y++) {
Block&b=gameBoard.getBlocks(i)[y];
if (b.pos.y<gameBoard.yBottom) {
b.vspeed+=gameBoard.gravity*fElapsedTime;
bool collisionDetected=false;
for (int yy=y-1;yy>=0;yy--) {
Block&b2=gameBoard.getBlocks(i)[yy];
if (b.pos.y+12>b2.pos.y) {
collisionDetected=true;
b.vspeed=0;
b.pos.y=b2.pos.y-12;
break;
}
}
if (!collisionDetected) {
b.pos.y+=b.vspeed*fElapsedTime;
for (int i=0;i<gameBoard.getBlockClumps().size();i++){
BlockClump&c=gameBoard.getBlockClumps()[i];
c.vspeed+=gameBoard.gravity*fElapsedTime;
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(j).size();k++){
Block&b2=gameBoard.getBlocks(j)[k];
if (c.getBlockPosition(b).y+12>=b2.pos.y&&c.getBlockPosition(b).y<=b2.pos.y+12){
c.y=b2.pos.y-12;
c.vspeed=0;
gameBoard.removeClump(c);
i--;
}
} 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);
}
}

Loading…
Cancel
Save