Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

programmer.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "programmer.h"
  2. #include "debuggers.h"
  3. #include "olcPGEX_Sound.h"
  4. #include <iostream>
  5. namespace pabloader {
  6. Programmer::Programmer(Debuggers* game)
  7. : Entity(game, &game->playerSprite)
  8. {
  9. pos = { (float)(game->ScreenWidth() - size.x) / 2, (float)size.y / 2 };
  10. #ifdef _DEBUG
  11. DEBUG_COLOR = olc::GREEN;
  12. #endif
  13. }
  14. void Programmer::Think()
  15. {
  16. Bug* closest = nullptr;
  17. float dist = 1e100;
  18. for (auto& bug : game->bugs) {
  19. float d = bug->Dist(this);
  20. if (d < dist && bug->IsInScreen() && bug->IsAlive()) {
  21. dist = d;
  22. closest = bug;
  23. }
  24. }
  25. if (closest != nullptr) {
  26. #ifdef _DEBUG
  27. target.x = closest->GetX();
  28. target.y = closest->GetY();
  29. #endif
  30. doJump = closest->GetY() < pos.y;
  31. moveDown = closest->GetY() > pos.y;
  32. walkLeft = closest->GetX() < pos.x;
  33. walkRight = closest->GetX() > pos.x;
  34. } else {
  35. #ifdef _DEBUG
  36. target.x = 0;
  37. target.y = 0;
  38. #endif
  39. doJump = false;
  40. walkLeft = false;
  41. walkRight = false;
  42. moveDown = false;
  43. }
  44. }
  45. void Programmer::Update(float dt)
  46. {
  47. pos += vel * dt;
  48. vel.x *= 1 - 0.2 * dt;
  49. if (!onGround) {
  50. vel.y += (70 + tile.x * 30) * dt;
  51. vel.y *= 1 - 0.05 * dt;
  52. } else {
  53. vel.x *= 1 - 2 * dt;
  54. }
  55. transform.Reset();
  56. bool userInput = false;
  57. if (doJump && onGround) {
  58. vel.y = -100 - tile.x * 30;
  59. userInput = true;
  60. }
  61. if (walkLeft) {
  62. vel.x -= (200 + tile.x * 30) * dt;
  63. userInput = true;
  64. }
  65. if (walkRight) {
  66. vel.x += (200 + tile.x * 30) * dt;
  67. userInput = true;
  68. }
  69. if (moveDown) {
  70. if (!onGround) {
  71. vel.y += (200 + tile.x * 30) * dt;
  72. }
  73. userInput = true;
  74. }
  75. if (pos.x > game->ScreenWidth() - size.x / 2) {
  76. vel.x *= -0.3;
  77. pos.x = game->ScreenWidth() - size.x / 2;
  78. if (game->GetState() == STATE_GAME)
  79. olc::SOUND::PlaySample(game->playerFallSample);
  80. }
  81. if (pos.x < size.x / 2) {
  82. vel.x *= -0.3;
  83. pos.x = size.x / 2;
  84. if (game->GetState() == STATE_GAME)
  85. olc::SOUND::PlaySample(game->playerFallSample);
  86. }
  87. auto groundLevel = game->GameScreenHeight() - size.y / 2 - 1;
  88. if (pos.y > groundLevel) {
  89. vel.y *= -0.2;
  90. if (std::abs(vel.y) < 3) {
  91. vel.y = 0;
  92. }
  93. pos.y = groundLevel;
  94. }
  95. bool newOnGround = pos.y >= groundLevel;
  96. if (newOnGround && !onGround && game->GetState() == STATE_GAME) {
  97. olc::SOUND::PlaySample(game->playerFallSample);
  98. }
  99. onGround = newOnGround;
  100. if (onGround) {
  101. tile.y = 2;
  102. tileCounter = static_cast<uint32_t>(0.1 / dt);
  103. } else if (tileCounter <= 0) {
  104. tile.y = 0;
  105. } else {
  106. tileCounter--;
  107. }
  108. if (!userInput && std::abs(vel.x) < 2) {
  109. vel.x = 0;
  110. }
  111. if (walkLeft) {
  112. transform.Scale(-1, 1);
  113. }
  114. transform.Translate(pos.x, pos.y);
  115. }
  116. void Programmer::Draw()
  117. {
  118. game->SetPixelMode(olc::Pixel::MASK);
  119. int xOff = walkLeft ? -1 : 0;
  120. if (walkLeft || walkRight) {
  121. olc::GFX2D::DrawPartialSprite(&game->playerSprite, tile.x * size.x + xOff, 0 + tile.y * size.y, size.x - xOff, size.y, transform, true);
  122. } else {
  123. olc::GFX2D::DrawPartialSprite(&game->playerSprite, tile.x * size.x + xOff, size.y + tile.y * size.y, size.x - xOff, size.y, transform, true);
  124. }
  125. game->SetPixelMode(olc::Pixel::NORMAL);
  126. #ifdef _DEBUG
  127. if (game->debug) {
  128. game->DrawRect(pos.x - size.x / 2, pos.y - size.y / 2, size.x, size.y, DEBUG_COLOR);
  129. if (target.x != 0 || target.y != 0) {
  130. game->DrawLine(pos.x, pos.y, target.x, target.y, DEBUG_COLOR);
  131. }
  132. }
  133. #endif
  134. }
  135. }