/ src / gfx.rs
gfx.rs
  1  //! Graphics helpers.
  2  
  3  use super::*;
  4  
  5  pub mod math {
  6      pub struct Vec2 {
  7          pub x: f32,
  8          pub y: f32,
  9      }
 10  
 11      impl Vec2 {
 12          pub fn new(x: f32, y: f32) -> Self {
 13              Self { x, y }
 14          }
 15  
 16          pub fn scaled(&self, scale: f32) -> Self {
 17              Self {
 18                  x: self.x * scale,
 19                  y: self.y * scale,
 20              }
 21          }
 22  
 23          pub fn added(&self, other: &Self) -> Self {
 24              Self {
 25                  x: self.x + other.x,
 26                  y: self.y + other.y,
 27              }
 28          }
 29      }
 30  }
 31  
 32  pub fn wave_pos(x: f32, y: f32, time: f32) -> math::Vec2 {
 33      math::Vec2::new(x, y).added(&wave_offset(x, y, time))
 34  }
 35  
 36  pub fn wave_offset(x: f32, y: f32, time: f32) -> math::Vec2 {
 37      math::Vec2::new(
 38          (time / 2.0 + ((x + y) / 200.0)).sin() * 6.0,
 39          (time / 2.0 + (3.14 / 4.0) + ((x + y) / 200.0)).sin() * 7.0,
 40      )
 41      // x + ((stats.playtime).sin() * 6.0)
 42      //     + (((NUM_ROWS - i as u32) as f32 + self.y_offset_percent) * 2.0).sin()
 43      //         * 4.0
 44      //     + (((j as u32) as f32) * 1.0).sin() * 4.0,
 45      // y,
 46  }
 47  
 48  pub fn ocean_alpha(x: f32, y: f32) -> f32 {
 49      match x {
 50          ..-50.0 => return 0.0,
 51          -50.0..0.0 => return (50.0 + x) / 50.0,
 52          0.0..600.0 => {}
 53          600.0..650.0 => return (650.0 - x) / 50.0,
 54          650.0.. => return 0.0,
 55          _ => return 0.0,
 56      };
 57  
 58      match y {
 59          0.0..50.0 => return (y) / 50.0,
 60          0.0..1000.0 => {}
 61          1000.0..1050.0 => return (1050.0 - y) / 50.0,
 62          1050.0.. => return 0.0,
 63          _ => return 0.0,
 64      };
 65  
 66      1.0
 67  }
 68  
 69  pub fn draw_ellipse_lines_custom(
 70      x: f32,
 71      y: f32,
 72      w: f32,
 73      h: f32,
 74      rotation: f32,
 75      thickness: f32,
 76      color: Color,
 77  ) {
 78      let sides = 32;
 79  
 80      let rot = rotation.to_radians();
 81      let sr = rot.sin();
 82      let cr = rot.cos();
 83      for i in 0..sides {
 84          let rx = (i as f32 / sides as f32 * std::f32::consts::PI * 2.).cos();
 85          let ry = (i as f32 / sides as f32 * std::f32::consts::PI * 2.).sin();
 86          let px = w * rx;
 87          let py = h * ry;
 88          let rotated_x = px * cr - py * sr;
 89          let rotated_y = py * cr + px * sr;
 90  
 91          let p0 = vec2(x + rotated_x, y + rotated_y);
 92  
 93          let rx = ((i + 1) as f32 / sides as f32 * std::f32::consts::PI * 2.).cos();
 94          let ry = ((i + 1) as f32 / sides as f32 * std::f32::consts::PI * 2.).sin();
 95          let px = w * rx;
 96          let py = h * ry;
 97          let rotated_x = px * cr - py * sr;
 98          let rotated_y = py * cr + px * sr;
 99  
100          let p1 = vec2(x + rotated_x, y + rotated_y);
101  
102          draw_line(p0.x, p0.y, p1.x, p1.y, thickness, color);
103      }
104  }