author | unC0Rr |
Fri, 06 Oct 2023 20:34:51 +0200 | |
changeset 16010 | 5ba4d3a0c3eb |
parent 15850 | 44b49f255e31 |
permissions | -rw-r--r-- |
15401 | 1 |
use crate::{ |
2 |
common::{GearId, Millis}, |
|
3 |
data::GearDataManager, |
|
4 |
}; |
|
15141 | 5 |
use fpnum::*; |
6 |
||
7 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
15401 | 8 |
#[repr(transparent)] |
9 |
pub struct PositionData(pub FPPoint); |
|
15141 | 10 |
|
15401 | 11 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
12 |
#[repr(transparent)] |
|
13 |
pub struct VelocityData(pub FPPoint); |
|
15141 | 14 |
|
15414 | 15 |
pub struct AffectedByWind; |
16 |
||
15141 | 17 |
pub struct PositionUpdates { |
18 |
pub gear_ids: Vec<GearId>, |
|
15281 | 19 |
pub shifts: Vec<(FPPoint, FPPoint)>, |
15141 | 20 |
} |
21 |
||
22 |
impl PositionUpdates { |
|
23 |
pub fn new(capacity: usize) -> Self { |
|
24 |
Self { |
|
25 |
gear_ids: Vec::with_capacity(capacity), |
|
15281 | 26 |
shifts: Vec::with_capacity(capacity), |
15141 | 27 |
} |
28 |
} |
|
29 |
||
15281 | 30 |
pub fn push(&mut self, gear_id: GearId, old_position: &FPPoint, new_position: &FPPoint) { |
15141 | 31 |
self.gear_ids.push(gear_id); |
15281 | 32 |
self.shifts.push((*old_position, *new_position)); |
15141 | 33 |
} |
15282 | 34 |
|
35 |
pub fn iter(&self) -> impl Iterator<Item = (GearId, &FPPoint, &FPPoint)> { |
|
36 |
self.gear_ids |
|
37 |
.iter() |
|
38 |
.cloned() |
|
39 |
.zip(self.shifts.iter()) |
|
40 |
.map(|(id, (from, to))| (id, from, to)) |
|
41 |
} |
|
15287
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
42 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
43 |
pub fn clear(&mut self) { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
44 |
self.gear_ids.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
45 |
self.shifts.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
46 |
} |
15141 | 47 |
} |
48 |
||
15402 | 49 |
pub struct PhysicsProcessor { |
50 |
gravity: FPNum, |
|
15414 | 51 |
wind: FPNum, |
15402 | 52 |
position_updates: PositionUpdates, |
53 |
} |
|
54 |
||
15141 | 55 |
impl PhysicsProcessor { |
15401 | 56 |
pub fn register_components(data: &mut GearDataManager) { |
57 |
data.register::<PositionData>(); |
|
58 |
data.register::<VelocityData>(); |
|
15414 | 59 |
data.register::<AffectedByWind>(); |
15401 | 60 |
} |
61 |
||
15141 | 62 |
pub fn new() -> Self { |
15291 | 63 |
Self { |
15404
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15403
diff
changeset
|
64 |
gravity: fp!(1 / 10), |
15414 | 65 |
wind: fp!(0), |
15401 | 66 |
position_updates: PositionUpdates::new(64), |
15141 | 67 |
} |
68 |
} |
|
69 |
||
15850 | 70 |
pub fn process(&mut self, data: &mut GearDataManager, time_step: Millis) -> &PositionUpdates { |
15802 | 71 |
if time_step == Millis::new(1) { |
72 |
self.process_impl::<true>(data, time_step) |
|
73 |
} else { |
|
74 |
self.process_impl::<false>(data, time_step) |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
fn process_impl<const SINGLE_TICK: bool>( |
|
79 |
&mut self, |
|
80 |
data: &mut GearDataManager, |
|
81 |
time_step: Millis, |
|
82 |
) -> &PositionUpdates { |
|
83 |
let fp_step = if SINGLE_TICK { |
|
84 |
fp!(1) |
|
85 |
} else { |
|
86 |
time_step.to_fixed() |
|
87 |
}; |
|
15414 | 88 |
let gravity = FPPoint::unit_y() * (self.gravity * fp_step); |
89 |
let wind = FPPoint::unit_x() * (self.wind * fp_step); |
|
90 |
||
15287
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15282
diff
changeset
|
91 |
self.position_updates.clear(); |
15401 | 92 |
|
15414 | 93 |
data.iter() |
94 |
.with_tags::<&AffectedByWind>() |
|
95 |
.run(|(vel,): (&mut VelocityData,)| { |
|
96 |
vel.0 += wind; |
|
97 |
}); |
|
98 |
||
15413 | 99 |
data.iter().run_id( |
15401 | 100 |
|gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| { |
15403 | 101 |
let old_pos = pos.0; |
15414 | 102 |
vel.0 += gravity; |
15802 | 103 |
pos.0 += if SINGLE_TICK { vel.0 } else { vel.0 * fp_step }; |
15403 | 104 |
self.position_updates.push(gear_id, &old_pos, &pos.0) |
15401 | 105 |
}, |
106 |
); |
|
107 |
||
15141 | 108 |
&self.position_updates |
109 |
} |
|
110 |
} |