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.

66 lines
1.8 KiB

  1. use glc::vector::{Angle, Vector2f, Vector4f};
  2. use space_crush_common::{
  3. components::{Orbit, Position},
  4. constants::SHIP_ORBIT_DISTANCE_MAX,
  5. };
  6. use specs::{prelude::*, ReadStorage, System, World, WriteExpect};
  7. use crate::resources::Geometry;
  8. #[derive(Default)]
  9. pub struct Orbits {}
  10. #[derive(SystemData)]
  11. pub struct OrbitsData<'a> {
  12. geometry: WriteExpect<'a, Geometry>,
  13. positions: ReadStorage<'a, Position>,
  14. orbits: ReadStorage<'a, Orbit>,
  15. }
  16. impl<'a> System<'a> for Orbits {
  17. type SystemData = OrbitsData<'a>;
  18. fn run(&mut self, data: Self::SystemData) {
  19. let OrbitsData {
  20. mut geometry,
  21. positions,
  22. orbits,
  23. } = data;
  24. gl::enable(gl::BLEND);
  25. for (position, orbit) in (&positions, &orbits).join() {
  26. gl::blend_func(gl::SRC_ALPHA, gl::ONE);
  27. gl::blend_equation(gl::FUNC_ADD);
  28. geometry.render_lines(
  29. Vector4f::new(0.5, 0.5, 0.5, 0.05),
  30. &create_circle(position.pos(), orbit.min()),
  31. );
  32. geometry.render_lines(
  33. Vector4f::new(0.5, 0.5, 0.5, 0.05),
  34. &create_circle(position.pos(), orbit.max()),
  35. );
  36. geometry.render_lines(
  37. Vector4f::new(0.5, 0.5, 0.5, 0.05),
  38. &create_circle(position.pos(), SHIP_ORBIT_DISTANCE_MAX * orbit.max()),
  39. );
  40. }
  41. gl::blend_equation(gl::FUNC_ADD);
  42. gl::disable(gl::BLEND);
  43. }
  44. }
  45. fn create_circle(p: &Vector2f, r: f32) -> Vec<Vector2f> {
  46. let mut points = Vec::new();
  47. for i in 0..=180 {
  48. points.push(Vector2f::new(
  49. p.x + r * Angle::Deg(i as f32 * 2.0).cos(),
  50. p.y + r * Angle::Deg(i as f32 * 2.0).sin(),
  51. ));
  52. }
  53. points
  54. }