1use glam::Vec2;
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28pub enum ControlMode {
29 Strategy,
31 Ego,
33}
34
35#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub enum InputOwner {
38 Camera,
40 Unit,
42}
43
44#[derive(Clone, Copy, Debug, PartialEq, Eq)]
46pub enum HandoffEvent {
47 Attached,
49 Released,
51}
52
53#[derive(Clone, Copy, Debug)]
55pub struct EgoConfig {
56 pub duration: f64,
59}
60
61impl Default for EgoConfig {
62 fn default() -> Self {
63 Self { duration: 1.2 }
64 }
65}
66
67fn smoothstep(t: f64) -> f64 {
68 let x = t.clamp(0.0, 1.0);
69 x * x * (3.0 - 2.0 * x)
70}
71
72pub fn fade_curve(t: f32) -> f32 {
74 smoothstep(t as f64) as f32
75}
76
77#[derive(Clone, Debug)]
85pub struct EgoHandoff {
86 cfg: EgoConfig,
87 mode: ControlMode,
88 anchor: Vec2,
90 unit: Vec2,
92 from: Vec2,
94 progress: f64,
96 to_unit: bool,
98}
99
100impl EgoHandoff {
101 pub fn new(cfg: EgoConfig, start: Vec2) -> Self {
102 Self {
103 cfg,
104 mode: ControlMode::Strategy,
105 anchor: start,
106 unit: start,
107 from: start,
108 progress: 1.0,
109 to_unit: true,
110 }
111 }
112
113 pub fn config(&self) -> &EgoConfig {
114 &self.cfg
115 }
116
117 pub fn mode(&self) -> ControlMode {
118 self.mode
119 }
120
121 pub fn is_transitioning(&self) -> bool {
123 self.progress < 1.0
124 }
125
126 pub fn heading(&self) -> ControlMode {
129 if self.to_unit {
130 ControlMode::Ego
131 } else {
132 ControlMode::Strategy
133 }
134 }
135
136 pub fn ego_blend(&self) -> f32 {
138 match (self.mode, self.is_transitioning()) {
139 (ControlMode::Ego, false) => 1.0,
140 (_, true) if self.heading() == ControlMode::Ego => fade_curve(self.progress as f32),
141 _ => 0.0,
142 }
143 }
144
145 pub fn progress(&self) -> f64 {
147 self.progress
148 }
149
150 pub fn camera(&self) -> Vec2 {
152 if self.progress >= 1.0 {
153 return match self.mode {
154 ControlMode::Strategy => self.anchor,
155 ControlMode::Ego => self.unit,
156 };
157 }
158 let target = if self.to_unit { self.unit } else { self.anchor };
159 self.from.lerp(target, smoothstep(self.progress) as f32)
160 }
161
162 pub fn input_owner(&self) -> InputOwner {
165 if !self.is_transitioning() && self.mode == ControlMode::Ego {
166 InputOwner::Unit
167 } else {
168 InputOwner::Camera
169 }
170 }
171
172 pub fn move_anchor(&mut self, delta: Vec2) {
175 self.anchor += delta;
176 }
177
178 pub fn anchor(&self) -> Vec2 {
179 self.anchor
180 }
181
182 pub fn move_unit(&mut self, delta: Vec2) {
185 self.unit += delta;
186 }
187
188 pub fn set_unit_position(&mut self, pos: Vec2) {
191 self.unit = pos;
192 }
193
194 pub fn unit_position(&self) -> Vec2 {
195 self.unit
196 }
197
198 pub fn attach(&mut self) -> bool {
202 let started =
203 self.cfg.duration > 0.0 && (self.mode != ControlMode::Ego || self.is_transitioning());
204 self.from = self.camera();
205 self.progress = if self.cfg.duration <= 0.0 { 1.0 } else { 0.0 };
206 self.to_unit = true;
207 if self.progress >= 1.0 {
208 self.mode = ControlMode::Ego;
209 }
210 started
211 }
212
213 pub fn release(&mut self) -> bool {
217 let started = self.cfg.duration > 0.0
218 && (self.mode != ControlMode::Strategy || self.is_transitioning());
219 self.anchor = self.camera();
220 self.from = self.anchor;
221 self.progress = if self.cfg.duration <= 0.0 { 1.0 } else { 0.0 };
222 self.to_unit = false;
223 if self.progress >= 1.0 {
224 self.mode = ControlMode::Strategy;
225 }
226 started
227 }
228
229 pub fn update(&mut self, dt: f64) -> Option<HandoffEvent> {
231 if self.progress >= 1.0 {
232 return None;
233 }
234 self.progress = (self.progress + dt / self.cfg.duration.max(1e-6)).min(1.0);
235 if self.progress < 1.0 {
236 return None;
237 }
238 if self.to_unit {
239 self.mode = ControlMode::Ego;
240 Some(HandoffEvent::Attached)
241 } else {
242 self.mode = ControlMode::Strategy;
243 Some(HandoffEvent::Released)
244 }
245 }
246}
247
248#[cfg(test)]
249mod tests {
250 use super::*;
251
252 fn rig() -> EgoHandoff {
253 EgoHandoff::new(EgoConfig { duration: 1.0 }, Vec2::new(0.0, 0.0))
254 }
255
256 #[test]
257 fn start_ist_strategie_mit_freier_kamera() {
258 let r = rig();
259 assert_eq!(r.mode(), ControlMode::Strategy);
260 assert_eq!(r.camera(), Vec2::new(0.0, 0.0));
261 assert_eq!(r.input_owner(), InputOwner::Camera);
262 assert!(!r.is_transitioning());
263 }
264
265 #[test]
266 fn attach_blendet_stetig_und_endet_bei_der_einheit() {
267 let mut r = rig();
268 r.set_unit_position(Vec2::new(100.0, 0.0));
269 assert!(r.attach());
270 let mut prev = r.camera();
271 let mut evs = Vec::new();
272 for _ in 0..60 {
273 let ev = r.update(1.0 / 60.0);
274 evs.extend(ev);
275 let c = r.camera();
276 assert!(
278 (c - prev).length() <= 100.0 / 30.0,
279 "Sprung von {prev} nach {c}"
280 );
281 prev = c;
282 }
283 assert_eq!(
284 evs,
285 vec![HandoffEvent::Attached],
286 "ein Abschluss, kein früheres"
287 );
288 assert_eq!(r.mode(), ControlMode::Ego);
289 assert_eq!(r.camera(), Vec2::new(100.0, 0.0));
290 assert_eq!(r.input_owner(), InputOwner::Unit);
291 }
292
293 #[test]
294 fn waehrend_der_blende_bleibt_die_eingabe_bei_der_kamera() {
295 let mut r = rig();
296 r.set_unit_position(Vec2::new(100.0, 0.0));
297 r.attach();
298 r.update(0.5);
299 assert_eq!(r.input_owner(), InputOwner::Camera, "Einheit noch gesperrt");
300 assert!(r.is_transitioning());
301 }
302
303 #[test]
304 fn release_gibt_frei_ohne_blicksprung() {
305 let mut r = rig();
306 r.set_unit_position(Vec2::new(100.0, 0.0));
307 r.attach();
308 for _ in 0..60 {
309 r.update(1.0 / 60.0);
310 }
311 assert!(r.release());
314 assert_eq!(r.camera(), Vec2::new(100.0, 0.0));
315 for _ in 0..60 {
316 r.update(1.0 / 60.0);
317 }
318 assert_eq!(r.mode(), ControlMode::Strategy);
319 assert_eq!(r.anchor(), Vec2::new(100.0, 0.0));
320 assert_eq!(r.input_owner(), InputOwner::Camera);
321 }
322
323 #[test]
324 fn dauer_null_ist_harter_schnitt() {
325 let mut r = EgoHandoff::new(EgoConfig { duration: 0.0 }, Vec2::ZERO);
326 r.set_unit_position(Vec2::new(50.0, 50.0));
327 assert!(!r.attach(), "kein Blendenstart nötig");
328 assert_eq!(r.mode(), ControlMode::Ego);
329 assert_eq!(r.camera(), Vec2::new(50.0, 50.0));
330 }
331
332 #[test]
333 fn einheit_begleiten_ohne_blende() {
334 let mut r = rig();
335 r.set_unit_position(Vec2::new(10.0, 0.0));
336 r.attach();
337 for _ in 0..60 {
338 r.update(1.0 / 60.0);
339 }
340 r.move_unit(Vec2::new(0.0, 2.0));
342 assert_eq!(r.camera(), Vec2::new(10.0, 2.0));
343 }
344
345 #[test]
346 fn umhaengen_waehrend_der_blende_startet_neu() {
347 let mut r = rig();
348 r.set_unit_position(Vec2::new(100.0, 0.0));
349 r.attach();
350 r.update(0.5);
351 r.set_unit_position(Vec2::new(100.0, 100.0));
352 let mid = r.camera();
355 assert!(r.attach(), "Zielwechsel ist ein neuer Blendenstart");
356 assert_eq!(r.camera(), mid, "neue Blende beginnt an alter Position");
357 for _ in 0..60 {
358 r.update(1.0 / 60.0);
359 }
360 assert_eq!(r.camera(), Vec2::new(100.0, 100.0));
361 }
362
363 #[test]
366 fn handoff_mitten_im_flug_ohne_szenen_neuaufbau() {
367 use crate::clipmap::ClipmapConfig;
368 use crate::partition::{PartitionConfig, WorldPartition};
369 use crate::stream::{MockSource, StreamConfig};
370 use crate::tier::TierPolicy;
371
372 let mut p = WorldPartition::new(
373 PartitionConfig {
374 clipmap: ClipmapConfig {
375 base_cell: 64.0,
376 radius: 2,
377 levels: 3,
378 },
379 stream: StreamConfig {
380 budget_bytes: 100_000,
381 max_inflight: 64,
382 },
383 tiers: TierPolicy::default(),
384 tiers_adaptive: false,
385 },
386 MockSource::new(0.0, 100),
387 );
388 let mut r = EgoHandoff::new(EgoConfig { duration: 0.5 }, Vec2::ZERO);
389 let avatar = p.spawn(Vec2::new(120.0, 0.0), Some(Vec2::ZERO));
390
391 let mut events = Vec::new();
392 let mut min_resident = u64::MAX;
393 let mut frame_of_attach = 0;
394 let dt = 1.0 / 60.0;
395 for i in 0..300 {
396 if i == 120 {
399 frame_of_attach = i;
400 r.attach();
401 }
402 if i > frame_of_attach && r.input_owner() == InputOwner::Unit {
403 r.move_unit(Vec2::new(2.0, 0.0));
404 p.set_instance_position(avatar, r.unit_position());
406 } else {
407 r.move_anchor(Vec2::new(2.0, 0.0));
408 }
409 let cam = r.camera();
410 if let Some(ev) = r.update(dt) {
411 events.push(ev);
412 }
413 p.update(cam, dt as f32);
414 if i > 5 {
417 min_resident = min_resident.min(p.streamer().resident_bytes());
418 }
419 }
420 assert_eq!(r.mode(), ControlMode::Ego);
421 assert_eq!(events, vec![HandoffEvent::Attached]);
422 assert!(min_resident > 0, "Zellen blieben durchgehend resident");
423 assert_eq!(p.tier_of(avatar), Some(crate::tier::Tier::T0));
425 }
426
427 #[test]
428 fn folge_ist_deterministisch() {
429 fn run() -> Vec<(Vec2, InputOwner)> {
430 let mut r = rig();
431 r.set_unit_position(Vec2::new(100.0, 0.0));
432 let mut log = Vec::new();
433 r.attach();
434 for _ in 0..120 {
435 r.update(1.0 / 60.0);
436 log.push((r.camera(), r.input_owner()));
437 }
438 r.release();
439 for _ in 0..120 {
440 r.update(1.0 / 60.0);
441 log.push((r.camera(), r.input_owner()));
442 }
443 log
444 }
445 let a = run();
446 let b = run();
447 assert_eq!(a.len(), b.len());
448 for (x, y) in a.iter().zip(&b) {
449 assert_eq!(x.0.x.to_bits(), y.0.x.to_bits());
450 assert_eq!(x.1, y.1);
451 }
452 }
453}