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.

level.cpp 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "level.hpp"
  2. #include "vec2util.hpp"
  3. #include "random.hpp"
  4. namespace Proto4 {
  5. Ground::Ground() {}
  6. Ground::Ground(sf::Vector2f pos, float size, sf::Time fadeDuration)
  7. : sf::CircleShape(size, 50)
  8. , Timer(fadeDuration) {
  9. setFillColor(sf::Color(90, 100, 120));
  10. setOrigin(size, size);
  11. setPosition(pos);
  12. }
  13. // TODO: self destruct once 0?
  14. void Ground::update(sf::Time timestep) {
  15. Timer::update(timestep);
  16. sf::Color col = getFillColor();
  17. col.a = getPercent() * 255;
  18. setFillColor(col);
  19. }
  20. /////////////////////////////////////////
  21. Level::Level() {}
  22. Level::Level(sf::Vector2f pos = sf::Vector2f(0, 0)) {
  23. setPosition(pos);
  24. // initialize with first ground
  25. spawn(400);
  26. Proto4::rotate(lastSpawn.direction, sf::Vector2f(1, 1), randomFloat() * 360.f);
  27. }
  28. void Level::update(sf::Time timestep) {
  29. for (auto &ground : groundArea) {
  30. ground.update(timestep);
  31. }
  32. }
  33. void Level::draw(sf::RenderTarget &target, sf::RenderStates states) const {
  34. states.transform *= getTransform();
  35. for (auto &ground : groundArea) {
  36. target.draw(ground, states);
  37. }
  38. }
  39. void Level::spawn(float size) {
  40. lastSpawn.direction = normalized(Proto4::rotate(lastSpawn.direction, randomFloat() * 160 - 80));
  41. lastSpawn.position += lastSpawn.direction * (lastSpawn.size + 0.7f * size);
  42. lastSpawn.size = size;
  43. groundArea.push_back(Ground(lastSpawn.position, size, sf::seconds(10)));
  44. }
  45. }