Cleaning up a bit things
This commit is contained in:
parent
55a0f0e064
commit
4f9d042877
|
@ -0,0 +1,2 @@
|
||||||
|
*.swp
|
||||||
|
*.o
|
2
Makefile
2
Makefile
|
@ -8,7 +8,7 @@ COMPILER_FLAGS = -w
|
||||||
|
|
||||||
LINKER_FLAGS = -lSDL2 -std=gnu++11
|
LINKER_FLAGS = -lSDL2 -std=gnu++11
|
||||||
|
|
||||||
OBJ_NAME = main.exe
|
OBJ_NAME = bin/main
|
||||||
|
|
||||||
|
|
||||||
all: $(OBJS)
|
all: $(OBJS)
|
||||||
|
|
|
@ -6,8 +6,6 @@ void Block::print(int x, int y, int w, int h,SDL_Renderer* renderer){
|
||||||
szW = w;
|
szW = w;
|
||||||
szH = h;
|
szH = h;
|
||||||
|
|
||||||
//std::cout<<x<<" "<<y<<" "<<w<<" "<<h<<" / ";
|
|
||||||
|
|
||||||
rect = {posX, posY, szW, szH};
|
rect = {posX, posY, szW, szH};
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(renderer,0,0xFF,0,0xFF);
|
SDL_SetRenderDrawColor(renderer,0,0xFF,0,0xFF);
|
||||||
|
|
31
main.cpp
31
main.cpp
|
@ -18,7 +18,6 @@ void init();
|
||||||
bool loadMedia();
|
bool loadMedia();
|
||||||
void close();
|
void close();
|
||||||
void gameLoop();
|
void gameLoop();
|
||||||
int check(SDL_Rect rectA, SDL_Rect rectB);
|
|
||||||
|
|
||||||
Player posweg;
|
Player posweg;
|
||||||
Block ground;
|
Block ground;
|
||||||
|
@ -90,33 +89,3 @@ int main(int argc, char* args[]){
|
||||||
close();
|
close();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int check(SDL_Rect rectA, SDL_Rect rectB){
|
|
||||||
|
|
||||||
int collision = 0;
|
|
||||||
|
|
||||||
int aX = rectA.x;
|
|
||||||
int aY = rectA.y;
|
|
||||||
int a2X = rectA.x + rectA.w;
|
|
||||||
int a2Y = rectA.y + rectA.h;
|
|
||||||
|
|
||||||
int bX = rectB.x;
|
|
||||||
int bY = rectB.y;
|
|
||||||
int b2X = rectB.x + rectB.w;
|
|
||||||
int b2Y = rectB.y + rectB.h;
|
|
||||||
|
|
||||||
if(aX > bX and aX < b2X){
|
|
||||||
if(aY > bY and aY < b2Y) collision += 1;
|
|
||||||
if(a2Y > bY and a2Y < b2Y) collision += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(a2X > bX and a2X < b2X){
|
|
||||||
if(aY > bY and aY < b2Y) collision += 4;
|
|
||||||
if(a2Y > bY and a2Y < b2Y) collision += 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
//std::cout<<aX<<" "<<a2X<<" "<<aY<<" "<<a2Y<<" / ";
|
|
||||||
//std::cout<<bX<<" "<<b2X<<" "<<bY<<" "<<b2Y;
|
|
||||||
|
|
||||||
return collision;
|
|
||||||
};
|
|
||||||
|
|
|
@ -30,9 +30,9 @@ int Player::intVelX(){
|
||||||
};
|
};
|
||||||
|
|
||||||
void Player::move(){
|
void Player::move(){
|
||||||
//float dt = dTime.getDt();
|
float dt = dTime.getDt();
|
||||||
float dt = 0.016;
|
|
||||||
|
|
||||||
|
//Set keyboard variable
|
||||||
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
|
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
//Intializing variables
|
//Intializing variables
|
||||||
|
@ -45,11 +45,13 @@ void Player::move(){
|
||||||
int gravity = 800; //Gravity force
|
int gravity = 800; //Gravity force
|
||||||
int jump = 500; //Jump force
|
int jump = 500; //Jump force
|
||||||
|
|
||||||
|
//Check keyboard current state
|
||||||
if(currentKeyStates[SDL_SCANCODE_LEFT]) direction += -1;
|
if(currentKeyStates[SDL_SCANCODE_LEFT]) direction += -1;
|
||||||
if(currentKeyStates[SDL_SCANCODE_RIGHT]) direction += 1;
|
if(currentKeyStates[SDL_SCANCODE_RIGHT]) direction += 1;
|
||||||
if(!currentKeyStates[SDL_SCANCODE_LSHIFT] and !isRunning) run = 1;
|
if(!currentKeyStates[SDL_SCANCODE_LSHIFT] and !isRunning) run = 1;
|
||||||
if(run!=1) dur /= 1;
|
if(run!=1) dur /= 1;
|
||||||
|
|
||||||
|
//Set velocity
|
||||||
if(direction == 1 and intVelX() < speed){
|
if(direction == 1 and intVelX() < speed){
|
||||||
velocityX += speed * dt * dur;
|
velocityX += speed * dt * dur;
|
||||||
if(intVelX() > speed) velocityX = speed;
|
if(intVelX() > speed) velocityX = speed;
|
||||||
|
@ -69,7 +71,7 @@ void Player::move(){
|
||||||
}
|
}
|
||||||
else if(velocityX <= 1 or velocityX >= -1) velocityX = 0;
|
else if(velocityX <= 1 or velocityX >= -1) velocityX = 0;
|
||||||
|
|
||||||
//Jump and gravity key logic
|
//Jump and gravity logic
|
||||||
if(ground){
|
if(ground){
|
||||||
isRunning = false;
|
isRunning = false;
|
||||||
if(currentKeyStates[SDL_SCANCODE_SPACE] and !topCollision){
|
if(currentKeyStates[SDL_SCANCODE_SPACE] and !topCollision){
|
||||||
|
@ -83,6 +85,7 @@ void Player::move(){
|
||||||
if(isRunning == false) run = 1;
|
if(isRunning == false) run = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Get the position and update the velY with gravity
|
||||||
velocityY -= gravity * dt;
|
velocityY -= gravity * dt;
|
||||||
float x = velocityX * dt * run;
|
float x = velocityX * dt * run;
|
||||||
float y = velocityY * dt;
|
float y = velocityY * dt;
|
||||||
|
|
4
player.h
4
player.h
|
@ -13,7 +13,6 @@ class Entity;
|
||||||
|
|
||||||
class Player: public Entity{
|
class Player: public Entity{
|
||||||
public:
|
public:
|
||||||
//Player(/*int* collision*/);
|
|
||||||
void print();
|
void print();
|
||||||
int check(SDL_Rect rectB);
|
int check(SDL_Rect rectB);
|
||||||
void init(int x, int y,int w, int h,SDL_Renderer** render);
|
void init(int x, int y,int w, int h,SDL_Renderer** render);
|
||||||
|
@ -27,13 +26,10 @@ class Player: public Entity{
|
||||||
bool ground;
|
bool ground;
|
||||||
bool topCollision;
|
bool topCollision;
|
||||||
//int posX, posY;
|
//int posX, posY;
|
||||||
//int cVelocity, oldVelX, oldVelY, newVelX, newVelY;
|
|
||||||
float velocityX = 0;
|
float velocityX = 0;
|
||||||
float velocityY = 0;
|
float velocityY = 0;
|
||||||
bool isRunning = false;
|
bool isRunning = false;
|
||||||
int oldPosX, oldPosY;
|
int oldPosX, oldPosY;
|
||||||
//int szW = 40;
|
|
||||||
//int szH = 40;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue