Player.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * Triangles
  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 "Player.hpp"
  19. #include "Assets.hpp"
  20. Player::Player():
  21. Triangle(),
  22. InputTarget(_inputMap),
  23. Collidable(),
  24. _movement(0.f),
  25. _movementSpeed(4.f),
  26. _movementMomentum(0.f),
  27. _movementAcceleration(10.f),
  28. _movementSuppresion(5.f),
  29. _rotation(0.f),
  30. _rotationSpeed(0.6f),
  31. _rotationMomentum(0.f),
  32. _rotationAcceleration(38.f),
  33. _rotationSuppresion(20.f),
  34. _hit(false),
  35. _dead(false)
  36. {
  37. /**
  38. * Default binding
  39. */
  40. _inputMap.duoMap(1, Input(sf::Keyboard::Up), Input(sf::Keyboard::W));
  41. _inputMap.duoMap(2, Input(sf::Keyboard::Down), Input(sf::Keyboard::S));
  42. _inputMap.duoMap(3, Input(sf::Keyboard::Left), Input(sf::Keyboard::A));
  43. _inputMap.duoMap(4, Input(sf::Keyboard::Right), Input(sf::Keyboard::D));
  44. _inputMap.map(5, Input(sf::Keyboard::Space));
  45. /**
  46. * Input lambdas
  47. */
  48. duoBind(1, [this](const sf::Event&)
  49. {
  50. _movement += 1.f;
  51. });
  52. duoBind(2, [this](const sf::Event&)
  53. {
  54. _movement -= 0.8f;
  55. });
  56. duoBind(3, [this](const sf::Event&)
  57. {
  58. _rotation -= 1.f;
  59. });
  60. duoBind(4, [this](const sf::Event&)
  61. {
  62. _rotation += 1.f;
  63. });
  64. bind(5, [this](const sf::Event&)
  65. {
  66. if(_bulletTimer.getElapsedTime().asMilliseconds() > 1500)
  67. {
  68. // Does not check if _context != nullptr
  69. // so please do remember to call setContext().
  70. _bullets.emplace_back(getRotation(), getPosition(),
  71. _context->assets->loadSound("data/audio/Shotgun.ogg"),
  72. _context->assets->loadSound("data/audio/Bomb.ogg"),
  73. _context->foreground);
  74. _bullets.back().setTexture(_innerTexture);
  75. _bulletTimer.restart();
  76. _movementMomentum -= 3.f;
  77. }
  78. });
  79. /**
  80. * Technical
  81. */
  82. genCollisionBox("data/hitbox/Player.png", sf::Vector2f(71.f, 100.f));
  83. _bullets.reserve(10);
  84. _bulletTimer.restart();
  85. _durability = 0.99f;
  86. }
  87. void Player::setContext(Context* context)
  88. {
  89. _context = context;
  90. _glass.setBuffer(_context->assets->loadSound("data/audio/GlassHit.ogg"));
  91. _deathSound.setBuffer(_context->assets->loadSound("data/audio/GlassRekt.ogg"));
  92. }
  93. bool Player::isDead()
  94. {
  95. return _dead;
  96. }
  97. void Player::setTexture(sf::Texture& texture)
  98. {
  99. _innerTexture = texture;
  100. _image = texture.copyToImage();
  101. _texture = &_innerTexture;
  102. vertexFit();
  103. unsigned pointCount = 0;
  104. for(unsigned i = 0; i < _image.getSize().x; ++i)
  105. for(unsigned j = 0; j < _image.getSize().y; ++j)
  106. if(_image.getPixel(i, j).a > 0)
  107. ++pointCount;
  108. setPointCount(pointCount);
  109. }
  110. bool Player::checkCollision(Foreground& foreground)
  111. {
  112. updateTransform(getTransform());
  113. int k = pixelPerfect(foreground);
  114. if(k == -1)
  115. {
  116. if(_glass.getStatus() != sf::Sound::Status::Playing)
  117. _glass.setVolume(0.f);
  118. _hit = false;
  119. return false;
  120. }
  121. float i = 10.f;
  122. while(k != -1)
  123. {
  124. sf::Vector2f pos = _cmap[k];
  125. sf::Vector2f center = sf::Vector2f(_image.getSize().x / 2.f, 100.f);
  126. if(center.x + pos.x >= 0 && center.x + pos.x < _image.getSize().x and center.y + pos.y >= 0 && center.y + pos.y < _image.getSize().y)
  127. _image.setPixel(center.x + pos.x, center.y + pos.y, sf::Color(0, 0, 0, 0));
  128. for(int i = -1; i <= 1; i++)
  129. for(int j = -1; j <= 1; j++)
  130. {
  131. if(i == 0 && j == 0)
  132. continue;
  133. if(pos.x + center.x + i < 0 || pos.y + center.y + j < 0 or pos.x + center.x + i > _image.getSize().x || pos.y + center.y + j > _image.getSize().y)
  134. continue;
  135. if(_image.getPixel(pos.x + center.x + i, pos.y + center.y + j).a > 0)
  136. {
  137. sf::Vector2f vec;
  138. vec.x = pos.x + i;
  139. vec.y = pos.y + j;
  140. if(std::find(_cmap.begin(), _cmap.end(), vec) == _cmap.end())
  141. if(std::find(_cbanned.begin(), _cbanned.end(), vec) == _cbanned.end())
  142. _cmap.push_back(vec);
  143. }
  144. }
  145. _cmap.erase(_cmap.begin() + k);
  146. _cbanned.push_back(pos);
  147. k = pixelPerfect(foreground);
  148. if(i < 100.f) i += 0.1f;
  149. }
  150. if(i > _glass.getVolume())
  151. _glass.setVolume(i);
  152. if(_glass.getStatus() != sf::Sound::Status::Playing) _glass.play();
  153. _innerTexture.loadFromImage(_image);
  154. _hit = true;
  155. if(_pointCount * _durability < _cbanned.size())
  156. {
  157. _dead = true;
  158. _deathSound.play();
  159. return true;
  160. }
  161. else
  162. return false;
  163. }
  164. void Player::update(sf::Time delta)
  165. {
  166. float seconds = delta.asSeconds();
  167. const double degree = 3.14159265358 / 180.;
  168. if(_hit)
  169. {
  170. if(_movement > 0 || _movementMomentum > 0)
  171. _movementMomentum -= seconds * 15.f;
  172. if(_movement < 0 || _movementMomentum < 0)
  173. _movementMomentum += seconds * 15.f;
  174. _movement *= 0.6f;
  175. }
  176. _movementMomentum += _movementAcceleration * seconds * _movement;
  177. if(_movementMomentum > _movementSuppresion * seconds)
  178. _movementMomentum -= _movementSuppresion * seconds;
  179. else if(_movementMomentum < -_movementSuppresion * seconds)
  180. _movementMomentum += _movementSuppresion * seconds;
  181. else
  182. _movementMomentum = 0.f;
  183. sf::Vector2f movement;
  184. movement.x = std::sin(getRotation() * degree) * 60.f * _movementSpeed * _movementMomentum * seconds;
  185. movement.y = -std::cos(getRotation() * degree) * 60.f * _movementSpeed * _movementMomentum * seconds;
  186. move(movement);
  187. //px.move(movement);
  188. _movement = 0.f;
  189. _rotationMomentum += _rotationAcceleration * seconds * _rotation;
  190. if(_rotationMomentum > _rotationSuppresion * seconds)
  191. _rotationMomentum -= _rotationSuppresion * seconds;
  192. else if(_rotationMomentum < -_rotationSuppresion * seconds)
  193. _rotationMomentum += _rotationSuppresion * seconds;
  194. else
  195. _rotationMomentum = 0.f;
  196. float rotation = 90.f * _rotationSpeed * _rotationMomentum * seconds;
  197. rotate(rotation);
  198. //px.rotate(rotation);
  199. _rotation = 0.f;
  200. for(unsigned i = 0; i < _bullets.size(); ++i)
  201. {
  202. _bullets[i].update(delta);
  203. if(_bullets[i].isDead())
  204. _bullets.erase(_bullets.begin() + i);
  205. }
  206. }
  207. void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const
  208. {
  209. for(unsigned i = 0; i < _bullets.size(); ++i)
  210. target.draw(_bullets[i]);
  211. states.transform *= getTransform();
  212. if(_texture != nullptr)
  213. states.texture = (_texture);
  214. target.draw(_vertices, states);
  215. }