Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Player.h"
- extern Game game;
- extern int TILE_SIZE;
- Player::Player()
- {
- xPos = 100;
- yPos = 500;
- width = 20;
- height = 30;
- starting_xPos = 100;
- starting_yPos = 500;
- clip = 0;
- for ( int c = 0; c < 10; c++ )
- {
- clips[c].x = c * width;
- clips[c].y = 0;
- clips[c].w = width;
- clips[c].h = height;
- }
- yForce = 0;
- xForce = 0;
- delayed_yForce = 0;
- direction = 1;
- onGround = false;
- doubleJump = true;
- left = false;
- right = false;
- hit = false;
- imgPlayer = game.load_image( "Assets/player.png" );
- }
- Player::~Player()
- {
- SDL_FreeSurface( imgPlayer );
- }
- void Player::handle_input()
- {
- if ( game.event.type == SDL_KEYDOWN )
- {
- switch( game.event.key.keysym.sym )
- {
- case SDLK_w:
- if ( onGround == true )
- {
- yForce = -15;
- }
- else if ( doubleJump == true )
- {
- yForce = -15;
- doubleJump = false;
- }
- break;
- case SDLK_a:
- left = true;
- direction = 0;
- break;
- case SDLK_d:
- right = true;
- direction = 1;
- break;
- case SDLK_ESCAPE:
- game.quit = true;
- break;
- }
- }
- if ( game.event.type == SDL_KEYUP )
- {
- switch( game.event.key.keysym.sym )
- {
- case SDLK_a:
- left = false;
- break;
- case SDLK_d:
- right = false;
- break;
- }
- }
- }
- void Player::logic( Tile* tiles[] )
- {
- //set the forces
- yForce++;
- if ( yForce > 15 )
- {
- yForce = 15;
- }
- xForce += right - left;
- if ( right - left == 0 )
- {
- if ( xForce < 0 )
- xForce++;
- if ( xForce > 0 )
- xForce--;
- }
- if ( xForce > 6 )
- xForce = 6;
- if ( xForce < -6 )
- xForce = -6;
- //move and check collision
- move( tiles );
- //change the animation
- animate();
- //set the camera over the player
- set_camera();
- }
- void Player::render()
- {
- game.apply_surface( xPos - game.camera.x, yPos - game.camera.y, imgPlayer, game.buffer, &clips[clip] );
- }
- void Player::move( Tile* tiles[] )
- {
- //set OnGround equal to false
- onGround = false;
- //corners of the window
- int startX = game.camera.x / TILE_SIZE;
- int endX = (game.camera.x + game.camera.w - 1) / TILE_SIZE;
- int startY = game.camera.y / TILE_SIZE;
- int endY = (game.camera.y + game.camera.h - 1) / TILE_SIZE;
- //move and check collision horizontally
- xPos += xForce;
- for ( int x = startX; x < endX; x++ )
- {
- for ( int y = startY; y < endY; y++ )
- {
- if ( tiles[y * game.map_width + x]->type != 0 )
- {
- if ( game.check_collision( xPos, yPos, width, height, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE ) == true )
- {
- //if we hit ground
- if ( tiles[y * game.map_width + x]->type == 1 )
- {
- //while there is still a collision
- while ( game.check_collision(xPos, yPos, width, height, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE) == true )
- {
- //move back 1 pixel
- if ( xForce < 0 )
- xPos++;
- if ( xForce > 0 )
- xPos--;
- }
- //set the xForce to zero
- xForce = 0;
- }
- //if we hit toxic
- if ( tiles[y * game.map_width + x]->type == 2 )
- {
- respawn();
- }
- }
- }
- }
- }
- //move and check collision vertically
- yPos += yForce;
- for ( int x = startX; x < endX; x++ )
- {
- for ( int y = startY; y < endY; y++ )
- {
- if ( tiles[y * game.map_width + x]->type != 0 )
- {
- if ( game.check_collision( xPos, yPos, width, height, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE ) == true )
- {
- //if we hit ground
- if ( tiles[y * game.map_width + x]->type == 1 )
- {
- //while there is still a collision
- while ( game.check_collision(xPos, yPos, width, height, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE) == true )
- {
- //move back 1 pixel
- if ( yForce < 0 )
- yPos++;
- if ( yForce > 0 )
- yPos--;
- }
- //set the yForce to zero
- yForce = 0;
- //see if the ground is below us
- if ( yPos < y * TILE_SIZE )
- {
- onGround = true;
- doubleJump = true;
- break;
- }
- }
- //if we hit toxic
- if ( tiles[y * game.map_width + x]->type == 2 )
- {
- respawn();
- }
- //if we hit a lift
- if ( tiles[y * game.map_width + x]->type == 3 )
- {
- delayed_yForce = yForce - 2;
- if ( yForce < -12 )
- delayed_yForce = -12;
- }
- }
- }
- }
- }
- if ( delayed_yForce != -1234 )
- {
- yForce = delayed_yForce;
- delayed_yForce = -1234;
- }
- }
- void Player::animate()
- {
- //right
- if ( direction == 1 )
- {
- //idle
- if ( onGround == true && xForce == 0 )
- clip = 0;
- //running
- if ( onGround == true && xForce != 0 )
- {
- if ( game.frame % 6 < 3 )
- clip = 1;
- else
- clip = 2;
- }
- //jumping
- if ( onGround == false && yForce < 0 )
- clip = 3;
- //falling
- if ( onGround == false && yForce > 0 )
- clip = 4;
- }
- //left
- if ( direction == 0 )
- {
- //idle
- if ( onGround == true && xForce == 0 )
- clip = 5;
- //running
- if ( onGround == true && xForce != 0 )
- {
- if ( game.frame % 6 < 3 )
- clip = 6;
- else
- clip = 7;
- }
- //jumping
- if ( onGround == false && yForce < 0 )
- clip = 8;
- //falling
- if ( onGround == false && yForce > 0 )
- clip = 9;
- }
- }
- void Player::respawn()
- {
- xPos = starting_xPos;
- yPos = starting_yPos;
- direction = 1;
- }
- void Player::set_camera()
- {
- //set the camera over the player
- game.camera.x = ( xPos + width / 2) - game.screen_width / 2;
- game.camera.y = ( yPos + height / 2) - game.screen_height / 2;
- //check the camera against the sides
- if( game.camera.x < 0 )
- {
- game.camera.x = 0;
- }
- if( game.camera.y < 0 )
- {
- game.camera.y = 0;
- }
- if( game.camera.x > game.map_width * TILE_SIZE - game.camera.w )
- {
- game.camera.x = game.map_width * TILE_SIZE - game.camera.w;
- }
- if( game.camera.y > game.map_height * TILE_SIZE - game.camera.h )
- {
- game.camera.y = game.map_height * TILE_SIZE - game.camera.h;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement