Player.cpp 11 KB

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