Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. struct PlayerEntity
  2. {
  3.     sf::Sprite* spriteptr;
  4.     MoveSpeed movespd;
  5.     JumpMultiplier jump;
  6.  
  7.     short x_movement = 0;
  8.     unsigned short fallCounter = 0;
  9.     unsigned short jumpFrames = 0;
  10.  
  11.     virtual XyMove getInput() abstract;
  12.     virtual void setTextureRect() abstract;
  13.  
  14.  
  15.     virtual float getFallSpeed()
  16.     {
  17.         return 3.2f*pow(1 + .025f,fallCounter);
  18.     }
  19.  
  20.     virtual float getJumpSpeed()
  21.     {
  22.         return -1 * (jump * (16.f*pow(1 + .08f,jumpFrames)));
  23.     }
  24.  
  25.     virtual void process()
  26.     {
  27.         inputHandler(getInput());
  28.         moveCharacter();
  29.         jumpProcess();
  30.  
  31.         if(jumpFrames == 0)
  32.             groundContactProcess();
  33.         else fallCounter = 0;
  34.     }
  35.  
  36.     virtual void inputHandler(const XyMove& movement)
  37.     {
  38.         if(!movement.doesNotMoveX())
  39.         {
  40.             if(movement.left)
  41.                 x_movement--;
  42.             if(movement.right)
  43.                 x_movement++;
  44.  
  45.             if(abs(x_movement) > movespd)
  46.                 x_movement = movement.left ? -1*movespd : movespd;
  47.         }
  48.         else
  49.             if(abs(x_movement) > 0) x_movement > 0 ? x_movement-- : x_movement++;
  50.  
  51.         if(movement.jump && fallCounter == 0 && jumpFrames == 0)
  52.             jumpFrames = 12;
  53.     }
  54.  
  55.     virtual void jumpProcess()
  56.     {
  57.         if(jumpFrames > 0)
  58.         {
  59.             moveCharacter(getJumpSpeed());
  60.             jumpFrames--;
  61.         }
  62.     }
  63.  
  64.     virtual short checkGroundContact()
  65.     {
  66.         for(unsigned short i=0; i < MAP.blocks.size();i++)
  67.         {
  68.             if(MAP.blocks.at(i).getGlobalBounds().intersects(spriteptr->getGlobalBounds()))
  69.                 return i;
  70.         }
  71.         return -1;
  72.     }
  73.  
  74.     virtual void groundContactProcess()
  75.     {
  76.         unsigned short counter = 0;
  77.         short index = 0;
  78.  
  79.         spriteptr->setTextureRect(sf::IntRect(
  80.                     0,0,spriteptr->getTextureRect().width,
  81.                     spriteptr->getTextureRect().height / 4));
  82.  
  83.         moveCharacter(spriteptr->getTextureRect().height * 3);
  84.  
  85.         while((index = checkGroundContact()) == -1 && ++counter < 4)
  86.              moveCharacter(getFallSpeed(),4);
  87.  
  88.         setTextureRect();
  89.  
  90.         if(index < 0)
  91.         {
  92.             moveCharacter(-1*(spriteptr->getTextureRect().height / 4 * 3));
  93.             fallCounter++;
  94.             return;
  95.         }
  96.  
  97.         warpToGround(index);
  98.         fallCounter = 0;
  99.     }
  100.  
  101.     virtual void warpToGround(short index)
  102.     {
  103.         spriteptr->setPosition(
  104.             spriteptr->getPosition().x,
  105.             MAP.blocks.at(index).getPosition().y - spriteptr->getTextureRect().height);
  106.     }
  107.  
  108.  
  109.     virtual void drawCharacter()
  110.         { app.draw( *spriteptr ); }
  111.  
  112.     virtual sf::Vector2f getPosition() const
  113.         { return spriteptr->getPosition(); }
  114.  
  115.     virtual void moveCharacter()
  116.         { spriteptr->move(x_movement,0); }
  117.  
  118.     virtual void moveCharacter(float y_speed, float divisor = 1)
  119.         { spriteptr->move(0, y_speed / divisor); }
  120.  
  121.  
  122.     PlayerEntity(sf::Sprite& sprite, MoveSpeed movespeed, JumpMultiplier jumpMultiplier)
  123.     {
  124.         spriteptr = &sprite;
  125.         movespd = movespeed;
  126.         jump = jumpMultiplier;
  127.     }
  128.     virtual ~PlayerEntity() {}
  129. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement