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.

84 lines
2.2 KiB

  1. use space_crush_common::{
  2. components::{Fleet, Orbit, Position},
  3. continue_if_none,
  4. misc::LogResult,
  5. };
  6. use specs::{prelude::*, ReadExpect, ReadStorage, System, World};
  7. use crate::{
  8. misc::{HorizontalAlign, Text, TextManager, VerticalAlign},
  9. resources::{Camera, GameState},
  10. Error,
  11. };
  12. pub struct Fleets {
  13. text: Text,
  14. }
  15. #[derive(SystemData)]
  16. pub struct FleetData<'a> {
  17. game_state: ReadExpect<'a, GameState>,
  18. camera: ReadExpect<'a, Camera>,
  19. positions: ReadStorage<'a, Position>,
  20. orbits: ReadStorage<'a, Orbit>,
  21. fleets: ReadStorage<'a, Fleet>,
  22. }
  23. impl Fleets {
  24. pub fn new(manager: &TextManager) -> Result<Self, Error> {
  25. let cache = manager.create_cache()?;
  26. let text = cache
  27. .new_text()
  28. .scale(20.0)
  29. .font("resources/fonts/DroidSansMono.ttf")
  30. .color(0.7, 0.7, 0.7, 1.0)
  31. .vert_align(VerticalAlign::Center)
  32. .horz_align(HorizontalAlign::Center)
  33. .text("")
  34. .build()?;
  35. Ok(Self { text })
  36. }
  37. }
  38. impl<'a> System<'a> for Fleets {
  39. type SystemData = FleetData<'a>;
  40. fn run(&mut self, data: Self::SystemData) {
  41. let FleetData {
  42. game_state,
  43. camera,
  44. positions,
  45. orbits,
  46. fleets,
  47. } = data;
  48. let player_index = game_state.player_index();
  49. gl::enable(gl::BLEND);
  50. for (position, orbit) in (&positions, &orbits).join() {
  51. let fleet_id = continue_if_none!(orbit.fleet(player_index));
  52. let fleet = continue_if_none!(fleets.get(fleet_id));
  53. gl::blend_func(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
  54. gl::blend_equation(gl::FUNC_SUBTRACT);
  55. self.text
  56. .update(
  57. 0,
  58. format!(
  59. "F:{}\nB:{}\nT:{}",
  60. fleet.count().fighter,
  61. fleet.count().bomber,
  62. fleet.count().transporter
  63. ),
  64. )
  65. .panic("Unable to update text")
  66. .render_offset(&camera.world_to_window(*position.pos()));
  67. }
  68. gl::blend_equation(gl::FUNC_ADD);
  69. gl::disable(gl::BLEND);
  70. }
  71. }