euv_core/game/camera/
impl.rs1use super::*;
2
3impl Default for Camera2D {
5 fn default() -> Self {
11 Self {
12 position: Point::new(0.0, 0.0),
13 zoom: 1.0,
14 trauma: 0.0,
15 max_shake: 14.0,
16 shake_time: 0.0,
17 world_bounds: None,
18 }
19 }
20}
21
22impl Camera2D {
24 #[must_use]
35 pub fn at(center: Point, zoom: f32) -> Self {
36 Self {
37 position: center,
38 zoom: zoom.clamp(0.25, 4.0),
39 ..Self::default()
40 }
41 }
42
43 pub fn add_trauma(&mut self, amount: f32) {
50 let next: f32 = (self.get_trauma() + amount.max(0.0)).clamp(0.0, 1.0);
51 self.set_trauma(next);
52 }
53
54 pub fn follow(&mut self, target: Point, responsiveness: f32, dt_secs: f32, viewport: Size) {
64 let alpha: f32 =
65 (1.0 - (-responsiveness.max(0.1) * dt_secs.max(0.0)).exp()).clamp(0.0, 1.0);
66 let cur: Point = self.get_position();
67 let mut nx: f32 = cur.get_x() + (target.get_x() - cur.get_x()) * alpha;
68 let mut ny: f32 = cur.get_y() + (target.get_y() - cur.get_y()) * alpha;
69 let Self { world_bounds, .. } = *self;
70 if let Some(bounds) = world_bounds {
71 let half_w: f32 = (viewport.get_width() * 0.5) / self.get_zoom().max(0.25);
72 let half_h: f32 = (viewport.get_height() * 0.5) / self.get_zoom().max(0.25);
73 let min_x: f32 = bounds.get_x() + half_w;
74 let max_x: f32 = bounds.right() - half_w;
75 let min_y: f32 = bounds.get_y() + half_h;
76 let max_y: f32 = bounds.bottom() - half_h;
77 nx = if min_x <= max_x {
78 nx.clamp(min_x, max_x)
79 } else {
80 bounds.get_x() + bounds.get_width() * 0.5
81 };
82 ny = if min_y <= max_y {
83 ny.clamp(min_y, max_y)
84 } else {
85 bounds.get_y() + bounds.get_height() * 0.5
86 };
87 }
88 self.set_position(Point::new(nx, ny));
89 let next_trauma: f32 = (self.get_trauma() - dt_secs * 1.8).max(0.0);
90 self.set_trauma(next_trauma);
91 self.set_shake_time(self.get_shake_time() + dt_secs * 38.0);
92 }
93
94 #[must_use]
104 pub fn shake_offset(&self) -> Point {
105 let t: f32 = self.get_trauma();
106 if t <= 0.001 {
107 return Point::new(0.0, 0.0);
108 }
109 let mag: f32 = t * t * self.get_max_shake();
110 let phase: f32 = self.get_shake_time();
111 Point::new((phase * 1.7).sin() * mag, (phase * 2.3).cos() * mag)
112 }
113
114 #[must_use]
126 pub fn world_to_screen(&self, world: Point, viewport: Size) -> Point {
127 let zoom: f32 = self.get_zoom().max(0.25);
128 let shake: Point = self.shake_offset();
129 let cam: Point = self.get_position();
130 Point::new(
131 (world.get_x() - (cam.get_x() + shake.get_x())) * zoom + viewport.get_width() * 0.5,
132 (world.get_y() - (cam.get_y() + shake.get_y())) * zoom + viewport.get_height() * 0.5,
133 )
134 }
135
136 #[must_use]
148 pub fn screen_to_world(&self, screen: Point, viewport: Size) -> Point {
149 let zoom: f32 = self.get_zoom().max(0.25);
150 let cam: Point = self.get_position();
151 Point::new(
152 (screen.get_x() - viewport.get_width() * 0.5) / zoom + cam.get_x(),
153 (screen.get_y() - viewport.get_height() * 0.5) / zoom + cam.get_y(),
154 )
155 }
156
157 #[must_use]
168 pub fn visible_world_rect(&self, viewport: Size) -> Rect {
169 let zoom: f32 = self.get_zoom().max(0.25);
170 let w: f32 = viewport.get_width() / zoom;
171 let h: f32 = viewport.get_height() / zoom;
172 let cam: Point = self.get_position();
173 let pad: f32 = self.get_max_shake();
174 Rect::new(
175 cam.get_x() - w * 0.5 - pad,
176 cam.get_y() - h * 0.5 - pad,
177 w + pad * 2.0,
178 h + pad * 2.0,
179 )
180 }
181}