Player.cpp 9.9 KB

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