Advertisement
Guest User

Untitled

a guest
Dec 30th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <raylib.h>
  3. #include <raymath.h>
  4.  
  5. //MAKRA
  6.  
  7. #define SIN(x) sin(x * 3.141592653589 / 180);
  8. #define COS(x) cos(x * 3.141592653589 / 180);
  9.  
  10. //FUNKCJE
  11.  
  12. float length(Vector2& distanceVec) {
  13. return sqrt(distanceVec.x * distanceVec.x + distanceVec.y * distanceVec.y);
  14. }
  15.  
  16. Vector2 normalize(Vector2& distanceVec, float& distanceNum) {
  17. return { distanceVec.x / distanceNum, distanceVec.y / distanceNum };
  18. }
  19.  
  20. double radiansToDegrees(double radians) {
  21. return radians * (180.0f / PI);
  22. }
  23.  
  24. double rotate(Vector2& objPosition, Vector2& pointPosition) {
  25. return atan2(pointPosition.y - objPosition.y, pointPosition.x - objPosition.x);
  26. }
  27.  
  28. //KLASY
  29.  
  30. class Entity {
  31. public:
  32. //ZMIENNE INT
  33. int size;
  34. int speed;
  35. int hp;
  36. //ZMIENNE WEKTOROWE
  37. Vector2 position;
  38. Vector2 direction = { 0,0 };
  39. //ZMIENNE ABSTRAKCYJNE
  40. Color color;
  41.  
  42. //FUNKCJE
  43. void entityDraw(Vector2 &entityPosition, int entitySize, Color &entityColor) {
  44. DrawCircle(entityPosition.x, entityPosition.y, entitySize, entityColor);
  45. }
  46. };
  47.  
  48. class Player : public Entity {
  49. public:
  50. //ZMIENNE
  51. bool moving = false;
  52. int dirHintX;
  53. int dirHintY;
  54. //FUNKCJE
  55. void directionUpdate(Vector2 &direction) {
  56. dirHintX = 0;
  57. dirHintY = 0;
  58. moving = false;
  59.  
  60. if (IsKeyDown(KEY_A)) dirHintX -= 1;
  61. if (IsKeyDown(KEY_D)) dirHintX += 1;
  62. if (IsKeyDown(KEY_W)) dirHintY -= 1;
  63. if (IsKeyDown(KEY_S)) dirHintY += 1;
  64.  
  65. if (dirHintX != 0 || dirHintY != 0) {
  66. moving = true;
  67. }
  68.  
  69. direction.x = position.x + dirHintX;
  70. direction.y = position.y + dirHintY;
  71. }
  72.  
  73. void playerUpdate(Vector2 &entityPosition, Vector2 &direction, int &entitySpeed, bool &isPlayerMoving) {
  74. directionUpdate(direction);
  75.  
  76. Vector2 distanceVec = { direction.x - entityPosition.x, direction.y - entityPosition.y };
  77. float distanceNum = length(distanceVec);
  78. Vector2 directionFinal = normalize(distanceVec, distanceNum);
  79.  
  80. if (isPlayerMoving == true) {
  81. entityPosition.x += entitySpeed * directionFinal.x;
  82. entityPosition.y += entitySpeed * directionFinal.y;
  83. }
  84. }
  85. };
  86.  
  87. class Sword : public Entity {
  88. public:
  89. //ZMIENNE
  90.  
  91. Vector2 pointToRot;
  92. double angle;
  93.  
  94. //FUNKCJE
  95.  
  96. void swordDraw(Vector2 &position) {
  97. Vector2 origin = { 0,0 }; // Środek obrotu
  98. Rectangle rect = { position.x, position.y, (float)size, (float)(size * 2) }; // Prostokąt miecza
  99. DrawRectanglePro(rect, origin, angle, BLUE);
  100. }
  101.  
  102. void swordUpdate(Vector2& position, Vector2& destPosition, Vector2 pointPosition, float angle) {
  103. position = destPosition;
  104.  
  105. // Oblicz kąt między graczem a myszką
  106. angle = rotate(position, pointPosition);
  107. }
  108.  
  109. };
  110.  
  111. //KLASY-ZMIENNE
  112.  
  113. Player player;
  114. Sword sword;
  115.  
  116. //MAIN
  117.  
  118.  
  119. int main()
  120. {
  121. //INICJALIZACJA!!!
  122. const int screenWidth = 640;
  123. const int screenHeight = 360;
  124.  
  125. //-Player
  126. player.size = 10;
  127. player.speed = 8;
  128. player.hp = 5;
  129. player.position = Vector2{ screenWidth / 2,screenHeight / 2 };
  130. player.color = BLACK;
  131. //-Sword
  132. sword.size = 15;
  133.  
  134. InitWindow(screenWidth, screenHeight, "Metachromatic");
  135.  
  136. SetTargetFPS(60);
  137.  
  138. //MAIN LOOP GRY!!!
  139. while (!WindowShouldClose()) {
  140. //------------------------------------------------------------------------------------------------------------
  141. //UPDATE
  142.  
  143. player.playerUpdate(player.position, player.direction, player.speed, player.moving);
  144. sword.swordUpdate(sword.position, player.position, GetMousePosition(), sword.angle);
  145.  
  146. //DRAW
  147. //DRAW-BEGIN
  148. BeginDrawing();
  149.  
  150. ClearBackground(WHITE);
  151.  
  152. player.entityDraw(player.position, player.size, player.color);
  153. sword.swordDraw(sword.position);
  154. DrawText("JACIE GYACIE DZIALA", screenWidth / 2, screenHeight / 2, 20, LIGHTGRAY);
  155.  
  156. //DRAW-END
  157. EndDrawing();
  158. //------------------------------------------------------------------------------------------------------------
  159. }
  160.  
  161. //DE-INICJALIZACJA
  162. //ZAMYKANIE OKNA GRY
  163. CloseWindow();
  164.  
  165. //KONIEC PROGRAMU
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement