Skip to main content

borger/handwritten/
interpolation.rs

1#[cfg(feature = "client")]
2use {
3	crate::Scope, crate::interpolation::*, crate::networked_types::primitive::usize32, crate::presentation,
4};
5
6///Classic lerp/slerp helper for various simple math primitives
7pub trait Interpolate: Copy {
8	fn interpolate(prv: Self, cur: Self, amount: f32) -> Self;
9}
10
11//trait exists to fire events when some change occurs between
12//ticks (eg. for slotmaps, adding+removing slots). prv has to
13//be an option in order to fire initial collection add/remove
14//events, otherwise no change would be detected
15#[cfg(feature = "client")]
16pub trait InterpolateTicks<Prv = Self> {
17	type InterpolationOutput;
18	fn interpolate_and_diff(
19		prv: Option<&Prv>,
20		cur: &Self,
21		amount: f32,
22		received_new_tick: bool,
23	) -> Self::InterpolationOutput;
24}
25
26#[cfg(feature = "client")]
27pub type Client = Scope<ClientOwned, ClientRemote>;
28
29#[cfg(feature = "client")]
30impl InterpolateTicks for presentation::Client {
31	type InterpolationOutput = Client;
32	fn interpolate_and_diff(
33		prv: Option<&Self>,
34		cur: &Self,
35		amount: f32,
36		received_new_tick: bool,
37	) -> Self::InterpolationOutput {
38		match cur {
39			Self::Owned(cur) => Client::Owned(InterpolateTicks::interpolate_and_diff(
40				prv.map(|prv| prv.as_owned().unwrap()),
41				cur,
42				amount,
43				received_new_tick,
44			)),
45			Self::Remote(cur) => {
46				if let Some(presentation::Client::Owned(prv)) = prv {
47					//special case: received a new client id after reconnecting
48					//to server. the previously owned client is now a stale
49					//remote client that the server hasn't timed out yet
50					Client::Remote(presentation::ClientRemote::interpolate_and_diff(
51						Some(prv),
52						cur,
53						amount,
54						received_new_tick,
55					))
56				} else {
57					Client::Remote(presentation::ClientRemote::interpolate_and_diff(
58						prv.map(|prv| prv.as_remote().unwrap()),
59						cur,
60						amount,
61						received_new_tick,
62					))
63				}
64			}
65		}
66	}
67}
68
69#[cfg(feature = "client")]
70pub struct InterpolationContext {
71	pub local_client_id: usize32,
72	pub output: InterpolationOutput,
73}