//Player class cpp #include"player.h" void Player::print(int cameraX){ SDL_Rect cameraFix = rect; cameraFix.x -= cameraX; if(power > 0){ std::cout << power << "ye" << std::endl; power--; } SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF); ply.render(&cameraFix,currentFrame,renderer); if(ifRunning)ply.render(&cameraFix,&plyFrame[5],renderer); currentFrame = &plyFrame[0]; oldPosX = posX; oldPosY = posY; move(); ground = false; topCollision = false; }; Player::Player(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; loadMedia(); currentFrame = &plyFrame[0]; }; bool Player::loadMedia(){ bool success = true; if(!ply.loadTexture("textures/player.png",renderer)){ std::cout << "Failed to load ply texture" << std::endl; success = false; } else{ int frame = 0; for(int row = 0;row < 2;row++){ for(int column = 0;column < 3;column++){ plyFrame[frame].w=szW; plyFrame[frame].h=szH; plyFrame[frame].x=szW*column; plyFrame[frame].y=szH*row; frame++; } } } return success; } int Player::intVelX(){ return static_cast(velocityX); }; void Player::move(){ float dt = dTime.getDt(); //Set keyboard variable 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 ifRunning = false; //static bool isRunning = false; int gravity = 800; //Gravity force int jump = 500; //Jump force //Check keyboard current state 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; //Set velocity 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 logic if(ground){ isRunning = false; if(currentKeyStates[SDL_SCANCODE_SPACE] and !topCollision){ velocityY = jump; if(currentKeyStates[SDL_SCANCODE_LSHIFT]){ isRunning = true; } } } else{ if(isRunning == false) run = 1; } if(run!=1) ifRunning = true; //Get the position and update the velY with gravity velocityY -= gravity * dt; float x = velocityX * dt * run; float y = velocityY * dt; //Convert and set new int position posX += static_cast(x); posY -= static_cast(y+0.5); rect = {posX,posY,szW,szH}; }; int Player::check(SDL_Rect rectA, int type){ //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 if(type == 1){ while(((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))){ 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) or (movX < 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) or (movY < bY and movY + szH > b2Y)){ while(movX + szW < bX){ movX++; } while(posX > b2X){ movX--; } } } //Set and int-ize the position posX = static_cast(movX); posY = static_cast(movY); //Check collsion type and reset velocities //Vertical collisions if((posX >= bX and posX < b2X) or (posX + szW > bX and posX + szW <= b2X) or (posX < bX and posX + szW > b2X)){ //Top collision if(posY + szH == bY){ ground = true; collision = 2; velocityY = 0; currentFrame = &plyFrame[1]; } //Bottom collision else if(posY == b2Y){ collision = 3; topCollision = true; currentFrame = &plyFrame[3]; if(velocityY > 0) velocityY -= static_cast(velocityY); } } //Horizontal collisions if((posY >= bY and posY < b2Y) or (posY + szH > bY and posY + szH <= b2Y) or (posY < bY and posY + szH > b2Y)){ //Left collision if(posX + szW == bX){ collision = 4; currentFrame = &plyFrame[2]; if(velocityX > 0) velocityX -= static_cast(velocityX); } //Right collision else if(posX == b2X){ collision = 5; currentFrame = &plyFrame[4]; if(velocityX < 0) velocityX -= static_cast(velocityX); } } } else if(type == 2){ 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; };