IngameState.cpp 7.7 KB

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