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.

128 lines
3.9 KiB

  1. use glc::{
  2. matrix::Matrix4f,
  3. misc::{BindGuard, Bindable},
  4. shader::{Program, Type, Uniform},
  5. texture::Texture,
  6. vector::Vector4f,
  7. };
  8. use space_crush_common::{
  9. components::{Player, PlayerOwned, Position, Ship, ShipType, Velocity},
  10. misc::LogResult,
  11. };
  12. use specs::{prelude::*, ReadExpect, ReadStorage, System, World};
  13. use crate::{
  14. constants::{PLAYER_COLOR_DEFAULT, UNIFORM_BUFFER_INDEX_CAMERA, UNIFORM_BUFFER_INDEX_UNIFORM},
  15. misc::WorldHelper,
  16. resources::Geometry,
  17. Error,
  18. };
  19. pub struct Ships {
  20. program: Program,
  21. texture_bomber: Texture,
  22. texture_fighter: Texture,
  23. texture_transporter: Texture,
  24. location_model: gl::GLint,
  25. location_glow_color: gl::GLint,
  26. }
  27. impl Ships {
  28. pub fn new(world: &World) -> Result<Self, Error> {
  29. let program = world.load_program(vec![
  30. (Type::Vertex, "resources/shader/ship/vert.glsl"),
  31. (Type::Fragment, "resources/shader/ship/frag.glsl"),
  32. ])?;
  33. program.uniform_block_binding("Camera", UNIFORM_BUFFER_INDEX_CAMERA)?;
  34. program.uniform_block_binding("Global", UNIFORM_BUFFER_INDEX_UNIFORM)?;
  35. let location_model = program.uniform_location("uModel")?;
  36. let location_glow_color = program.uniform_location("uGlowColor")?;
  37. program.bind();
  38. program.uniform("uTexture", Uniform::Texture(0))?;
  39. program.unbind();
  40. let texture_bomber = world.load_texture("resources/textures/ship_bomber.png")?;
  41. let texture_fighter = world.load_texture("resources/textures/ship_fighter.png")?;
  42. let texture_transporter = world.load_texture("resources/textures/ship_transporter.png")?;
  43. Ok(Self {
  44. program,
  45. texture_bomber,
  46. texture_fighter,
  47. texture_transporter,
  48. location_model,
  49. location_glow_color,
  50. })
  51. }
  52. }
  53. #[derive(SystemData)]
  54. pub struct ShipsData<'a> {
  55. geometry: ReadExpect<'a, Geometry>,
  56. position: ReadStorage<'a, Position>,
  57. velocity: ReadStorage<'a, Velocity>,
  58. ship: ReadStorage<'a, Ship>,
  59. owned: ReadStorage<'a, PlayerOwned>,
  60. player: ReadStorage<'a, Player>,
  61. }
  62. impl<'a> System<'a> for Ships {
  63. type SystemData = ShipsData<'a>;
  64. fn run(&mut self, data: Self::SystemData) {
  65. let ShipsData {
  66. geometry,
  67. position,
  68. velocity,
  69. ship,
  70. owned,
  71. player,
  72. } = data;
  73. gl::enable(gl::BLEND);
  74. gl::blend_func(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
  75. let _guard = BindGuard::new(&self.program);
  76. for (p, v, ship, owned) in (&position, &velocity, &ship, owned.maybe()).join() {
  77. let _guard = match ship.type_ {
  78. ShipType::Fighter => BindGuard::new(&self.texture_fighter),
  79. ShipType::Bomber => BindGuard::new(&self.texture_bomber),
  80. ShipType::Transporter => BindGuard::new(&self.texture_transporter),
  81. };
  82. let c = match owned.and_then(|owned| player.get(owned.owner)) {
  83. Some(pv) => &pv.color,
  84. None => &*PLAYER_COLOR_DEFAULT,
  85. };
  86. let p_x = p.pos.x;
  87. let p_y = p.pos.y;
  88. let d_x = v.dir.x;
  89. let d_y = v.dir.y;
  90. let s = p.size;
  91. let m = Matrix4f::new(
  92. Vector4f::new(-s * d_y, s * d_x, 0.0, 0.0),
  93. Vector4f::new(-s * d_x, -s * d_y, 0.0, 0.0),
  94. Vector4f::new(0.0, 0.0, s, 0.0),
  95. Vector4f::new(p_x, p_y, 0.0, 1.0),
  96. );
  97. self.program
  98. .uniform(self.location_glow_color, Uniform::Vector4f(&c))
  99. .error("Error while updating glow color");
  100. self.program
  101. .uniform(self.location_model, Uniform::Matrix4f(&m))
  102. .error("Error while updating model matrix");
  103. geometry.render_quad();
  104. }
  105. gl::disable(gl::BLEND);
  106. }
  107. }