Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct PlayerEntity
- {
- sf::Sprite* spriteptr;
- MoveSpeed movespd;
- JumpMultiplier jump;
- short x_movement = 0;
- unsigned short fallCounter = 0;
- unsigned short jumpFrames = 0;
- virtual XyMove getInput() abstract;
- virtual void setTextureRect() abstract;
- virtual float getFallSpeed()
- {
- return 3.2f*pow(1 + .025f,fallCounter);
- }
- virtual float getJumpSpeed()
- {
- return -1 * (jump * (16.f*pow(1 + .08f,jumpFrames)));
- }
- virtual void process()
- {
- inputHandler(getInput());
- moveCharacter();
- jumpProcess();
- if(jumpFrames == 0)
- groundContactProcess();
- else fallCounter = 0;
- }
- virtual void inputHandler(const XyMove& movement)
- {
- if(!movement.doesNotMoveX())
- {
- if(movement.left)
- x_movement--;
- if(movement.right)
- x_movement++;
- if(abs(x_movement) > movespd)
- x_movement = movement.left ? -1*movespd : movespd;
- }
- else
- if(abs(x_movement) > 0) x_movement > 0 ? x_movement-- : x_movement++;
- if(movement.jump && fallCounter == 0 && jumpFrames == 0)
- jumpFrames = 12;
- }
- virtual void jumpProcess()
- {
- if(jumpFrames > 0)
- {
- moveCharacter(getJumpSpeed());
- jumpFrames--;
- }
- }
- virtual short checkGroundContact()
- {
- for(unsigned short i=0; i < MAP.blocks.size();i++)
- {
- if(MAP.blocks.at(i).getGlobalBounds().intersects(spriteptr->getGlobalBounds()))
- return i;
- }
- return -1;
- }
- virtual void groundContactProcess()
- {
- unsigned short counter = 0;
- short index = 0;
- spriteptr->setTextureRect(sf::IntRect(
- 0,0,spriteptr->getTextureRect().width,
- spriteptr->getTextureRect().height / 4));
- moveCharacter(spriteptr->getTextureRect().height * 3);
- while((index = checkGroundContact()) == -1 && ++counter < 4)
- moveCharacter(getFallSpeed(),4);
- setTextureRect();
- if(index < 0)
- {
- moveCharacter(-1*(spriteptr->getTextureRect().height / 4 * 3));
- fallCounter++;
- return;
- }
- warpToGround(index);
- fallCounter = 0;
- }
- virtual void warpToGround(short index)
- {
- spriteptr->setPosition(
- spriteptr->getPosition().x,
- MAP.blocks.at(index).getPosition().y - spriteptr->getTextureRect().height);
- }
- virtual void drawCharacter()
- { app.draw( *spriteptr ); }
- virtual sf::Vector2f getPosition() const
- { return spriteptr->getPosition(); }
- virtual void moveCharacter()
- { spriteptr->move(x_movement,0); }
- virtual void moveCharacter(float y_speed, float divisor = 1)
- { spriteptr->move(0, y_speed / divisor); }
- PlayerEntity(sf::Sprite& sprite, MoveSpeed movespeed, JumpMultiplier jumpMultiplier)
- {
- spriteptr = &sprite;
- movespd = movespeed;
- jump = jumpMultiplier;
- }
- virtual ~PlayerEntity() {}
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement