Advertisement
sbloom85

Character.cpp

Jan 27th, 2023
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include "Character.h"
  2. #include "raymath.h"
  3.  
  4. Character::Character()
  5. {
  6.     width = texture.width / maxFrames;
  7.     height = texture.height;
  8. }
  9.  
  10. void Character::setScreenPos(int winWidth, int winHeight)
  11. {
  12.     screenPos = {(float)winWidth / 2.0f - 4.0f * (0.5f * width / 6.0f),
  13.                  (float)winHeight / 2.0f - 4.0f * (0.5f * height)};
  14. }
  15.  
  16. void Character::tick(float deltaTime)
  17. {
  18.     worldPosLastFrame = worldPos;
  19.     Vector2 direction{};
  20.     if (IsKeyDown(KEY_A))
  21.         direction.x -= 1.0;
  22.     if (IsKeyDown(KEY_D))
  23.         direction.x += 1.0;
  24.     if (IsKeyDown(KEY_W))
  25.         direction.y -= 1.0;
  26.     if (IsKeyDown(KEY_S))
  27.         direction.y += 1.0;
  28.     if (Vector2Length(direction) != 0.0)
  29.     {
  30.  
  31.         worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(direction), speed));
  32.         direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;
  33.         texture = run;
  34.     }
  35.     else
  36.     {
  37.         texture = idle;
  38.     }
  39.  
  40.     runningTime += deltaTime;
  41.     if (runningTime >= updateTime)
  42.     {
  43.         frame++;
  44.         runningTime = 0.f;
  45.         if (frame > maxFrames)
  46.             frame = 0;
  47.     }
  48.  
  49.     // Draw Player
  50.     Rectangle source{frame * width, 0.f, rightLeft * width, height};
  51.     Rectangle dest{screenPos.x, screenPos.y, 4.0f * width, 4.0f * height};
  52.     DrawTexturePro(texture, source, dest, Vector2{}, 0.0, WHITE);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement