Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

programmer.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. doJump = closest->GetY() < pos.y;
  27. moveDown = closest->GetY() > pos.y;
  28. walkLeft = closest->GetX() < pos.x;
  29. walkRight = closest->GetX() > pos.x;
  30. } else {
  31. doJump = false;
  32. walkLeft = false;
  33. walkRight = false;
  34. moveDown = false;
  35. }
  36. }
  37. void Programmer::Update(float dt)
  38. {
  39. pos += vel * dt;
  40. vel.x *= 1 - 0.2 * dt;
  41. if (!onGround) {
  42. vel.y += (70 + tile.x * 30) * dt;
  43. vel.y *= 1 - 0.05 * dt;
  44. } else {
  45. vel.x *= 1 - 3 * dt;
  46. }
  47. transform.Reset();
  48. bool userInput = false;
  49. if (doJump && onGround) {
  50. vel.y = -100 - tile.x * 30;
  51. userInput = true;
  52. }
  53. if (walkLeft) {
  54. vel.x -= (150 + tile.x * 30) * dt;
  55. userInput = true;
  56. }
  57. if (walkRight) {
  58. vel.x += (150 + tile.x * 30) * dt;
  59. userInput = true;
  60. }
  61. if (moveDown) {
  62. if (!onGround) {
  63. vel.y += (150 + tile.x * 30) * dt;
  64. }
  65. userInput = true;
  66. }
  67. if (pos.x > game->ScreenWidth() - size.x / 2) {
  68. vel.x *= -0.3;
  69. pos.x = game->ScreenWidth() - size.x / 2;
  70. olc::SOUND::PlaySample(game->playerFallSample);
  71. }
  72. if (pos.x < size.x / 2) {
  73. vel.x *= -0.3;
  74. pos.x = size.x / 2;
  75. olc::SOUND::PlaySample(game->playerFallSample);
  76. }
  77. auto groundLevel = game->GameScreenHeight() - size.y / 2 - 1;
  78. if (pos.y > groundLevel) {
  79. vel.y *= -0.3;
  80. if (std::abs(vel.y) < 2) {
  81. vel.y = 0;
  82. }
  83. pos.y = groundLevel;
  84. }
  85. bool newOnGround = pos.y >= groundLevel;
  86. if (newOnGround && !onGround) {
  87. olc::SOUND::PlaySample(game->playerFallSample);
  88. }
  89. onGround = newOnGround;
  90. if (onGround) {
  91. tile.y = 2;
  92. tileCounter = static_cast<uint32_t>(0.1 / dt);
  93. } else if (tileCounter <= 0) {
  94. tile.y = 0;
  95. } else {
  96. tileCounter--;
  97. }
  98. if (!userInput && std::abs(vel.x) < 2) {
  99. vel.x = 0;
  100. }
  101. if (walkLeft) {
  102. transform.Scale(-1, 1);
  103. }
  104. transform.Translate(pos.x, pos.y);
  105. }
  106. void Programmer::Draw()
  107. {
  108. int xOff = walkLeft ? -1 : 0;
  109. if (std::abs(vel.x) < 1) {
  110. olc::GFX2D::DrawPartialSprite(&game->playerSprite, tile.x * size.x + xOff, size.y + tile.y * size.y, size.x - xOff, size.y, transform, true);
  111. } else {
  112. olc::GFX2D::DrawPartialSprite(&game->playerSprite, tile.x * size.x + xOff, 0 + tile.y * size.y, size.x - xOff, size.y, transform, true);
  113. }
  114. #ifdef _DEBUG
  115. if (game->debug) {
  116. game->DrawRect(pos.x - size.x / 2, pos.y - size.y / 2, size.x, size.y, DEBUG_COLOR);
  117. }
  118. #endif
  119. }
  120. }