Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <player.hpp>
- #include <math.h>
- using namespace kit;
- #define VELX_MAX (1.5f)
- #define VELY_MAX (2.5f)
- #define MID_AIR (ticksInAir>1 || jumped)
- #define RND_AMOUNT (0.5f) //0.5f for rounding, 0.0f for truncation
- #define RND(_v) ( (s32)((_v)+RND_AMOUNT) )
- #define TILE_SIZE (8) //actual sprite is 8x8
- #define TILE_ADJUST(_v) ((_v)*(TILE_SIZE+1)) //+1 for one pixel of spacing
- void Player::blit(kit::f32 scaleX, kit::f32 scaleY){ //scale is 1x by default
- if(!visible || scaleX<=0.0f || scaleY<=0.0f) return;
- shape::rect dst;
- dst.w = (s32)( TILE_SIZE*scaleX );
- dst.h = (s32)( TILE_SIZE*scaleY );
- dst.x = RND(pos.x) - (dst.w/2);
- dst.y = RND(pos.y) - (dst.h/2);
- shape::rect src(0,0, TILE_SIZE,TILE_SIZE);
- if(confused ) src.y += TILE_ADJUST(1);
- if(!facingRight) src.y += TILE_ADJUST(2);
- // >1 instead of >0 to account for ghost step
- if(MID_AIR){ //mid-air
- if(fabsf(vel.x) > PLAYER_NEUTRAL){ //going fast horizontally
- if(vel.y<=0) src.x = TILE_ADJUST(2); //-Y; back leg extended
- else src.x = TILE_ADJUST(8); //+Y; front leg extended
- } else { //going slow (or not at all) horizontally
- if( vel.y <-PLAYER_NEUTRAL) src.x = TILE_ADJUST(1); //-Y; back leg down
- else if(vel.y > PLAYER_NEUTRAL) src.x = TILE_ADJUST(4); //+Y; front leg down
- else src.x = TILE_ADJUST(7); //0Y; both legs up
- }
- } else { //on the ground
- if(vel.x < 0 && acc.x > 0){ //slowing down while moving left
- if(fabsf(vel.x) > PLAYER_NEUTRAL){ //going fast horizontally
- if(facingRight) src.x = TILE_ADJUST(2); //back leg extended
- else src.x = TILE_ADJUST(8); //front leg extended
- } else { //going slow (or not at all) horizontally
- if(facingRight) src.x = TILE_ADJUST(1); //back leg down
- else src.x = TILE_ADJUST(4); //front leg down
- }
- } else if(vel.x > 0 && acc.x < 0){ //slowing down while moving right
- if(fabsf(vel.x) > PLAYER_NEUTRAL){ //going fast horizontally
- if(facingRight) src.x = TILE_ADJUST(8); //front leg extended
- else src.x = TILE_ADJUST(2); //back leg extended
- } else { //going slow (or not at all) horizontally
- if(facingRight) src.x = TILE_ADJUST(4); //front leg down
- else src.x = TILE_ADJUST(1); //back leg down
- }
- } else if(fabsf(vel.x) > PLAYER_NEUTRAL){ //moving forward
- src.x = TILE_ADJUST(1 + RND(runningState)%6);
- } else if(acc.x < 0){ //standing still while accelerating left
- if(facingRight) src.x = TILE_ADJUST(4); //front leg down
- else src.x = TILE_ADJUST(1); //back leg down
- } else if(acc.x > 0){ //standing still while accelerating right
- if(facingRight) src.x = TILE_ADJUST(1); //back leg down
- else src.x = TILE_ADJUST(4); //front leg down
- } else { //standing still without acceleration
- src.x = TILE_ADJUST(0); //technically redundant, but left here for clarity
- }
- }
- //0xff00ff for magenta as the transparency color
- gl_spritesheetPlayer->blitRect(&dst, &src, 0xff00ff);
- }
- //acc, visible, and confused are not modified here
- void Player::update(){
- vel.x += (MID_AIR) ? acc.x*PLAYER_AIR_FRICTION : acc.x;
- vel.y += acc.y;
- if(enforceMaxVel){
- vel.x = CLAMP(vel.x, -VELX_MAX, VELX_MAX);
- vel.y = CLAMP(vel.y, -VELY_MAX, VELY_MAX);
- }
- pos += vel;
- if(fabsf(vel.x)<0.05f && acc.x==0) vel.x = 0.0f;
- if(ticksInAir <= 1 && vel.x != 0){ //if on ground while moving horizontally
- facingRight = vel.x > 0;
- if(ticksInAir <= 1 && acc.x == 0){
- vel.x -= (vel.x>0) ? PLAYER_SPEED*PLAYER_GND_FRICTION
- : -PLAYER_SPEED*PLAYER_GND_FRICTION;
- }
- }
- f32 prevRunningState = runningState;
- if(fabs(vel.x) > PLAYER_NEUTRAL) runningState += fabsf(vel.x)*PLAYER_RUN_MUL;
- else runningState = 0.0f;
- runningState = fmodf(fabsf(runningState), 6.0f);
- //used to indicate that a foot step sound effect should play
- if(prevRunningState < 1.0f && runningState >= 1.0f && !MID_AIR) footstep = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement