Guest User

Untitled

a guest
Jan 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | None | 0 0
  1. void PlayerObject::render(SDL_Surface* display){
  2.  
  3.     currentFrameRow = animation.getCurrentFrame() / (animationState == CRASHING ? 5 : 6); //crashing spritesheet is only 5 across, the rest are 6
  4.     currentFrameCol = animation.getCurrentFrame() % (animationState == CRASHING ? 5 : 6);
  5.  
  6.     SDL_Surface* spriteSheet = NULL;
  7.   spriteSheet = displayObject;
  8.   //std::cout << state;
  9.     //dead
  10.     if (state == DEAD){
  11.         spriteSheet = displayObjectCrash;
  12.         animation.maxFrames = 18;
  13.  
  14.         currentFrameRow = (animation.maxFrames-1) / 5;
  15.         currentFrameCol = (animation.maxFrames-1) % 5;
  16.  
  17.         if (animationState == CRASHING && animation.getCurrentFrame() == animation.maxFrames-2){
  18.             animationState = DEAD;
  19.         }
  20.  
  21.     //braking
  22.     } else if (state == BRAKING){
  23.  
  24.         spriteSheet = displayObjectBrake;
  25.  
  26.         //lead-in animation to braking
  27.         if (animationState != BRAKING && animationState != BRAKING_START){
  28.             animationState = BRAKING_START;
  29.             animation.setCurrentFrame(0);
  30.             animation.maxFrames = 4;
  31.  
  32.         //lead-in is finished, main braking stage - 1 frame
  33.         } else if (animationState == BRAKING_START && animation.getCurrentFrame() == animation.maxFrames - 1){
  34.             animationState = BRAKING;
  35.             animation.setCurrentFrame(0);
  36.         }
  37.  
  38.         //special case for braking - frame is not the first on the sheet
  39.         if (animationState == BRAKING){
  40.             animation.maxFrames = 1;
  41.             currentFrameRow = 1;
  42.             currentFrameCol = 0;
  43.         }
  44.  
  45.     //accelerating
  46.     } else if (state == ACCELERATING){
  47.         spriteSheet = displayObjectAccel;
  48.  
  49.     //start accelerating - start animation from frame 0
  50.         if (animationState != ACCELERATING){
  51.             animation.maxFrames = 20;
  52.             animation.setCurrentFrame(0); //the sprite sheet gets changed, but it is a few ticks before the new animation frame starts?? maybe?
  53.         }
  54.         animationState = ACCELERATING;
  55.  
  56.     //crashing
  57.     } else if (state == CRASHING){
  58.  
  59.         //start crashing - reset animation
  60.         if (animationState != CRASHING){
  61.             animation.setCurrentFrame(0);
  62.             animationState = CRASHING;
  63.         }
  64.  
  65.         spriteSheet = displayObjectCrash;
  66.         animation.maxFrames = 18;
  67.  
  68.         //animation finished -> dead
  69.         if (animation.getCurrentFrame() == animation.maxFrames - 1){
  70.             state = DEAD;
  71.         }
  72.  
  73.     //coasting
  74.     } else {
  75.  
  76.         //just stopped braking - need to add special animation to let go of wheel
  77.         if (animationState == BRAKING){
  78.             animationState = BRAKING_STOP;
  79.             animation.maxFrames = 12;
  80.             animation.setCurrentFrame(0);
  81.             spriteSheet = displayObjectBrake;
  82.  
  83.         //if brake-stop animation is finished, change to coasting animation
  84.         } else if (animationState == BRAKING_STOP && animation.getCurrentFrame() == animation.maxFrames - 1){
  85.             animationState = COASTING;
  86.             animation.setCurrentFrame(0);
  87.             animation.maxFrames = 20;
  88.             spriteSheet = displayObject;
  89.        
  90.         //just stopped accelerating - finish off acceleration animation
  91.         } else if (animationState == ACCELERATING && animation.getCurrentFrame() == animation.maxFrames - 1){
  92.             animationState = COASTING;
  93.             animation.setCurrentFrame(0);
  94.             animation.maxFrames = 20;
  95.             spriteSheet = displayObject;
  96.         }
  97.  
  98.  
  99.         //set sprite sheet based on animationState
  100.         if (animationState == BRAKING_STOP){
  101.             spriteSheet = displayObjectBrake;
  102.  
  103.             //special case for animation - not starting at beginning of spritesheet
  104.             currentFrameRow = (animation.getCurrentFrame() + 20) / 6;
  105.             currentFrameCol = (animation.getCurrentFrame() + 20) % 6;
  106.  
  107.         } else if (animationState == COASTING){
  108.             spriteSheet = displayObject;
  109.  
  110.         } else if (animationState == ACCELERATING){
  111.             spriteSheet = displayObjectAccel;
  112.  
  113.         } else if (animationState == CRASHING){
  114.             spriteSheet = displayObjectCrash;
  115.         }
  116.     }
  117.  
  118.     if (velocity == 0 && animationState != CRASHING && animationState != DEAD){
  119.         spriteSheet = displayObject;
  120.         //animation.setCurrentFrame(0);
  121.         currentFrameRow = animation.getCurrentFrame() / 6;
  122.         currentFrameCol = animation.getCurrentFrame() % 6;
  123.         animation.maxFrames = 20;
  124.     }
  125.   /*
  126.   std::string spriteSheetStr;
  127.   if (spriteSheet == displayObject) spriteSheetStr = "idle";
  128.   else if (spriteSheet == displayObjectAccel) spriteSheetStr = "accel";
  129.   else if (spriteSheet == displayObjectBrake) spriteSheetStr = "brake";
  130.   else if (spriteSheet == displayObjectCrash) spriteSheetStr = "crash";
  131.   else spriteSheetStr = "other";
  132.  
  133.   std::cout <<
  134.     spriteSheetStr
  135.   << ' ' << animation.getCurrentFrame() << std::endl;
  136.   */
  137.     if (spriteSheet && display){
  138.         int width = this->width * (spriteSheet==displayObjectCrash ? (4.0/3.0) : 1);  //crashing sprite sheet is 400x350
  139.         int height = this->height * (spriteSheet==displayObjectCrash ? (7.0/6.0) : 1);
  140.        
  141.         //crashing -> Wheels flies out of his chair
  142.         int x = this->x;
  143.         int y = this->y;
  144.         if (spriteSheet == displayObjectCrash){
  145.             int currentFrame = (state==DEAD ? animation.maxFrames : animation.getCurrentFrame());
  146.             x -= 50; //he sits 50 pixels to the right compared with the other sprite sheets
  147.             //x += (crashVelocity/2) * currentFrame;
  148.       //cameraOffset = (crashVelocity/2) * currentFrame;
  149.             y += 5 * currentFrame;
  150.         }
  151.  
  152.         Surface::draw(display, spriteSheet,
  153.                                     x - Camera::cameraControl.getX(),
  154.                                     y - Camera::cameraControl.getY(),
  155.                                     currentFrameCol * width  //crash spritesheet images are twice as big
  156.                                         + (spriteSheet==displayObjectBrake ? 13 : 0),  //the braking sprite-sheet is 13 pixels off in both axes
  157.                                     currentFrameRow * height
  158.                     + (spriteSheet==displayObjectBrake ? -13 : 0),
  159.                                     width, height);
  160.     } else {
  161.     std::cerr << "Error: failed to load spritesheet: " << std::endl;
  162.         exit(0);
  163.     }
  164. }
Add Comment
Please, Sign In to add comment