Player.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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(2.f),
  26. _movementMomentum(0.f),
  27. _movementAcceleration(10.f),
  28. _movementSuppresion(10.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. _particleSystem(1920, 1080)
  37. {
  38. /**
  39. * Default binding
  40. */
  41. if(sf::Joystick::isConnected(0))
  42. {
  43. _inputMap.map(1, Input(sf::Joystick::Axis::Y, false));
  44. _inputMap.map(2, Input(sf::Joystick::Axis::Y, true));
  45. _inputMap.map(3, Input(sf::Joystick::Axis::Z, false));
  46. _inputMap.map(4, Input(sf::Joystick::Axis::Z, true));
  47. _inputMap.map(5, Input(7));
  48. }
  49. else
  50. {
  51. _inputMap.map(1, Input(sf::Keyboard::W));
  52. _inputMap.map(2, Input(sf::Keyboard::S));
  53. _inputMap.map(3, Input(sf::Keyboard::A));
  54. _inputMap.map(4, Input(sf::Keyboard::D));
  55. _inputMap.map(5, Input(sf::Keyboard::Space));
  56. }
  57. /**
  58. * Input lambdas
  59. */
  60. bind(1, [this](const sf::Event&, float power)
  61. {
  62. _movement += power;
  63. });
  64. bind(2, [this](const sf::Event&, float power)
  65. {
  66. _movement -= power * 0.8f;
  67. });
  68. bind(3, [this](const sf::Event&, float power)
  69. {
  70. _rotation -= power;
  71. });
  72. bind(4, [this](const sf::Event&, float power)
  73. {
  74. _rotation += power;
  75. });
  76. bind(5, [this](const sf::Event&, float)
  77. {
  78. if(_bulletTimer.getElapsedTime().asMilliseconds() > 1500)
  79. {
  80. // Does not check if _context != nullptr
  81. // so please do remember to call setContext().
  82. _bullets.emplace_back(getRotation(), getPosition(),
  83. _context->assets->loadSound("data/audio/Shotgun.ogg"),
  84. _context->assets->loadSound("data/audio/Bomb.ogg"),
  85. _context->foreground);
  86. _bullets.back().setTexture(_innerTexture);
  87. _bulletTimer.restart();
  88. _movementMomentum -= 3.f;
  89. }
  90. });
  91. /**
  92. * Particles
  93. */
  94. _particleSystem.setDissolutionRate(255.f);
  95. /**
  96. * Technical
  97. */
  98. genCollisionBox("data/hitbox/Player.png", sf::Vector2f(71.f, 100.f));
  99. _bullets.reserve(10);
  100. _bulletTimer.restart();
  101. _durability = 0.99f;
  102. }
  103. void Player::setContext(Context* context)
  104. {
  105. _context = context;
  106. _glass.setBuffer(_context->assets->loadSound("data/audio/GlassHit.ogg"));
  107. _deathSound.setBuffer(_context->assets->loadSound("data/audio/GlassRekt.ogg"));
  108. }
  109. bool Player::isDead()
  110. {
  111. return _dead;
  112. }
  113. void Player::setTexture(sf::Texture& texture)
  114. {
  115. _innerTexture = texture;
  116. _image = texture.copyToImage();
  117. _texture = &_innerTexture;
  118. vertexFit();
  119. unsigned pointCount = 0;
  120. for(unsigned i = 0; i < _image.getSize().x; ++i)
  121. for(unsigned j = 0; j < _image.getSize().y; ++j)
  122. if(_image.getPixel(i, j).a > 0)
  123. ++pointCount;
  124. setPointCount(pointCount);
  125. }
  126. bool Player::checkCollision(Foreground& foreground)
  127. {
  128. updateTransform(getTransform());
  129. int k = pixelPerfect(foreground);
  130. if(k == -1)
  131. {
  132. if(_glass.getStatus() != sf::Sound::Status::Playing)
  133. _glass.setVolume(0.f);
  134. _hit = false;
  135. return false;
  136. }
  137. float i = 10.f;
  138. while(k != -1)
  139. {
  140. sf::Vector2f pos = _cmap[k];
  141. sf::Vector2f center = sf::Vector2f(_image.getSize().x / 2.f, 100.f);
  142. 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)
  143. _image.setPixel(center.x + pos.x, center.y + pos.y, sf::Color(0, 0, 0, 0));
  144. for(int i = -1; i <= 1; i++)
  145. for(int j = -1; j <= 1; j++)
  146. {
  147. if(i == 0 && j == 0)
  148. continue;
  149. 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)
  150. continue;
  151. if(_image.getPixel(pos.x + center.x + i, pos.y + center.y + j).a > 0)
  152. {
  153. sf::Vector2f vec;
  154. vec.x = pos.x + i;
  155. vec.y = pos.y + j;
  156. if(std::find(_cmap.begin(), _cmap.end(), vec) == _cmap.end())
  157. if(std::find(_cbanned.begin(), _cbanned.end(), vec) == _cbanned.end())
  158. _cmap.push_back(vec);
  159. }
  160. }
  161. _cmap.erase(_cmap.begin() + k);
  162. _cbanned.push_back(pos);
  163. k = pixelPerfect(foreground);
  164. if(i < 100.f) i += 0.1f;
  165. }
  166. if(i > _glass.getVolume())
  167. _glass.setVolume(i);
  168. if(_glass.getStatus() != sf::Sound::Status::Playing) _glass.play();
  169. _innerTexture.loadFromImage(_image);
  170. _hit = true;
  171. if(_pointCount * _durability < _cbanned.size())
  172. {
  173. _dead = true;
  174. _deathSound.play();
  175. return true;
  176. }
  177. else
  178. return false;
  179. }
  180. void Player::keyboardControls(sf::Time delta)
  181. {
  182. float seconds = delta.asSeconds();
  183. const double degree = 3.14159265358 / 180.;
  184. if(_hit)
  185. {
  186. if(_movement > 0 || _movementMomentum > 0)
  187. _movementMomentum -= seconds * 15.f;
  188. if(_movement < 0 || _movementMomentum < 0)
  189. _movementMomentum += seconds * 15.f;
  190. _movement *= 0.6f;
  191. }
  192. _movementMomentum += _movementAcceleration * seconds * _movement;
  193. if(_movement == 0.f)
  194. {
  195. if(_movementMomentum > _movementSuppresion * seconds)
  196. _movementMomentum -= _movementSuppresion * seconds;
  197. else if(_movementMomentum < -_movementSuppresion * seconds)
  198. _movementMomentum += _movementSuppresion * seconds;
  199. else
  200. _movementMomentum = 0.f;
  201. }
  202. sf::Vector2f movement;
  203. movement.x = std::sin(getRotation() * degree) * 60.f * _movementSpeed * _movementMomentum * seconds;
  204. movement.y = -std::cos(getRotation() * degree) * 60.f * _movementSpeed * _movementMomentum * seconds;
  205. move(movement);
  206. _particleSystem.fuel(movement.x * 20.f, getRotation());
  207. _particleSystem.fuel(movement.y * 20.f, getRotation());
  208. _movement = 0.f;
  209. _rotationMomentum += _rotationAcceleration * seconds * _rotation;
  210. if(_rotationMomentum > _rotationSuppresion * seconds)
  211. _rotationMomentum -= _rotationSuppresion * seconds;
  212. else if(_rotationMomentum < -_rotationSuppresion * seconds)
  213. _rotationMomentum += _rotationSuppresion * seconds;
  214. else
  215. _rotationMomentum = 0.f;
  216. float rotation = 90.f * _rotationSpeed * _rotationMomentum * seconds;
  217. rotate(rotation);
  218. _rotation = 0.f;
  219. }
  220. void Player::padControls(sf::Time delta)
  221. {
  222. float seconds = delta.asSeconds();
  223. const double degree = 3.14159265358 / 180.;
  224. sf::Vector2f v = sf::Vector2f(
  225. sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::X) / 100.f,
  226. sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::Y) / 100.f);
  227. float curr = getRotation();
  228. static float supposed;
  229. if(v.x != 0 or v.y != 0)
  230. supposed = atan2(v.x, -v.y) / degree;
  231. if(supposed < 0.f)
  232. supposed += 360.f;
  233. float c = supposed - curr;
  234. if(curr > 180.f && supposed < 180.f)
  235. {
  236. float a = (supposed - curr);
  237. float b = (supposed - curr + 360.f);
  238. if(std::fabs(a) < std::fabs(b))
  239. c = a;
  240. else
  241. c = b;
  242. }
  243. if(curr < 180.f && supposed > 180.f)
  244. {
  245. float a = (supposed - curr);
  246. float b = (supposed - curr - 360.f);
  247. if(std::fabs(a) < std::fabs(b))
  248. c = a;
  249. else
  250. c = b;
  251. }
  252. if(v.x != 0 or v.y != 0)
  253. {
  254. if(c > 0.f)
  255. _rotationMomentum += _rotationAcceleration * seconds;
  256. else if(c < 0.f)
  257. _rotationMomentum -= _rotationAcceleration * seconds;
  258. }
  259. if(_rotationMomentum > _rotationSuppresion * seconds)
  260. _rotationMomentum -= _rotationSuppresion * seconds;
  261. else if(_rotationMomentum < -_rotationSuppresion * seconds)
  262. _rotationMomentum += _rotationSuppresion * seconds;
  263. else
  264. _rotationMomentum = 0.f;
  265. float rotation = 90.f * _rotationSpeed * _rotationMomentum * seconds;
  266. rotate(rotation);
  267. if((v.x != 0 || v.y != 0) && c < 1.f)
  268. _movement = std::fabs(v.x + v.y);
  269. if(_hit)
  270. {
  271. if(_movement > 0 || _movementMomentum > 0)
  272. _movementMomentum -= seconds * 15.f;
  273. if(_movement < 0 || _movementMomentum < 0)
  274. _movementMomentum += seconds * 15.f;
  275. _movement *= 0.6f;
  276. }
  277. _movementMomentum += _movementAcceleration * seconds * _movement;
  278. if(_movement == 0.f)
  279. {
  280. if(_movementMomentum > _movementSuppresion * seconds)
  281. _movementMomentum -= _movementSuppresion * seconds;
  282. else if(_movementMomentum < -_movementSuppresion * seconds)
  283. _movementMomentum += _movementSuppresion * seconds;
  284. else
  285. _movementMomentum = 0.f;
  286. }
  287. sf::Vector2f movement;
  288. movement.x = std::sin(getRotation() * degree) * 60.f * _movementSpeed * _movementMomentum * seconds;
  289. movement.y = -std::cos(getRotation() * degree) * 60.f * _movementSpeed * _movementMomentum * seconds;
  290. move(movement);
  291. _particleSystem.fuel(movement.x * 20.f, getRotation());
  292. _particleSystem.fuel(movement.y * 20.f, getRotation());
  293. _movement = 0.f;
  294. }
  295. void Player::update(sf::Time delta)
  296. {
  297. //if(sf::Joystick::isConnected(0))
  298. //padControls(delta);
  299. //else
  300. keyboardControls(delta);
  301. for(unsigned i = 0; i < _bullets.size(); ++i)
  302. {
  303. _bullets[i].update(delta);
  304. if(_bullets[i].isDead())
  305. _bullets.erase(_bullets.begin() + i);
  306. }
  307. _particleSystem.setPosition(getPosition());
  308. _particleSystem.clear();
  309. _particleSystem.update(delta);
  310. _particleSystem.render();
  311. }
  312. void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const
  313. {
  314. target.draw(_particleSystem.getSprite());
  315. for(unsigned i = 0; i < _bullets.size(); ++i)
  316. target.draw(_bullets[i]);
  317. states.transform *= getTransform();
  318. if(_texture != nullptr)
  319. states.texture = (_texture);
  320. target.draw(_vertices, states);
  321. }