Açıklama Yok
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.

entity.cpp 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "entity.h"
  2. #include "debuggers.h"
  3. namespace pabloader {
  4. Entity::Entity(Debuggers* game, olc::Sprite* s)
  5. : sprite(s)
  6. , pos(0, 0)
  7. , vel(0, 0)
  8. , size(s->width / 4, s->height / 4)
  9. {
  10. Entity::game = game;
  11. }
  12. void Entity::Update(float dt)
  13. {
  14. pos += vel * dt;
  15. vel.y += game->gravity * dt;
  16. rotation += rotationSpeed * dt;
  17. transform.Reset();
  18. transform.Rotate(rotation);
  19. transform.Translate(pos.x, pos.y);
  20. }
  21. bool Entity::Collides(Entity* entity)
  22. {
  23. bool collidesX = (pos.x - size.x / 2 < entity->pos.x + entity->size.x / 2) && (entity->pos.x - entity->size.x / 2 < pos.x + size.x / 2);
  24. bool collidesY = (pos.y - size.y / 2 < entity->pos.y + entity->size.y / 2) && (entity->pos.y - entity->size.y / 2 < pos.y + size.y / 2);
  25. return collidesX && collidesY;
  26. }
  27. void Entity::Draw()
  28. {
  29. if (IsInScreen()) {
  30. #ifdef _DEBUG
  31. if (game->debug) {
  32. game->DrawRect(pos.x - size.x / 2, pos.y - size.y / 2, size.x, size.y, DEBUG_COLOR);
  33. }
  34. #endif
  35. game->SetPixelMode(olc::Pixel::MASK);
  36. olc::GFX2D::DrawPartialSprite(sprite, tile.x * size.x, tile.y * size.y, size.x, size.y, transform, true);
  37. game->SetPixelMode(olc::Pixel::NORMAL);
  38. }
  39. }
  40. bool Entity::IsActive()
  41. {
  42. return pos.y - size.y / 2 < game->GameScreenHeight();
  43. }
  44. bool Entity::IsInScreen()
  45. {
  46. return pos.x > -size.x / 2 && pos.y > -size.y
  47. && pos.x < game->ScreenWidth() + size.x / 2
  48. && pos.y < game->ScreenHeight() + size.y / 2;
  49. }
  50. bool Entity::IsAlive()
  51. {
  52. return alive;
  53. }
  54. void Entity::ResetPosition()
  55. {
  56. pos.x = game->GetRandom(size.x, game->ScreenWidth() - size.x);
  57. pos.y = game->GetRandom(-20, -game->ScreenHeight());
  58. vel.x = 0;
  59. vel.y = 0;
  60. tile.x = game->GetRandom<int>(0, tileDim.x);
  61. tile.y = game->GetRandom<int>(0, tileDim.y);
  62. rotationSpeed = game->GetRandom(-3.14f, 3.14f);
  63. alive = true;
  64. }
  65. void Entity::Kill()
  66. {
  67. pos.y = game->ScreenHeight() + 100;
  68. alive = false;
  69. }
  70. }