Advertisement
Kitomas

player.cpp as of 2024-03-21

Mar 21st, 2024
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1. #include <player.hpp>
  2.  
  3. #include <math.h>
  4.  
  5. using namespace kit;
  6.  
  7.  
  8.  
  9.  
  10.  
  11. #define VELX_MAX (1.5f)
  12. #define VELY_MAX (2.5f)
  13. #define MID_AIR (ticksInAir>1 || jumped)
  14.  
  15.  
  16.  
  17.  
  18.  
  19. #define RND_AMOUNT (0.5f) //0.5f for rounding, 0.0f for truncation
  20. #define RND(_v) ( (s32)((_v)+RND_AMOUNT) )
  21. #define TILE_SIZE (8) //actual sprite is 8x8
  22. #define TILE_ADJUST(_v) ((_v)*(TILE_SIZE+1)) //+1 for one pixel of spacing
  23.  
  24. void Player::blit(kit::f32 scaleX, kit::f32 scaleY){ //scale is 1x by default
  25.   if(!visible || scaleX<=0.0f || scaleY<=0.0f) return;
  26.  
  27.   shape::rect dst;
  28.   dst.w = (s32)( TILE_SIZE*scaleX );
  29.   dst.h = (s32)( TILE_SIZE*scaleY );
  30.   dst.x = RND(pos.x) - (dst.w/2);
  31.   dst.y = RND(pos.y) - (dst.h/2);
  32.  
  33.   shape::rect src(0,0, TILE_SIZE,TILE_SIZE);
  34.   if(confused    ) src.y += TILE_ADJUST(1);
  35.   if(!facingRight) src.y += TILE_ADJUST(2);
  36.  
  37.  
  38.  
  39.   // >1 instead of >0 to account for ghost step
  40.   if(MID_AIR){ //mid-air
  41.     if(fabsf(vel.x) > PLAYER_NEUTRAL){ //going fast horizontally
  42.       if(vel.y<=0) src.x = TILE_ADJUST(2); //-Y; back leg extended
  43.       else         src.x = TILE_ADJUST(8); //+Y; front leg extended
  44.     } else { //going slow (or not at all) horizontally
  45.       if(     vel.y <-PLAYER_NEUTRAL) src.x = TILE_ADJUST(1); //-Y; back leg down
  46.       else if(vel.y > PLAYER_NEUTRAL) src.x = TILE_ADJUST(4); //+Y; front leg down
  47.       else                            src.x = TILE_ADJUST(7); //0Y; both legs up
  48.     }
  49.  
  50.   } else { //on the ground
  51.       if(vel.x < 0  &&  acc.x > 0){ //slowing down while moving left
  52.         if(fabsf(vel.x) > PLAYER_NEUTRAL){ //going fast horizontally
  53.           if(facingRight) src.x = TILE_ADJUST(2); //back leg extended
  54.           else            src.x = TILE_ADJUST(8); //front leg extended
  55.         } else { //going slow (or not at all) horizontally
  56.           if(facingRight) src.x = TILE_ADJUST(1); //back leg down
  57.           else            src.x = TILE_ADJUST(4); //front leg down
  58.         }
  59.  
  60.       } else if(vel.x > 0  &&  acc.x < 0){ //slowing down while moving right
  61.         if(fabsf(vel.x) > PLAYER_NEUTRAL){ //going fast horizontally
  62.           if(facingRight) src.x = TILE_ADJUST(8); //front leg extended
  63.           else            src.x = TILE_ADJUST(2); //back leg extended
  64.         } else { //going slow (or not at all) horizontally
  65.           if(facingRight) src.x = TILE_ADJUST(4); //front leg down
  66.           else            src.x = TILE_ADJUST(1); //back leg down
  67.         }
  68.  
  69.       } else if(fabsf(vel.x) > PLAYER_NEUTRAL){ //moving forward
  70.         src.x = TILE_ADJUST(1 + RND(runningState)%6);
  71.  
  72.       } else if(acc.x < 0){ //standing still while accelerating left
  73.         if(facingRight) src.x = TILE_ADJUST(4); //front leg down
  74.         else            src.x = TILE_ADJUST(1); //back leg down
  75.  
  76.       } else if(acc.x > 0){ //standing still while accelerating right
  77.         if(facingRight) src.x = TILE_ADJUST(1); //back leg down
  78.         else            src.x = TILE_ADJUST(4); //front leg down
  79.  
  80.       } else { //standing still without acceleration
  81.         src.x = TILE_ADJUST(0); //technically redundant, but left here for clarity
  82.  
  83.       }
  84.  
  85.   }
  86.  
  87.  
  88.  
  89.   //0xff00ff for magenta as the transparency color
  90.   gl_spritesheetPlayer->blitRect(&dst, &src, 0xff00ff);
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97. //acc, visible, and confused are not modified here
  98. void Player::update(){
  99.   vel.x += (MID_AIR) ? acc.x*PLAYER_AIR_FRICTION : acc.x;
  100.   vel.y += acc.y;
  101.   if(enforceMaxVel){
  102.     vel.x = CLAMP(vel.x, -VELX_MAX, VELX_MAX);
  103.     vel.y = CLAMP(vel.y, -VELY_MAX, VELY_MAX);
  104.   }
  105.   pos += vel;
  106.  
  107.  
  108.   if(fabsf(vel.x)<0.05f && acc.x==0) vel.x = 0.0f;
  109.  
  110.  
  111.   if(ticksInAir <= 1  &&  vel.x != 0){ //if on ground while moving horizontally
  112.     facingRight = vel.x > 0;
  113.     if(ticksInAir <= 1  &&  acc.x == 0){
  114.       vel.x -= (vel.x>0) ?  PLAYER_SPEED*PLAYER_GND_FRICTION
  115.                          : -PLAYER_SPEED*PLAYER_GND_FRICTION;
  116.     }
  117.   }
  118.  
  119.  
  120.   f32 prevRunningState = runningState;
  121.  
  122.   if(fabs(vel.x) > PLAYER_NEUTRAL) runningState += fabsf(vel.x)*PLAYER_RUN_MUL;
  123.   else                             runningState  = 0.0f;
  124.   runningState = fmodf(fabsf(runningState), 6.0f);
  125.  
  126.   //used to indicate that a foot step sound effect should play
  127.   if(prevRunningState < 1.0f  &&  runningState >= 1.0f  &&  !MID_AIR) footstep = true;
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement