Gravity + jumping
This commit is contained in:
parent
18f1213985
commit
9195be85d0
105
main.cpp
105
main.cpp
@ -4,19 +4,85 @@
|
||||
#include <SDL2/SDL2_framerate.h>
|
||||
|
||||
#define PLAYER_ID 0x00000001
|
||||
#define FINALLY_ID 0x00000002
|
||||
#define STONE_ID 0x00000002
|
||||
|
||||
std::shared_ptr<SDLPP::RenderObject> player;
|
||||
class Player : public SDLPP::RectangleRender {
|
||||
public:
|
||||
Player(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r, double _max_gravity = 1.0, uint32_t _max_gravity_time = 1000) : SDLPP::RectangleRender(x,y,w,h,r) {
|
||||
max_gravity = _max_gravity;
|
||||
jump_speed = 1.5*max_gravity;
|
||||
max_gravity_time = _max_gravity_time;
|
||||
cur_gravity_time = max_gravity_time;
|
||||
}
|
||||
void setGravity(bool grav) {
|
||||
gravity_enabled = grav;
|
||||
}
|
||||
void enableGravity() {
|
||||
gravity_enabled = true;
|
||||
}
|
||||
void disableGravity() {
|
||||
gravity_enabled = false;
|
||||
}
|
||||
bool isGravityEnabled() {
|
||||
return gravity_enabled;
|
||||
}
|
||||
bool isJumping() {
|
||||
return jumping;
|
||||
}
|
||||
void setLastStand() {
|
||||
cur_gravity_time = max_gravity_time;
|
||||
jumping = false;
|
||||
}
|
||||
void jump() {
|
||||
jumping = true;
|
||||
}
|
||||
virtual void move(int ticks) {
|
||||
auto dimensions = renderer->getDimensions();
|
||||
auto time_portion = (static_cast<double>(ticks)/1000);
|
||||
auto addx = static_cast<double>(movementSpeed * movementDirection.first)*time_portion;
|
||||
auto addy = static_cast<double>(movementSpeed * movementDirection.second)*time_portion;
|
||||
x_ += addx;
|
||||
y_ += addy;
|
||||
cur_gravity_time -= ticks;
|
||||
auto grav = gravity_enabled * max_gravity * (max_gravity_time - cur_gravity_time)/max_gravity_time;
|
||||
if( grav > max_gravity)
|
||||
grav = max_gravity;
|
||||
auto jump_ = jumping * jump_speed * cur_gravity_time/max_gravity_time;
|
||||
if(jump_ < 0 || jump_ > jump_speed)
|
||||
jump_ = 0;
|
||||
y_ += grav * time_portion;
|
||||
y_ -= jump_ * time_portion;
|
||||
rect.x = x_ * dimensions.first;
|
||||
rect.y = y_ * dimensions.second;
|
||||
for( auto &x : collisions ) {
|
||||
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
||||
}
|
||||
}
|
||||
private:
|
||||
double max_gravity = 1.0;
|
||||
double jump_speed = 2.0;
|
||||
uint32_t max_gravity_time = 1000;
|
||||
uint32_t cur_gravity_time = 1000;
|
||||
bool gravity_enabled = false;
|
||||
bool jumping = false;
|
||||
};
|
||||
|
||||
std::shared_ptr<Player> player;
|
||||
std::vector<std::shared_ptr<SDLPP::Texture>> bgtextures;
|
||||
bool quit = false;
|
||||
|
||||
void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
|
||||
auto y = std::make_shared<SDLPP::RectangleRender>(0.4,0.2,0.25,0.4, r);
|
||||
y->addCollision(SDLPP::Rect(0,0,1,1));
|
||||
y->setTexture("6.png");
|
||||
y->setId(FINALLY_ID);
|
||||
scene.addObject(y);
|
||||
auto x = std::make_shared<SDLPP::RectangleRender>(0,0,0.2,0.2, r);
|
||||
std::shared_ptr<SDLPP::RectangleRender> stone;
|
||||
double posx = 0;
|
||||
while(posx < 1) {
|
||||
stone = std::make_shared<SDLPP::RectangleRender>(posx,0.5,0.15,0.1,r);
|
||||
stone->addCollision(SDLPP::Rect(0,0,1,1));
|
||||
stone->setTexture("stone.png");
|
||||
stone->setId(STONE_ID);
|
||||
scene.addObject(stone);
|
||||
posx += 0.15;
|
||||
}
|
||||
auto x = std::make_shared<Player>(0,0,0.2,0.2, r);
|
||||
x->addCollision(SDLPP::Rect(0,0,1,1));
|
||||
x->setTexture("5.png");
|
||||
x->setId(PLAYER_ID);
|
||||
@ -53,10 +119,12 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
|
||||
player->addMovement(1,0);
|
||||
break;
|
||||
case SDLK_w:
|
||||
player->addMovement(0,-1);
|
||||
if(!player->isJumping()) {
|
||||
player->setLastStand();
|
||||
player->jump();
|
||||
}
|
||||
break;
|
||||
case SDLK_s:
|
||||
player->addMovement(0,1);
|
||||
break;
|
||||
default:
|
||||
scene.setBackground(bgtextures[4]);
|
||||
@ -73,10 +141,8 @@ void handleKeyUp(SDL_Keycode key) {
|
||||
player->addMovement(-1,0);
|
||||
break;
|
||||
case SDLK_w:
|
||||
player->addMovement(0,1);
|
||||
break;
|
||||
case SDLK_s:
|
||||
player->addMovement(0,-1);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -115,11 +181,21 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
|
||||
SDL_framerateDelay(&gFPS);
|
||||
pollEvents(*scene);
|
||||
scene->movement();
|
||||
bool gravity = true;
|
||||
for( auto &x : scene->getCollisions(*player) ) {
|
||||
if( x->getId() == FINALLY_ID ) {
|
||||
std::cout << "Finally!" << std::endl;
|
||||
if( x->getId() == STONE_ID ) {
|
||||
gravity = false;
|
||||
auto stoneRect = x->getDoubleRect();
|
||||
auto playerPos = player->getDoubleRect();
|
||||
auto newPX = playerPos.first.first;
|
||||
auto newPY = playerPos.first.second;
|
||||
newPY = stoneRect.first.second - playerPos.second.second;
|
||||
player->setPos(newPX, newPY);
|
||||
if(player->isGravityEnabled())
|
||||
player->setLastStand();
|
||||
}
|
||||
}
|
||||
player->setGravity(gravity);
|
||||
frames++;
|
||||
if(SDL_GetTicks() - base >= 1000) {
|
||||
base = SDL_GetTicks();
|
||||
@ -142,6 +218,7 @@ int main() {
|
||||
main_scene->setBackground(bgtextures[0]);
|
||||
addStuff(*main_scene, renderer);
|
||||
player->setMovementSpeed(0.3);
|
||||
player->enableGravity();
|
||||
|
||||
FPSmanager gFPS;
|
||||
SDL_initFramerate(&gFPS);
|
||||
|
14
sdlpp.hpp
14
sdlpp.hpp
@ -170,6 +170,8 @@ public:
|
||||
virtual int collisionPushY() = 0;
|
||||
virtual int collisionWidth() = 0;
|
||||
virtual int collisionHeight() = 0;
|
||||
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() = 0;
|
||||
virtual void setPos(double x, double y) = 0;
|
||||
bool colidesWith(const RenderObject &other) const {
|
||||
if(!hasCollisions() || !other.hasCollisions()) {
|
||||
return false;
|
||||
@ -333,6 +335,16 @@ public:
|
||||
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
||||
}
|
||||
}
|
||||
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() {
|
||||
return {{x_,y_}, {w_,h_}};
|
||||
}
|
||||
virtual void setPos(double x, double y) {
|
||||
auto dimensions = renderer->getDimensions();
|
||||
x_ = x;
|
||||
y_ = y;
|
||||
rect.x = x_ * dimensions.first;
|
||||
rect.y = y_ * dimensions.second;
|
||||
}
|
||||
virtual int leftmost() {
|
||||
return rect.x;
|
||||
}
|
||||
@ -359,7 +371,7 @@ public:
|
||||
rect.w = w_ * smaller_dimension;
|
||||
rect.h = h_ * smaller_dimension;
|
||||
}
|
||||
private:
|
||||
protected:
|
||||
double x_;
|
||||
double y_;
|
||||
double w_;
|
||||
|
Loading…
Reference in New Issue
Block a user