Fall and landing physics

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent cf17d444b0
commit fe96cb8df0
  1. 16
      Board.cpp
  2. 7
      Board.h
  3. BIN
      C++ProjectTemplate
  4. 2
      Meteos.h
  5. 39
      main.cpp

@ -3,8 +3,20 @@
extern Meteos*game; extern Meteos*game;
Board::Board(vi2d boardSize,float gravity) Board::Board(vi2d boardSize,float gravity,float spawnRate)
:boardSize(boardSize),gravity(gravity){ :boardSize(boardSize),gravity(gravity),spawnRate(spawnRate){
drawOffset={(float)game->ScreenWidth()/2-boardSize.x/2*12,(float)game->ScreenHeight()/2-boardSize.y/2*12}; drawOffset={(float)game->ScreenWidth()/2-boardSize.x/2*12,(float)game->ScreenHeight()/2-boardSize.y/2*12};
yBottom=(boardSize.y-1)*12; yBottom=(boardSize.y-1)*12;
for (int i=0;i<boardSize.x;i++) {
cols.push_back(std::vector<Block>());
}
}
void Board::spawnBlock(int col){
Block newBlock=Block({(float)col*12,-120});
cols[col].push_back(newBlock);
}
std::vector<Block>&Board::getBlocks(int col){
return cols[col];
} }

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

Binary file not shown.

@ -1,7 +1,6 @@
#ifndef METEOS_H #ifndef METEOS_H
#define METEOS_H #define METEOS_H
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "Block.h"
#include "Board.h" #include "Board.h"
#include <random> #include <random>
@ -10,7 +9,6 @@ class Meteos : public olc::PixelGameEngine{
Meteos(){ Meteos(){
sAppName="Meteos"; sAppName="Meteos";
} }
std::vector<Block> blocks;
float lastBlockSpawn=0.0f; float lastBlockSpawn=0.0f;
std::uniform_int_distribution<> randBlockPos; std::uniform_int_distribution<> randBlockPos;
std::mt19937 gen; std::mt19937 gen;

@ -13,16 +13,16 @@ bool Meteos::OnUserCreate()
gen=std::mt19937(rd()); //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);
gameBoard=Board({10,14},6.f); gameBoard=Board({10,14},12.f,3.0f);
return true; return true;
} }
bool Meteos::OnUserUpdate(float fElapsedTime) bool Meteos::OnUserUpdate(float fElapsedTime)
{ {
lastBlockSpawn+=fElapsedTime; lastBlockSpawn+=fElapsedTime;
if (lastBlockSpawn>=5.0f){ if (lastBlockSpawn>=3.0f){
lastBlockSpawn-=5.0f; lastBlockSpawn-=3.0f;
blocks.push_back(Block({(float)randBlockPos(gen)*12,0})); 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++) {
@ -34,15 +34,30 @@ bool Meteos::OnUserUpdate(float fElapsedTime)
} }
} }
} }
for (Block&b:blocks) { for (int i=0;i<gameBoard.boardSize.x;i++) {
if (b.pos.y<gameBoard.yBottom) { for (int y=0;y<gameBoard.getBlocks(i).size();y++) {
b.vspeed+=gameBoard.gravity*fElapsedTime; Block&b=gameBoard.getBlocks(i)[y];
b.pos.y+=b.vspeed*fElapsedTime; if (b.pos.y<gameBoard.yBottom) {
} else { b.vspeed+=gameBoard.gravity*fElapsedTime;
b.vspeed=0; bool collisionDetected=false;
b.pos.y=gameBoard.yBottom; 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;
}
} else {
b.vspeed=0;
b.pos.y=gameBoard.yBottom;
}
FillRectDecal(b.pos+gameBoard.drawOffset,{12,12},GREEN);
} }
FillRectDecal(b.pos+gameBoard.drawOffset,{12,12},GREEN);
} }
return true; return true;
} }

Loading…
Cancel
Save