Bullet.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * one room arena
  3. * Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <cmath>
  18. #include "Bullet.hpp"
  19. #include "Map.hpp"
  20. #include "Player.hpp"
  21. Bullet::Bullet(Vector2f position, float angle, int strength):
  22. _map(nullptr),
  23. _self(nullptr),
  24. _dead(false),
  25. _strength(strength),
  26. _madeByPlayer(false)
  27. {
  28. this->setPosition(position);
  29. this->setRotation(angle);
  30. }
  31. void Bullet::registerMap(Map* map)
  32. {
  33. _map = map;
  34. }
  35. void Bullet::registerSelf(Player* player)
  36. {
  37. _self = player;
  38. }
  39. RectBullet::RectBullet(Vector2f position, float angle, int strength): Bullet(position, angle, strength)
  40. {
  41. this->setSize(Vector2f(2.f, 50.f));
  42. this->setOrigin(1.f, 25.f);
  43. _speed = 1200.f;
  44. _explosive = false;
  45. }
  46. NukeBullet::NukeBullet(Vector2f position, float angle, int strength): Bullet(position, angle, strength)
  47. {
  48. _badlyAllocatedTexture.loadFromFile("data/fatman.png");
  49. this->setSize(Vector2f(64.f, 128.f));
  50. this->setOrigin(32.f, 64.f);
  51. this->setTexture(&_badlyAllocatedTexture);
  52. _speed = 200.f;
  53. _explosive = true;
  54. }
  55. void Bullet::update(float delta)
  56. {
  57. this->move(Vector2f(std::sin(getRotation() * 3.1415f / 180.f), -std::cos(getRotation() * 3.1415f / 180.f)) * _speed * delta);
  58. for(unsigned i = 0; i < getPointCount(); ++i)
  59. {
  60. Vector2f pos = getTransform().transformPoint(getPoint(i));
  61. if(_map->collision(pos))
  62. {
  63. if(_explosive)
  64. {
  65. _map->destroy(getPosition(), _strength);
  66. }
  67. _dead = true;
  68. // delete this;
  69. }
  70. for(unsigned j = 0; j < Player::aiPlayer->size(); ++j)
  71. {
  72. if(Player::aiPlayer->at(j) == _self)
  73. continue;
  74. if(std::pow(Player::aiPlayer->at(j)->getPosition().x - pos.x, 2) + std::pow(Player::aiPlayer->at(j)->getPosition().y - pos.y, 2) < 50*50)
  75. {
  76. Player::aiPlayer->at(j)->kill(_madeByPlayer);
  77. _dead = true;
  78. break;
  79. }
  80. }
  81. if(Player::actualPlayer != _self)
  82. {
  83. if(std::pow(Player::actualPlayer->getPosition().x - pos.x, 2) + std::pow(Player::actualPlayer->getPosition().y - pos.y, 2) < 50*50)
  84. {
  85. Player::actualPlayer->kill(false);
  86. _dead = true;
  87. }
  88. }
  89. }
  90. }
  91. bool Bullet::isDead() const
  92. {
  93. return _dead;
  94. }
  95. bool Bullet::isExplosive() const
  96. {
  97. return _explosive;
  98. }
  99. void Bullet::setColor(Color color)
  100. {
  101. this->setFillColor(color);
  102. }
  103. void Bullet::madeByPlayer()
  104. {
  105. _madeByPlayer = true;
  106. }