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.

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