185 lines
4.1 KiB
C++
185 lines
4.1 KiB
C++
//Player class cpp
|
|
|
|
#include"player.h"
|
|
|
|
void Player::print(){
|
|
rect = {posX,posY,szW,szH};
|
|
|
|
SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF);
|
|
SDL_RenderFillRect(*renderer, &rect);
|
|
|
|
oldPosX = posX;
|
|
oldPosY = posY;
|
|
move();
|
|
ground = false;
|
|
topCollision = false;
|
|
};
|
|
|
|
void Player::init(int x,int y, int w, int h, SDL_Renderer** render){
|
|
posX = x;
|
|
posY = y;
|
|
szW = w;
|
|
szH = h;
|
|
renderer = render;
|
|
|
|
oldPosX = posX;
|
|
oldPosY = posY; };
|
|
|
|
int Player::intVelX(){
|
|
return static_cast<int>(velocityX);
|
|
};
|
|
|
|
void Player::move(){
|
|
//float dt = dTime.getDt();
|
|
float dt = 0.016;
|
|
|
|
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
|
|
|
|
//Intializing variables
|
|
int direction = 0; //1 = right, 0 = idle/both, -1 = left
|
|
int dur = 4; //Divide transition time
|
|
int speed = 200; //Horizontal movement speed
|
|
float run = 1.5; //Running speed multiplication
|
|
//static bool isRunning = false;
|
|
|
|
int gravity = 800; //Gravity force
|
|
int jump = 500; //Jump force
|
|
|
|
if(currentKeyStates[SDL_SCANCODE_LEFT]) direction += -1;
|
|
if(currentKeyStates[SDL_SCANCODE_RIGHT]) direction += 1;
|
|
if(!currentKeyStates[SDL_SCANCODE_LSHIFT] and !isRunning) run = 1;
|
|
if(run!=1) dur /= 1;
|
|
|
|
if(direction == 1 and intVelX() < speed){
|
|
velocityX += speed * dt * dur;
|
|
if(intVelX() > speed) velocityX = speed;
|
|
}
|
|
if(direction == -1 and intVelX() > -speed){
|
|
velocityX -= speed * dt * dur;
|
|
if(intVelX() < -speed) velocityX = -speed;
|
|
}
|
|
if(direction == 0 and velocityX != 0)
|
|
if(intVelX() > 0){
|
|
velocityX -= speed * dt * dur;
|
|
if(intVelX() < 0) velocityX = 0;
|
|
}
|
|
else if(intVelX() < 0){
|
|
velocityX += speed * dt * dur;
|
|
if(intVelX() > 0) velocityX = 0;
|
|
}
|
|
else if(velocityX <= 1 or velocityX >= -1) velocityX = 0;
|
|
|
|
//Jump and gravity key logic
|
|
if(ground){
|
|
isRunning = false;
|
|
if(currentKeyStates[SDL_SCANCODE_SPACE] and !topCollision){
|
|
velocityY = jump;
|
|
if(run!=1){
|
|
isRunning = true;
|
|
}
|
|
}
|
|
}
|
|
else{
|
|
if(isRunning == false) run = 1;
|
|
}
|
|
|
|
velocityY -= gravity * dt;
|
|
float x = velocityX * dt * run;
|
|
float y = velocityY * dt;
|
|
|
|
//Convert and set new int position
|
|
posX += static_cast<int>(x);
|
|
posY -= static_cast<int>(y+0.5);
|
|
};
|
|
|
|
int Player::check(SDL_Rect rectA){
|
|
//Initialize and reset collision type
|
|
int collision = 0;
|
|
|
|
//Set B rectangle variables
|
|
int bX = rectA.x;
|
|
int bY = rectA.y;
|
|
int b2X = rectA.x + rectA.w;
|
|
int b2Y = rectA.y + rectA.h;
|
|
|
|
//Float-ize the position
|
|
float movX = posX, movY = posY;
|
|
|
|
//Clacule the opposite od the player direction angle
|
|
float angle = atan2(oldPosY - posY, oldPosX - posX);
|
|
|
|
//Move the player out of the rectangle
|
|
while(((movX >= bX and movX < b2X)
|
|
or (movX + szW > bX and movX + szW <= b2X))
|
|
and ((movY >= bY and movY < b2Y)
|
|
or (movY + szH > bY and movY + szH <= b2Y))){
|
|
movX += cos(angle) / 2;
|
|
movY += sin(angle) / 2;
|
|
collision = 1;
|
|
}
|
|
|
|
//Correct possible position issues
|
|
if(collision == 1){
|
|
//Vertical adjustement
|
|
if((movX > bX and movX < b2X)
|
|
or (movX + szW > bX and movX + szW < b2X)){
|
|
while(movY + szH < bY){
|
|
movY++;
|
|
velocityY = 0;
|
|
}
|
|
while(movY - 1 > b2Y){
|
|
movY--;
|
|
}
|
|
}
|
|
//Horizontal adjustement
|
|
if((movY > bY and movY < b2Y)
|
|
or (movY + szH > bY and movY + szH < b2Y)){
|
|
while(movX + szW < bX){
|
|
movX++;
|
|
}
|
|
while(posX > b2X){
|
|
movX--;
|
|
}
|
|
}
|
|
}
|
|
|
|
//Set and int-ize the position
|
|
posX = static_cast<int>(movX);
|
|
posY = static_cast<int>(movY);
|
|
|
|
//Check collsion type and reset velocities
|
|
//Vertical collisions
|
|
if((posX >= bX and posX < b2X)
|
|
or (posX + szW > bX and posX + szW <= b2X)){
|
|
//Top collision
|
|
if(posY + szH == bY){
|
|
ground = true;
|
|
collision = 2;
|
|
velocityY = 0;
|
|
}
|
|
//Bottom collision
|
|
else if(posY == b2Y){
|
|
collision = 3;
|
|
topCollision = true;
|
|
if(velocityY > 0)
|
|
velocityY -= static_cast<int>(velocityY);
|
|
}
|
|
}
|
|
//Horizontal collisions
|
|
if((posY >= bY and posY < b2Y)
|
|
or (posY + szH > bY and posY + szH <= b2Y)){
|
|
//Left collision
|
|
if(posX + szW == bX){
|
|
collision = 4;
|
|
if(velocityX > 0)
|
|
velocityX -= static_cast<int>(velocityX);
|
|
}
|
|
//Right collision
|
|
else if(posX == b2X){
|
|
collision = 5;
|
|
if(velocityX < 0)
|
|
velocityX -= static_cast<int>(velocityX);
|
|
}
|
|
}
|
|
};
|