Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 KB | None | 0 0
  1. #include "Player.h"
  2.  
  3. extern Game game;
  4. extern int TILE_SIZE;
  5.  
  6. Player::Player()
  7. {
  8.     xPos = 100;
  9.     yPos = 500;
  10.     width = 20;
  11.     height = 30;
  12.  
  13.     starting_xPos = 100;
  14.     starting_yPos = 500;
  15.  
  16.     clip = 0;
  17.     for ( int c = 0;  c < 10;  c++ )
  18.     {
  19.         clips[c].x = c * width;
  20.         clips[c].y = 0;
  21.         clips[c].w = width;
  22.         clips[c].h = height;
  23.     }
  24.  
  25.     yForce = 0;
  26.     xForce = 0;
  27.     delayed_yForce = 0;
  28.  
  29.     direction = 1;
  30.  
  31.     onGround = false;
  32.     doubleJump = true;
  33.     left = false;
  34.     right = false;
  35.     hit = false;
  36.  
  37.     imgPlayer = game.load_image( "Assets/player.png" );
  38. }
  39.  
  40. Player::~Player()
  41. {
  42.     SDL_FreeSurface( imgPlayer );
  43. }
  44.  
  45. void Player::handle_input()
  46. {
  47.     if ( game.event.type == SDL_KEYDOWN )
  48.     {
  49.         switch( game.event.key.keysym.sym )
  50.         {
  51.         case SDLK_w:
  52.             if ( onGround == true )
  53.             {
  54.                 yForce = -15;
  55.             }
  56.             else if ( doubleJump == true )
  57.             {
  58.                 yForce = -15;
  59.                 doubleJump = false;
  60.             }
  61.             break;
  62.  
  63.         case SDLK_a:
  64.             left = true;
  65.             direction = 0;
  66.             break;
  67.  
  68.         case SDLK_d:
  69.             right = true;
  70.             direction = 1;
  71.             break;
  72.  
  73.         case SDLK_ESCAPE:
  74.             game.quit = true;
  75.             break;
  76.         }
  77.     }
  78.  
  79.     if ( game.event.type == SDL_KEYUP )
  80.     {
  81.         switch( game.event.key.keysym.sym )
  82.         {
  83.         case SDLK_a:
  84.             left = false;
  85.             break;
  86.  
  87.         case SDLK_d:
  88.             right = false;
  89.             break;
  90.         }
  91.     }
  92. }
  93.  
  94. void Player::logic( Tile* tiles[] )
  95. {
  96.   //set the forces
  97.     yForce++;
  98.     if ( yForce > 15 )
  99.     {
  100.         yForce = 15;
  101.     }
  102.     xForce += right - left;
  103.     if ( right - left == 0 )
  104.     {
  105.         if ( xForce < 0 )
  106.             xForce++;
  107.         if ( xForce > 0 )
  108.             xForce--;
  109.     }
  110.     if ( xForce > 6 )
  111.         xForce = 6;
  112.     if ( xForce < -6 )
  113.         xForce = -6;
  114.  
  115.   //move and check collision
  116.     move( tiles );
  117.  
  118.   //change the animation
  119.     animate();
  120.  
  121.   //set the camera over the player
  122.     set_camera();
  123.  
  124.  
  125. }
  126.  
  127. void Player::render()
  128. {
  129.     game.apply_surface( xPos - game.camera.x, yPos - game.camera.y, imgPlayer, game.buffer, &clips[clip] );
  130. }
  131.  
  132.  
  133. void Player::move( Tile* tiles[] )
  134. {
  135.   //set OnGround equal to false
  136.     onGround = false;
  137.  
  138.   //corners of the window
  139.     int startX = game.camera.x / TILE_SIZE;
  140.     int endX = (game.camera.x + game.camera.w - 1) / TILE_SIZE;
  141.     int startY = game.camera.y / TILE_SIZE;
  142.     int endY = (game.camera.y + game.camera.h - 1) / TILE_SIZE;
  143.  
  144.   //move and check collision horizontally
  145.     xPos += xForce;
  146.     for ( int x = startX;  x < endX;  x++ )
  147.     {
  148.         for ( int y = startY;  y < endY;  y++ )
  149.         {
  150.             if ( tiles[y * game.map_width + x]->type != 0 )
  151.             {
  152.                 if ( game.check_collision( xPos, yPos, width, height, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE ) == true )
  153.                 {
  154.                   //if we hit ground
  155.                     if ( tiles[y * game.map_width + x]->type == 1 )
  156.                     {
  157.                       //while there is still a collision
  158.                         while ( game.check_collision(xPos, yPos, width, height, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE) == true )
  159.                         {
  160.                           //move back 1 pixel
  161.                             if ( xForce < 0 )
  162.                                 xPos++;
  163.                             if ( xForce > 0 )
  164.                                 xPos--;
  165.                         }
  166.    
  167.                       //set the xForce to zero
  168.                         xForce = 0;
  169.                     }
  170.                    
  171.                   //if we hit toxic
  172.                     if ( tiles[y * game.map_width + x]->type == 2 )
  173.                     {
  174.                         respawn();
  175.                     }
  176.                 }
  177.             }
  178.         }
  179.     }
  180.  
  181.  
  182.   //move and check collision vertically
  183.     yPos += yForce;
  184.     for ( int x = startX;  x < endX;  x++ )
  185.     {
  186.         for ( int y = startY;  y < endY;  y++ )
  187.         {
  188.             if ( tiles[y * game.map_width + x]->type != 0 )
  189.             {
  190.                 if ( game.check_collision( xPos, yPos, width, height, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE ) == true )
  191.                 {
  192.                   //if we hit ground
  193.                     if ( tiles[y * game.map_width + x]->type == 1 )
  194.                     {
  195.                       //while there is still a collision
  196.                         while ( game.check_collision(xPos, yPos, width, height, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE) == true )
  197.                         {
  198.                           //move back 1 pixel
  199.                             if ( yForce < 0 )
  200.                                 yPos++;
  201.                             if ( yForce > 0 )
  202.                                 yPos--;
  203.                         }
  204.    
  205.                       //set the yForce to zero
  206.                         yForce = 0;
  207.                        
  208.                       //see if the ground is below us
  209.                         if ( yPos < y * TILE_SIZE )
  210.                         {
  211.                             onGround = true;
  212.                             doubleJump = true;
  213.                             break;
  214.                         }
  215.                     }
  216.                    
  217.                   //if we hit toxic
  218.                     if ( tiles[y * game.map_width + x]->type == 2 )
  219.                     {
  220.                         respawn();
  221.                     }
  222.  
  223.                   //if we hit a lift
  224.                     if ( tiles[y * game.map_width + x]->type == 3 )
  225.                     {
  226.                         delayed_yForce = yForce - 2;
  227.                         if ( yForce < -12 )
  228.                             delayed_yForce = -12;
  229.                     }
  230.                 }
  231.             }
  232.         }
  233.     }
  234.  
  235.     if ( delayed_yForce != -1234 )
  236.     {
  237.         yForce = delayed_yForce;
  238.         delayed_yForce = -1234;
  239.     }
  240.  
  241. }
  242.  
  243. void Player::animate()
  244. {
  245.   //right
  246.     if ( direction == 1 )
  247.     {
  248.       //idle
  249.         if ( onGround == true && xForce == 0 )
  250.             clip = 0;
  251.  
  252.       //running
  253.         if ( onGround == true && xForce != 0 )
  254.         {
  255.             if ( game.frame % 6 < 3 )
  256.                 clip = 1;
  257.             else
  258.                 clip = 2;
  259.         }
  260.  
  261.       //jumping
  262.         if ( onGround == false && yForce < 0 )
  263.             clip = 3;
  264.  
  265.       //falling
  266.         if ( onGround == false && yForce > 0 )
  267.             clip = 4;
  268.     }
  269.  
  270.   //left
  271.     if ( direction == 0 )
  272.     {
  273.       //idle
  274.         if ( onGround == true && xForce == 0 )
  275.             clip = 5;
  276.  
  277.       //running
  278.         if ( onGround == true && xForce != 0 )
  279.         {
  280.             if ( game.frame % 6 < 3 )
  281.                 clip = 6;
  282.             else
  283.                 clip = 7;
  284.         }
  285.  
  286.       //jumping
  287.         if ( onGround == false && yForce < 0 )
  288.             clip = 8;
  289.  
  290.       //falling
  291.         if ( onGround == false && yForce > 0 )
  292.             clip = 9;
  293.     }
  294. }
  295.  
  296. void Player::respawn()
  297. {
  298.     xPos = starting_xPos;
  299.     yPos = starting_yPos;
  300.     direction = 1;
  301. }
  302.  
  303. void Player::set_camera()
  304. {
  305.   //set the camera over the player
  306.     game.camera.x = ( xPos + width / 2) - game.screen_width / 2;
  307.     game.camera.y = ( yPos + height / 2) - game.screen_height / 2;
  308.  
  309.   //check the camera against the sides
  310.     if( game.camera.x < 0 )
  311.     {
  312.         game.camera.x = 0;
  313.     }
  314.     if( game.camera.y < 0 )
  315.     {
  316.         game.camera.y = 0;
  317.     }
  318.     if( game.camera.x > game.map_width * TILE_SIZE - game.camera.w )
  319.     {
  320.         game.camera.x = game.map_width * TILE_SIZE - game.camera.w;
  321.     }
  322.     if( game.camera.y > game.map_height * TILE_SIZE - game.camera.h )
  323.     {
  324.         game.camera.y = game.map_height * TILE_SIZE - game.camera.h;
  325.     }
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement