Player.cpp 9.9 KB

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