very early prototype of a spaceshooter in SFML
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

game.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/System.hpp>
  3. #include <SFML/Window.hpp>
  4. #include "main.hpp"
  5. #include "game.hpp"
  6. namespace Proto4 {
  7. bool Game::init() {
  8. mainView.setCenter(0, 0);
  9. mainView.setSize(xResolution, yResolution);
  10. return true;
  11. }
  12. AppState Game::handleKey(const sf::Event &event) {
  13. switch (state) {
  14. case GameState::Paused:
  15. switch (event.key.code) {
  16. case sf::Keyboard::Q:
  17. state = GameState::Finished;
  18. break;
  19. case sf::Keyboard::S:
  20. state = GameState::Running;
  21. break;
  22. default:
  23. break;
  24. }
  25. break;
  26. case GameState::Running:
  27. if (event.key.code == sf::Keyboard::Q)
  28. state = GameState::Paused;
  29. default:
  30. break;
  31. }
  32. return AppState::Game;
  33. }
  34. AppState Game::update(sf::RenderWindow &window, sf::Time timestep) {
  35. switch (state) {
  36. case GameState::Finished:
  37. reset();
  38. return AppState::MainMenu;
  39. case GameState::Uninitialized:
  40. start();
  41. break;
  42. case GameState::Paused:
  43. break;
  44. case GameState::Running:
  45. level.update(timestep);
  46. player.update(timestep, window.mapPixelToCoords(sf::Mouse::getPosition(window), mainView));
  47. // mainView acts as camera. TODO: introduce lagging camera
  48. mainView.setCenter(player.getPosition());
  49. break;
  50. default:
  51. break;
  52. }
  53. return AppState::Game;
  54. }
  55. void Game::draw(sf::RenderTarget &target, sf::RenderStates states) const {
  56. states.transform *= getTransform();
  57. target.setView(mainView);
  58. target.draw(level, states);
  59. target.draw(player, states);
  60. }
  61. bool Game::reset() {
  62. player.reset();
  63. state = GameState::Uninitialized;
  64. return true;
  65. }
  66. bool Game::start() {
  67. player.setPosition(mainView.getCenter());
  68. level = Level(mainView.getCenter());
  69. level.spawn(200);
  70. level.spawn(200);
  71. level.spawn(200);
  72. level.spawn(700);
  73. level.spawn(100);
  74. level.spawn(100);
  75. level.spawn(100);
  76. level.spawn(100);
  77. level.spawn(100);
  78. level.spawn(100);
  79. state = GameState::Running;
  80. return true;
  81. }
  82. // TODO: pause menu
  83. void Game::pause() {
  84. if (state == GameState::Running)
  85. state = GameState::Paused;
  86. }
  87. void Game::resume() {
  88. if (state == GameState::Paused)
  89. state = GameState::Running;
  90. }
  91. void Game::resize(sf::Event &resizeEvent) {
  92. mainView.setSize(resizeEvent.size.width, resizeEvent.size.height);
  93. mainView.setCenter(resizeEvent.size.width / 2, resizeEvent.size.height / 2);
  94. }
  95. sf::View& Game::getMainView() { return mainView; }
  96. }