platform-test/source/entity/player.cpp

280 lines
5.7 KiB
C++
Raw Normal View History

//Player class body
2017-04-29 18:43:17 +00:00
#include"player.h"
2017-06-21 20:03:11 +00:00
Player::Player(int x,int y, int lvlH, SDL_Renderer* render){
2017-06-17 17:12:59 +00:00
szW = 40;
szH = 40;
2017-06-21 20:03:11 +00:00
2017-06-17 17:12:59 +00:00
rect.w = szW;
rect.h = szH;
2017-06-21 20:03:11 +00:00
2017-06-17 17:12:59 +00:00
power = 0;
ground = false;
levelHeight = lvlH;
posX = x;
posY = y;
2017-06-21 20:03:11 +00:00
initPosX = x;
initPosY = y;
renderer = render;
2017-06-17 17:12:59 +00:00
ply.setRenderer(renderer);
loadMedia();
};
int Player::print(int cameraX){
2017-05-21 07:22:25 +00:00
oldPosX = posX;
oldPosY = posY;
2017-05-21 07:22:25 +00:00
rect.x = posX;
rect.y = posY;
2017-04-29 18:43:17 +00:00
SDL_Rect cameraFix = rect;
cameraFix.x -= cameraX;
2017-05-21 07:22:25 +00:00
SDL_SetRenderDrawColor(renderer,0xFF,0,0,0xFF);
2017-05-06 16:01:20 +00:00
if(power == 0)ply.render(&cameraFix,&plyFrame[0]);
else{
ply.render(&cameraFix,&plyRun);
power--;
}
2017-05-21 07:22:25 +00:00
if(ifRunning or power > 0)ply.render(&cameraFix,&plyFrame[1]);
2017-04-29 18:43:17 +00:00
move();
ground = false;
topCollision = false;
//Check if the player has fell out of the world (not horizontally, sadly).
2017-06-21 20:03:11 +00:00
if(posY >= levelHeight) die();
return 0;
2017-04-29 18:43:17 +00:00
};
2017-06-21 20:03:11 +00:00
void Player::die(){
posX = initPosX;
posY = initPosY;
velocityX = 0;
velocityY = 0;
power = 0;
}
2017-05-06 07:52:25 +00:00
void Player::loadMedia(){
ply.loadTexture("textures/player.png");
plyFrame[0].w = szW;
plyFrame[0].h = szH;
plyFrame[0].x = 0;
plyFrame[0].y = 0;
2017-05-01 17:40:03 +00:00
2017-05-06 07:52:25 +00:00
plyFrame[1].w = szW;
plyFrame[1].h = szH;
2017-05-06 16:01:20 +00:00
plyFrame[1].x = szW;
plyFrame[1].y = 0;
2017-05-06 07:52:25 +00:00
2017-05-06 16:01:20 +00:00
plyRun.w = szW;
plyRun.h = szH;
plyRun.x = szW*2;
plyRun.y = 0;
2017-05-06 07:52:25 +00:00
};
2017-04-29 18:43:17 +00:00
int Player::intVelX(){
return static_cast<int>(velocityX);
};
void Player::move(){
2017-05-21 07:22:25 +00:00
//float dt = dTime.getDt();
float dt = 0.016;
//Set keyboard variable
2017-04-29 18:43:17 +00:00
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
2017-04-29 18:43:17 +00:00
//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
2017-05-01 17:40:03 +00:00
ifRunning = false;
2017-04-29 18:43:17 +00:00
//static bool isRunning = false;
int gravity = 800; //Gravity force
int jump = 500; //Jump force
2017-05-06 16:01:20 +00:00
if(power > 0){
jump = 700;
speed = 250;
}
2017-04-29 18:43:17 +00:00
2017-04-30 10:26:31 +00:00
//Check keyboard current state
2017-04-29 18:43:17 +00:00
if(currentKeyStates[SDL_SCANCODE_LEFT]) direction += -1;
if(currentKeyStates[SDL_SCANCODE_RIGHT]) direction += 1;
2017-05-01 17:40:03 +00:00
if(!currentKeyStates[SDL_SCANCODE_LSHIFT] and !isRunning){
run = 1;
}
2017-04-29 18:43:17 +00:00
if(run!=1) dur /= 1;
2017-04-30 10:26:31 +00:00
//Set velocity
2017-04-29 18:43:17 +00:00
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){
2017-04-29 18:43:17 +00:00
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;
}
2017-04-30 10:26:31 +00:00
//Jump and gravity logic
2017-04-29 18:43:17 +00:00
if(ground){
isRunning = false;
if(currentKeyStates[SDL_SCANCODE_SPACE] and !topCollision){
velocityY = jump;
if(currentKeyStates[SDL_SCANCODE_LSHIFT]){
2017-04-29 18:43:17 +00:00
isRunning = true;
}
}
}
else{
if(isRunning == false) run = 1;
}
2017-05-01 17:40:03 +00:00
if(run!=1) ifRunning = true;
2017-04-30 10:26:31 +00:00
//Get the position and update the velY with gravity
2017-04-29 18:43:17 +00:00
velocityY -= gravity * dt;
float x = velocityX * dt * run;
float y = velocityY * dt;
2017-04-29 18:43:17 +00:00
//Convert and set new int position
posX += static_cast<int>(x);
posY -= static_cast<int>(y+0.5);
2017-04-29 18:43:17 +00:00
};
int Player::check(SDL_Rect rectA, int type){
2017-04-29 18:43:17 +00:00
//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;
2017-04-29 18:43:17 +00:00
//Move the player out of the rectangle
if(type == 1){
2017-04-29 18:43:17 +00:00
while(((movX >= bX and movX < b2X)
2017-05-02 18:56:32 +00:00
or (movX + szW > bX and movX + szW <= b2X)
or (movX < bX and movX + szW > b2X))
2017-05-21 07:22:25 +00:00
and ((movY > bY and movY < b2Y)
or (movY + szH > bY and movY + szH < b2Y)
2017-05-02 18:56:32 +00:00
or (movY < bY and movY + szH > b2Y))){
2017-05-21 07:22:25 +00:00
movX -= (posX - oldPosX)/2;
movY -= (posY - oldPosY)/2;
2017-04-29 18:43:17 +00:00
collision = 1;
}
2017-04-29 18:43:17 +00:00
//Correct possible position issues
if(collision == 1){
//Vertical adjustement
if((movX > bX and movX < b2X)
2017-05-02 18:56:32 +00:00
or (movX + szW > bX and movX + szW < b2X)
or (movX < bX and movX + szW > b2X)){
2017-04-29 18:43:17 +00:00
while(movY + szH < bY){
movY++;
velocityY = 0;
}
while(movY - 1 > b2Y){
movY--;
}
}
//Horizontal adjustement
if((movY > bY and movY < b2Y)
2017-05-02 18:56:32 +00:00
or (movY + szH > bY and movY + szH < b2Y)
or (movY < bY and movY + szH > b2Y)){
2017-04-29 18:43:17 +00:00
while(movX + szW < bX){
movX++;
2017-05-21 07:22:25 +00:00
velocityX = 0;
2017-04-29 18:43:17 +00:00
}
2017-05-21 07:22:25 +00:00
while(movX > b2X){
2017-04-29 18:43:17 +00:00
movX--;
2017-05-21 07:22:25 +00:00
velocityX = 0;
2017-04-29 18:43:17 +00:00
}
}
}
2017-05-21 07:22:25 +00:00
//Set and int-ize the position*/
2017-04-29 18:43:17 +00:00
posX = static_cast<int>(movX);
posY = static_cast<int>(movY);
2017-04-29 18:43:17 +00:00
//Check collsion type and reset velocities
//Vertical collisions
if((posX >= bX and posX < b2X)
2017-05-02 18:56:32 +00:00
or (posX + szW > bX and posX + szW <= b2X)
or (posX < bX and posX + szW > b2X)){
2017-04-29 18:43:17 +00:00
//Top collision
if(posY + szH == bY){
ground = true;
collision = 2;
velocityY = 0;
2017-05-21 07:22:25 +00:00
//posY -= 40;
2017-04-29 18:43:17 +00:00
}
//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)
2017-05-02 18:56:32 +00:00
or (posY + szH > bY and posY + szH <= b2Y)
or (posY < bY and posY + szH > b2Y)){
//Left collision
2017-04-29 18:43:17 +00:00
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);
}
}
}
2017-05-06 16:01:20 +00:00
else if(type == 2 and power == 0){
if(((movX >= bX and movX < b2X)
or (movX + szW > bX and movX + szW <= b2X)
or (movX < bX and movX + szW > b2X))
and ((movY >= bY and movY < b2Y)
or (movY + szH > bY and movY + szH <= b2Y)
or (movY < bY and movY + szH > b2Y))){
//power = 10 / dTime.getDt();
power = 360;
}
}
return power;
2017-04-29 18:43:17 +00:00
};