author | Wuzzy <Wuzzy2@mail.ru> |
Mon, 08 Jun 2020 23:37:38 +0200 | |
changeset 15613 | d9c62f196fe0 |
parent 15393 | 0ef770a40e75 |
child 15780 | f4b563a9ac5e |
permissions | -rw-r--r-- |
15380 | 1 |
use crate::{ |
2 |
common::{GearId, Millis}, |
|
3 |
data::GearDataManager, |
|
4 |
}; |
|
15120 | 5 |
use fpnum::*; |
6 |
||
7 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
|
15380 | 8 |
#[repr(transparent)] |
9 |
pub struct PositionData(pub FPPoint); |
|
15120 | 10 |
|
15380 | 11 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
12 |
#[repr(transparent)] |
|
13 |
pub struct VelocityData(pub FPPoint); |
|
15120 | 14 |
|
15393 | 15 |
pub struct AffectedByWind; |
16 |
||
15120 | 17 |
pub struct PositionUpdates { |
18 |
pub gear_ids: Vec<GearId>, |
|
15260 | 19 |
pub shifts: Vec<(FPPoint, FPPoint)>, |
15120 | 20 |
} |
21 |
||
22 |
impl PositionUpdates { |
|
23 |
pub fn new(capacity: usize) -> Self { |
|
24 |
Self { |
|
25 |
gear_ids: Vec::with_capacity(capacity), |
|
15260 | 26 |
shifts: Vec::with_capacity(capacity), |
15120 | 27 |
} |
28 |
} |
|
29 |
||
15260 | 30 |
pub fn push(&mut self, gear_id: GearId, old_position: &FPPoint, new_position: &FPPoint) { |
15120 | 31 |
self.gear_ids.push(gear_id); |
15260 | 32 |
self.shifts.push((*old_position, *new_position)); |
15120 | 33 |
} |
15261 | 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 |
} |
|
15266
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
42 |
|
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
43 |
pub fn clear(&mut self) { |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
44 |
self.gear_ids.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
45 |
self.shifts.clear(); |
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
46 |
} |
15120 | 47 |
} |
48 |
||
15381 | 49 |
pub struct PhysicsProcessor { |
50 |
gravity: FPNum, |
|
15393 | 51 |
wind: FPNum, |
15381 | 52 |
position_updates: PositionUpdates, |
53 |
} |
|
54 |
||
15120 | 55 |
impl PhysicsProcessor { |
15380 | 56 |
pub fn register_components(data: &mut GearDataManager) { |
57 |
data.register::<PositionData>(); |
|
58 |
data.register::<VelocityData>(); |
|
15393 | 59 |
data.register::<AffectedByWind>(); |
15380 | 60 |
} |
61 |
||
15120 | 62 |
pub fn new() -> Self { |
15270 | 63 |
Self { |
15383
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
64 |
gravity: fp!(1 / 10), |
15393 | 65 |
wind: fp!(0), |
15380 | 66 |
position_updates: PositionUpdates::new(64), |
15120 | 67 |
} |
68 |
} |
|
69 |
||
15383
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
70 |
pub fn process_single_tick(&mut self, data: &mut GearDataManager) -> &PositionUpdates { |
15393 | 71 |
let gravity = FPPoint::unit_y() * self.gravity; |
72 |
let wind = FPPoint::unit_x() * self.wind; |
|
73 |
||
15383
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
74 |
self.position_updates.clear(); |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
75 |
|
15393 | 76 |
data.iter() |
77 |
.with_tags::<&AffectedByWind>() |
|
78 |
.run(|(vel,): (&mut VelocityData,)| { |
|
79 |
vel.0 += wind; |
|
80 |
}); |
|
81 |
||
15392 | 82 |
data.iter().run_id( |
15383
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
83 |
|gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| { |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
84 |
let old_pos = pos.0; |
15393 | 85 |
vel.0 += gravity; |
15383
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
86 |
pos.0 += vel.0; |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
87 |
self.position_updates.push(gear_id, &old_pos, &pos.0) |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
88 |
}, |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
89 |
); |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
90 |
|
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
91 |
&self.position_updates |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
92 |
} |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
93 |
|
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
94 |
pub fn process_multiple_ticks( |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
95 |
&mut self, |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
96 |
data: &mut GearDataManager, |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
97 |
time_step: Millis, |
701ad89a9f2a
avoid time multiplication when not skipping ticks
alfadur
parents:
15382
diff
changeset
|
98 |
) -> &PositionUpdates { |
15275 | 99 |
let fp_step = time_step.to_fixed(); |
15393 | 100 |
let gravity = FPPoint::unit_y() * (self.gravity * fp_step); |
101 |
let wind = FPPoint::unit_x() * (self.wind * fp_step); |
|
102 |
||
15266
b58f98bbc120
clear intermediate result structures between iterations
alfadur
parents:
15261
diff
changeset
|
103 |
self.position_updates.clear(); |
15380 | 104 |
|
15393 | 105 |
data.iter() |
106 |
.with_tags::<&AffectedByWind>() |
|
107 |
.run(|(vel,): (&mut VelocityData,)| { |
|
108 |
vel.0 += wind; |
|
109 |
}); |
|
110 |
||
15392 | 111 |
data.iter().run_id( |
15380 | 112 |
|gear_id, (pos, vel): (&mut PositionData, &mut VelocityData)| { |
15382 | 113 |
let old_pos = pos.0; |
15393 | 114 |
vel.0 += gravity; |
15382 | 115 |
pos.0 += vel.0 * fp_step; |
116 |
self.position_updates.push(gear_id, &old_pos, &pos.0) |
|
15380 | 117 |
}, |
118 |
); |
|
119 |
||
15120 | 120 |
&self.position_updates |
121 |
} |
|
122 |
} |