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.

61 rivejä
1.7 KiB

  1. use glc::vector::Vector4f;
  2. use space_crush_common::{
  3. components::{Position, Ship, ShipObstacle, Velocity},
  4. continue_if_none,
  5. };
  6. use specs::{prelude::*, ReadStorage, System, World, WriteExpect};
  7. use crate::resources::Geometry;
  8. #[derive(Default)]
  9. pub struct Ships;
  10. #[derive(SystemData)]
  11. pub struct ShipsData<'a> {
  12. geometry: WriteExpect<'a, Geometry>,
  13. positions: ReadStorage<'a, Position>,
  14. velocities: ReadStorage<'a, Velocity>,
  15. ships: ReadStorage<'a, Ship>,
  16. }
  17. impl<'a> System<'a> for Ships {
  18. type SystemData = ShipsData<'a>;
  19. fn run(&mut self, data: Self::SystemData) {
  20. let ShipsData {
  21. mut geometry,
  22. positions,
  23. velocities,
  24. ships,
  25. } = data;
  26. gl::enable(gl::BLEND);
  27. gl::blend_func(gl::SRC_ALPHA, gl::ONE);
  28. for (position, velocity, ship) in (&positions, &velocities, &ships).join() {
  29. let ship_pos = position.pos;
  30. geometry.render_lines(
  31. Vector4f::new(0.0, 0.0, 1.0, 0.2),
  32. &[ship_pos, ship_pos + velocity.dir * velocity.speed],
  33. );
  34. geometry.render_lines(
  35. Vector4f::new(1.0, 0.0, 0.0, 0.2),
  36. &[ship_pos, ship_pos + ship.target_dir * 100.0],
  37. );
  38. geometry.render_lines(
  39. Vector4f::new(1.0, 1.0, 1.0, 0.2),
  40. &[ship_pos, ship.target_pos],
  41. );
  42. if let ShipObstacle::Known(obstacle) = ship.obstacle {
  43. let obstacle_pos = continue_if_none!(positions.get(obstacle)).pos;
  44. geometry.render_lines(Vector4f::new(0.0, 1.0, 0.0, 0.2), &[ship_pos, obstacle_pos]);
  45. }
  46. }
  47. gl::disable(gl::BLEND);
  48. }
  49. }