Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void PlayerObject::render(SDL_Surface* display){
- currentFrameRow = animation.getCurrentFrame() / (animationState == CRASHING ? 5 : 6); //crashing spritesheet is only 5 across, the rest are 6
- currentFrameCol = animation.getCurrentFrame() % (animationState == CRASHING ? 5 : 6);
- SDL_Surface* spriteSheet = NULL;
- spriteSheet = displayObject;
- //std::cout << state;
- //dead
- if (state == DEAD){
- spriteSheet = displayObjectCrash;
- animation.maxFrames = 18;
- currentFrameRow = (animation.maxFrames-1) / 5;
- currentFrameCol = (animation.maxFrames-1) % 5;
- if (animationState == CRASHING && animation.getCurrentFrame() == animation.maxFrames-2){
- animationState = DEAD;
- }
- //braking
- } else if (state == BRAKING){
- spriteSheet = displayObjectBrake;
- //lead-in animation to braking
- if (animationState != BRAKING && animationState != BRAKING_START){
- animationState = BRAKING_START;
- animation.setCurrentFrame(0);
- animation.maxFrames = 4;
- //lead-in is finished, main braking stage - 1 frame
- } else if (animationState == BRAKING_START && animation.getCurrentFrame() == animation.maxFrames - 1){
- animationState = BRAKING;
- animation.setCurrentFrame(0);
- }
- //special case for braking - frame is not the first on the sheet
- if (animationState == BRAKING){
- animation.maxFrames = 1;
- currentFrameRow = 1;
- currentFrameCol = 0;
- }
- //accelerating
- } else if (state == ACCELERATING){
- spriteSheet = displayObjectAccel;
- //start accelerating - start animation from frame 0
- if (animationState != ACCELERATING){
- animation.maxFrames = 20;
- animation.setCurrentFrame(0); //the sprite sheet gets changed, but it is a few ticks before the new animation frame starts?? maybe?
- }
- animationState = ACCELERATING;
- //crashing
- } else if (state == CRASHING){
- //start crashing - reset animation
- if (animationState != CRASHING){
- animation.setCurrentFrame(0);
- animationState = CRASHING;
- }
- spriteSheet = displayObjectCrash;
- animation.maxFrames = 18;
- //animation finished -> dead
- if (animation.getCurrentFrame() == animation.maxFrames - 1){
- state = DEAD;
- }
- //coasting
- } else {
- //just stopped braking - need to add special animation to let go of wheel
- if (animationState == BRAKING){
- animationState = BRAKING_STOP;
- animation.maxFrames = 12;
- animation.setCurrentFrame(0);
- spriteSheet = displayObjectBrake;
- //if brake-stop animation is finished, change to coasting animation
- } else if (animationState == BRAKING_STOP && animation.getCurrentFrame() == animation.maxFrames - 1){
- animationState = COASTING;
- animation.setCurrentFrame(0);
- animation.maxFrames = 20;
- spriteSheet = displayObject;
- //just stopped accelerating - finish off acceleration animation
- } else if (animationState == ACCELERATING && animation.getCurrentFrame() == animation.maxFrames - 1){
- animationState = COASTING;
- animation.setCurrentFrame(0);
- animation.maxFrames = 20;
- spriteSheet = displayObject;
- }
- //set sprite sheet based on animationState
- if (animationState == BRAKING_STOP){
- spriteSheet = displayObjectBrake;
- //special case for animation - not starting at beginning of spritesheet
- currentFrameRow = (animation.getCurrentFrame() + 20) / 6;
- currentFrameCol = (animation.getCurrentFrame() + 20) % 6;
- } else if (animationState == COASTING){
- spriteSheet = displayObject;
- } else if (animationState == ACCELERATING){
- spriteSheet = displayObjectAccel;
- } else if (animationState == CRASHING){
- spriteSheet = displayObjectCrash;
- }
- }
- if (velocity == 0 && animationState != CRASHING && animationState != DEAD){
- spriteSheet = displayObject;
- //animation.setCurrentFrame(0);
- currentFrameRow = animation.getCurrentFrame() / 6;
- currentFrameCol = animation.getCurrentFrame() % 6;
- animation.maxFrames = 20;
- }
- /*
- std::string spriteSheetStr;
- if (spriteSheet == displayObject) spriteSheetStr = "idle";
- else if (spriteSheet == displayObjectAccel) spriteSheetStr = "accel";
- else if (spriteSheet == displayObjectBrake) spriteSheetStr = "brake";
- else if (spriteSheet == displayObjectCrash) spriteSheetStr = "crash";
- else spriteSheetStr = "other";
- std::cout <<
- spriteSheetStr
- << ' ' << animation.getCurrentFrame() << std::endl;
- */
- if (spriteSheet && display){
- int width = this->width * (spriteSheet==displayObjectCrash ? (4.0/3.0) : 1); //crashing sprite sheet is 400x350
- int height = this->height * (spriteSheet==displayObjectCrash ? (7.0/6.0) : 1);
- //crashing -> Wheels flies out of his chair
- int x = this->x;
- int y = this->y;
- if (spriteSheet == displayObjectCrash){
- int currentFrame = (state==DEAD ? animation.maxFrames : animation.getCurrentFrame());
- x -= 50; //he sits 50 pixels to the right compared with the other sprite sheets
- //x += (crashVelocity/2) * currentFrame;
- //cameraOffset = (crashVelocity/2) * currentFrame;
- y += 5 * currentFrame;
- }
- Surface::draw(display, spriteSheet,
- x - Camera::cameraControl.getX(),
- y - Camera::cameraControl.getY(),
- currentFrameCol * width //crash spritesheet images are twice as big
- + (spriteSheet==displayObjectBrake ? 13 : 0), //the braking sprite-sheet is 13 pixels off in both axes
- currentFrameRow * height
- + (spriteSheet==displayObjectBrake ? -13 : 0),
- width, height);
- } else {
- std::cerr << "Error: failed to load spritesheet: " << std::endl;
- exit(0);
- }
- }
Add Comment
Please, Sign In to add comment