burstshot_pattern.cxx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // burstshot_pattern.cxx
  3. // danmaku
  4. //
  5. // Created by Sam Jaffe on 5/26/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include <json/json.h>
  9. #include <vector>
  10. #include "danmaku/bullet_pattern.hpp"
  11. #include "game/math/angle.hpp"
  12. #include "game/math/common.hpp"
  13. #include "game/math/math_fwd.hpp"
  14. #include "resource_factory/prototype_factory.hpp"
  15. #include "vector/vector.hpp"
  16. using namespace danmaku;
  17. using shot = std::pair<math::vec2, float>;
  18. struct burstshot : public danmaku::bullet_pattern {
  19. static std::shared_ptr<burstshot> create(danmaku::actor *,
  20. Json::Value const &);
  21. burstshot(danmaku::actor *, float shot_interval, std::vector<shot> shots);
  22. void update(float delta) override;
  23. danmaku::actor * actor;
  24. danmaku::bullet_timer shot_timer;
  25. float last_fired{std::numeric_limits<float>::lowest()};
  26. std::vector<shot> shots;
  27. };
  28. std::vector<shot> shot_vector(std::size_t count, float facing, float covered,
  29. float speed, bool clockwise) {
  30. if (covered == 360.f) { ++count; }
  31. if (clockwise) { covered = -covered; }
  32. std::vector<shot> out(count);
  33. float start = facing + (.5f * covered);
  34. float end = start - covered;
  35. if (clockwise != (end < start)) { std::swap(start, end); }
  36. float const delta = (end - start) / count;
  37. float current = start;
  38. for (std::size_t i = 0; i < count; ++i, current += delta) {
  39. out[i].first = math::rotate({{speed, 0}}, math::degree(current));
  40. out[i].second = current + 90;
  41. }
  42. return out;
  43. }
  44. burstshot::burstshot(danmaku::actor * actor, float shot_interval,
  45. std::vector<shot> shots)
  46. : actor(actor), shot_timer{shot_interval}, shots(shots) {}
  47. std::shared_ptr<burstshot> burstshot::create(danmaku::actor * actor,
  48. Json::Value const & json) {
  49. return std::make_shared<burstshot>(
  50. actor, json["timeBetweenBullets"].asFloat(),
  51. shot_vector(json["bulletsPerWave"].asUInt(),
  52. json["centerAngle"].asFloat(), json["burstAngle"].asFloat(),
  53. json["bulletSpeed"].asFloat(), json["clockwise"].asBool()));
  54. }
  55. void burstshot::update(float delta) {}
  56. namespace danmaku {
  57. BIND_BULLET_PATTERN("burstshot", &burstshot::create);
  58. }