Advertisement
shout1232131

Untitled

Jul 8th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. /*Written 2017 by Michał Przekota.
  2. To the extent possible under law, the author(s) have dedicated all copyright to men who want to help with the problem.
  3. */
  4. #pragma once
  5.  
  6. #include "SpriteSheetAnimation.h"
  7. #include "Entity.h"
  8.  
  9. class Player: public Entity
  10. {
  11. public:
  12.     Player();
  13.     ~Player();
  14.  
  15.     void LoadContent(std::vector<std::string> attributes, std::vector<std::string> contents, SDL_Renderer *renderer);
  16.     void UnloadContent();
  17.     void Draw(SDL_Renderer *renderer);
  18.     void Update(SDL_Renderer *renderer, InputManager input);
  19.     void UpdateEvent(SDL_Event &event);
  20. private:
  21.     enum Direction
  22.     {
  23.         Right,
  24.         Left
  25.     };
  26.     Direction direction;
  27.     SpriteSheetAnimation ssAnim;
  28.  
  29.     float moveSpeed;
  30.     float grav;
  31. };
  32.  
  33. ///////////////////////////////////////////////////////////////////////////////////////////
  34.  
  35. /*Written 2017 by Michał Przekota.
  36. To the extent possible under law, the author(s) have dedicated all copyright to men who want to help with the problem.
  37. */
  38. #include "Player.h"
  39.  
  40. Player::Player()
  41. {
  42. }
  43.  
  44. Player::~Player()
  45. {
  46. }
  47.  
  48. void Player::LoadContent(std::vector<std::string> attributes, std::vector<std::string> contents, SDL_Renderer *renderer)
  49. {
  50.     Entity::LoadContent(attributes, contents, renderer);
  51.  
  52.     moveSpeed = 0.5f;
  53.     direction = Direction::Right;
  54. }
  55.  
  56. void Player::UnloadContent()
  57. {
  58.     Entity::UnloadContent();
  59.     ssAnim.UnloadContent();
  60.     Anim.UnloadContent();
  61. }
  62. void Player::Draw(SDL_Renderer *renderer)
  63. {
  64.     Entity::DrawR(renderer,Anim.GetSourceRect());
  65. }
  66.  
  67. void Player::Update(SDL_Renderer *renderer, InputManager input)
  68. {
  69.     Entity::Update(input);
  70.  
  71.     Anim.getActive() = true;
  72.     if (input.KeyDown(SDLK_LEFT))
  73.     {
  74.         direction = Direction::Left;
  75.         posX -= moveSpeed;
  76.     }
  77.     else if (input.KeyDown(SDLK_RIGHT))
  78.     {
  79.         direction = Direction::Right;
  80.         posX += moveSpeed;
  81.     }
  82.     else if (input.KeyDown(SDLK_UP))
  83.     {
  84.         posY -= moveSpeed;
  85.     }
  86.  
  87.     else if (input.KeyDown(SDLK_DOWN))
  88.     {
  89.         posY += moveSpeed;
  90.     }
  91.     else
  92.     {
  93.         Anim.getActive() = false;
  94.     }
  95.  
  96.     Anim.CurrentFrame().second = direction;
  97.     Anim.getPositionX() = posX;
  98.     Anim.getPositionY() = posY;
  99.     ssAnim.Update(Anim);
  100.  
  101.     delete rect;
  102.     delete prevRect;
  103.  
  104.     rect = new FloatRect(posX, posY, 41, 60);
  105.     prevRect = new FloatRect(prevPosX, prevPosY, 41, 60);
  106. }
  107.  
  108. void Player::UpdateEvent(SDL_Event &event)
  109. {
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement