IngameState.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "IngameState.hpp"
  2. #include "Player.hpp"
  3. #include <thread>
  4. IngameState::IngameState(): State(), _player(), _paused(false), _blurSize(0.f), _frameAlpha(0.f)
  5. {
  6. }
  7. Rect Player::getBounds()
  8. {
  9. Rect rect;
  10. sf::Vector2f A = _vertices[0].position;
  11. sf::Vector2f B = _vertices[1].position;
  12. sf::Vector2f C = _vertices[2].position;
  13. rect.top = std::min({A.y, B.y, C.y});
  14. rect.bottom = std::max({A.y, B.y, C.y});
  15. rect.left = std::min({A.x, B.x, C.x});
  16. rect.right = std::max({A.x, B.x, C.x});
  17. return rect;
  18. }
  19. void IngameState::init()
  20. {
  21. _background.setTexture(_context->assets->loadTexture("data/background/Background.png"));
  22. _context->assets->loadTexture("data/background/Background.png").setRepeated(true);
  23. _player.setContext(_context);
  24. _player.setTexture(_context->assets->loadTexture("data/triangle/Texture.png"));
  25. _player.setPosition(400.f, 400.f);
  26. _foreground.create("data/background/Foreground.png", "data/background/75pxjitter03.png");
  27. _context->foreground = &_foreground;
  28. _pauseFrame.setFillColor(sf::Color(0, 0, 0, 155));
  29. _pauseFrame.setPosition(0, 0);
  30. /*_unshadowShader.loadFromFile("assets/unshadowShader.frag", sf::Shader::Fragment);
  31. _lightOverShapeShader.loadFromFile("assets/lightOverShapeShader.frag", sf::Shader::Fragment);
  32. _normalsShader.loadFromFile("assets/normalsShader.frag", sf::Shader::Fragment);
  33. _penumbraTexture.loadFromFile("data/light/penumbraTexture.png");
  34. _penumbraTexture.setSmooth(true);*/
  35. /*_light = std::make_shared<ltbl::LightPointEmission>();
  36. _light->_emissionSprite.setOrigin(330.f, 871.f);
  37. _light->_emissionSprite.setPosition(0.f, 0.f);
  38. _light->_emissionSprite.setTexture(_context->assets->loadTexture("data/background/texture.png"));
  39. _light->_emissionSprite.setColor(sf::Color::White);
  40. _light->_localCastCenter = sf::Vector2f(0.0f, 0.0f); // This is where the shadows emanate from relative to the sprite*/
  41. _blurV.loadFromFile("data/shaders/old_pi_blur/blur.vert", "data/shaders/old_pi_blur/blur-v.frag");
  42. _blurH.loadFromFile("data/shaders/old_pi_blur/blur.vert", "data/shaders/old_pi_blur/blur-h.frag");
  43. _marioSound.setBuffer(_context->assets->loadSound("data/audio/Mario.ogg"));
  44. _wasted.setTexture(_context->assets->loadTexture("data/background/Wasted.png"));
  45. // Global window is not initialized yet in this method. If you need _context->window->getSize(), go to refresh() instead.
  46. _status = State::Ongoing;
  47. }
  48. void IngameState::refresh()
  49. {
  50. sf::Vector2u res = _context->window->getSize();
  51. _background.setTextureRect(sf::IntRect(0, 0, _context->window->getSize().x, _context->window->getSize().y));
  52. _camera.setSize(static_cast<sf::Vector2f>(_context->window->getSize()));
  53. _camera.setTwilightViewport(_context->window->getSize().x / 2.f - 240.f/* * (bool) _czyWOgóleTwilightViewportMaByć*/, _context->window->getSize().y / 2.f - 240.f);
  54. _camera.setCenter(_player.getPosition());
  55. _foreground.setResolution(res);
  56. _pauseFrame.setSize((sf::Vector2f)res);
  57. _wasted.setScale(_context->window->getSize().x / 1920.f, _context->window->getSize().y / 1080.f);
  58. _wasted.setPosition(0, _context->window->getSize().y);
  59. _backgroundH.create(_context->window->getSize().x, _context->window->getSize().y);
  60. _backgroundV.create(_context->window->getSize().x, _context->window->getSize().y);
  61. //_lightSystem.create(sf::FloatRect{{0.f, 0.f}, {0.f, 0.f}}, {1920, 1080}, _penumbraTexture, _unshadowShader, _lightOverShapeShader, _normalsShader);
  62. //_lightSystem.normalsEnabled(true);
  63. //_lightSystem.addLight(_light);
  64. }
  65. void IngameState::coreThink(const sf::Event& event)
  66. {
  67. if(event.type == sf::Event::Closed or (event.type == sf::Event::KeyPressed and event.key.code == sf::Keyboard::Escape))
  68. _status = State::Menu;
  69. if(event.type == sf::Event::KeyPressed and event.key.code == sf::Keyboard::P and !_player.isDead())
  70. _paused = !_paused;
  71. _player.think(event);
  72. }
  73. void IngameState::coreInput()
  74. {
  75. if(_paused)
  76. return;
  77. _player.think();
  78. }
  79. void IngameState::coreUpdate(sf::Time delta)
  80. {
  81. if(_player.isDead())
  82. {
  83. if(_blurSize < 4.f)
  84. _blurSize += delta.asSeconds();
  85. /**
  86. * This will dynamically fade the grey pause foreground from 0 to 100 alpha.
  87. */
  88. if(_frameAlpha < 100.f)
  89. _frameAlpha += (delta.asSeconds() * 25.f);
  90. sf::Color color = _pauseFrame.getFillColor();
  91. color.a = (int)_frameAlpha;
  92. _pauseFrame.setFillColor(color);
  93. _blurH.setParameter("blurSize", _blurSize / _context->window->getSize().x);
  94. _blurV.setParameter("blurSize", _blurSize / _context->window->getSize().y);
  95. if(_wasted.getPosition().y > 0.f)
  96. _wasted.move(0.f, -250.f * delta.asSeconds());
  97. else if(_wasted.getPosition().y < 0.f)
  98. _wasted.setPosition(0.f, 0.f);
  99. }
  100. if(_paused)
  101. return;
  102. const sf::Vector2f pos = _player.getPosition();
  103. _player.update(delta);
  104. _camera.move(_player.getPosition() - pos);
  105. //_light->_emissionSprite.setPosition(_player.getPosition());
  106. //_light->_emissionSprite.setRotation(_player.getRotation());
  107. _foreground.setPosition(_camera.getCenter() - _camera.getSize() / 2.f);
  108. if(_player.checkCollision(_foreground)) /// True if the target died.
  109. {
  110. _paused = true;
  111. _marioSound.play();
  112. }
  113. }
  114. void IngameState::coreRender(const bool shaders)
  115. {
  116. if(_player.isDead())
  117. {
  118. // _blurH.setParameter("RTScene", backgroundH.getTexture());
  119. // _blurV.setParameter("RTBlurH", backgroundV.getTexture());
  120. _background.setTexture(_context->assets->loadTexture("data/background/Background.png"));
  121. _backgroundH.draw(_background);
  122. _backgroundH.setView(*_camera.getView());
  123. _backgroundH.draw(_foreground);
  124. _backgroundH.draw(_player);
  125. sf::Sprite sprite(_backgroundH.getTexture());
  126. sprite.setScale(1.f, -1.f);
  127. sprite.setOrigin(0, _context->window->getSize().y);
  128. sprite.setPosition(0.f, 0.f);
  129. _backgroundV.draw(sprite, &_blurH);
  130. sf::Sprite sprite2(_backgroundV.getTexture());
  131. sprite2.setScale(1.f, -1.f);
  132. sprite2.setOrigin(0, _context->window->getSize().y);
  133. sprite2.setPosition(0.f, 0.f);
  134. _context->window->draw(sprite2, &_blurH);
  135. _context->window->draw(_pauseFrame);
  136. _context->window->draw(_wasted);
  137. }
  138. else
  139. {
  140. //_lightSystem.normalsTargetClear();
  141. _background.setTexture(_context->assets->loadTexture("data/background/Background.png"));
  142. _context->window->draw(_background);//, &_normalBackground);
  143. //_background.setTexture(_context->assets->loadTexture("data/background/normal.JPG"));
  144. //_lightSystem.normalsTargetDraw(_background);
  145. //_lightSystem.normalsTargetDraw(_foreground);
  146. _context->window->setView(_camera.getView());
  147. _context->window->draw(_foreground);
  148. //_context->window->draw(_foreground);
  149. _context->window->draw(_player);
  150. //_lightSystem.normalsTargetDisplay();
  151. //_lightSystem.render(*_camera.getView(), _unshadowShader, _lightOverShapeShader, _normalsShader);
  152. //sf::Sprite sprite(_lightSystem.getLightingTexture());
  153. //sf::RenderStates lightRenderStates;
  154. //lightRenderStates.blendMode = sf::BlendMultiply;
  155. //_context->window->draw(sprite, lightRenderStates);
  156. _context->window->setView();
  157. if(_paused)
  158. _context->window->draw(_pauseFrame);
  159. }
  160. }