1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::sync::{Arc, RwLock};
use amethyst_core::{
ecs::{Component, DenseVecStorage, FlaggedStorage},
math::Isometry3,
};
use crate::PtReal;
macro_rules! define_opaque_object {
($what:ident, $gc_name:ident) => {
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum $what {
#[allow(missing_docs)]
U32(u32),
#[allow(missing_docs)]
U64(u64),
#[allow(missing_docs)]
U32U32(u32, u32),
#[allow(missing_docs)]
U64U64(u64, u64),
#[allow(missing_docs)]
UsizeU32(usize, u32),
#[allow(missing_docs)]
UsizeU64(usize, u64),
}
impl $what {
#[allow(missing_docs)]
pub unsafe fn new_u32(a: u32) -> Self {
$what::U32(a)
}
#[allow(missing_docs)]
pub unsafe fn new_u64(a: u64) -> Self {
$what::U64(a)
}
#[allow(missing_docs)]
pub unsafe fn new_u32u32(a: u32, b: u32) -> Self {
$what::U32U32(a, b)
}
#[allow(missing_docs)]
pub unsafe fn new_u64u64(a: u64, b: u64) -> Self {
$what::U64U64(a, b)
}
#[allow(missing_docs)]
pub unsafe fn new_usizeu32(a: usize, b: u32) -> Self {
$what::UsizeU32(a, b)
}
#[allow(missing_docs)]
pub unsafe fn new_usizeu64(a: usize, b: u64) -> Self {
$what::UsizeU64(a, b)
}
}
impl PhysicsTag for $what {
fn request_resource_removal(&mut self, gc: &mut PhysicsGarbageCollector) {
gc.$gc_name.push(*self);
}
}
};
}
define_opaque_object!(PhysicsRigidBodyTag, bodies);
define_opaque_object!(PhysicsAreaTag, areas);
define_opaque_object!(PhysicsShapeTag, shapes);
define_opaque_object!(PhysicsJointTag, joints);
pub(crate) type PhysicsSetupStorages<'a> = (
amethyst_core::ecs::ReadStorage<'a, PhysicsHandle<PhysicsRigidBodyTag>>,
amethyst_core::ecs::ReadStorage<'a, PhysicsHandle<PhysicsAreaTag>>,
amethyst_core::ecs::ReadStorage<'a, PhysicsHandle<PhysicsShapeTag>>,
amethyst_core::ecs::ReadStorage<'a, PhysicsHandle<PhysicsJointTag>>,
);
pub trait PhysicsTag: Copy + std::fmt::Debug + Sync + Send + Sized + 'static {
fn request_resource_removal(&mut self, gc: &mut PhysicsGarbageCollector);
}
pub struct PhysicsHandle<T: PhysicsTag> {
tag_container: Arc<PhysicsTagContainer<T>>,
}
impl<T: PhysicsTag> PhysicsHandle<T> {
pub fn new(tag: T, garbage_collector: Arc<RwLock<PhysicsGarbageCollector>>) -> Self {
PhysicsHandle {
tag_container: Arc::new(PhysicsTagContainer {
tag,
garbage_collector,
}),
}
}
pub fn get(&self) -> T {
self.tag_container.tag
}
}
impl<T: PhysicsTag> std::fmt::Debug for PhysicsHandle<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"PhysicsHandle{{\n tag = {:?}\n owner = {}\n weak = {}\n}};",
self.get(),
Arc::strong_count(&self.tag_container),
Arc::weak_count(&self.tag_container)
)
}
}
impl<T: PhysicsTag> Clone for PhysicsHandle<T> {
fn clone(&self) -> Self {
PhysicsHandle {
tag_container: self.tag_container.clone(),
}
}
}
impl<T: PhysicsTag> Component for PhysicsHandle<T> {
type Storage = FlaggedStorage<PhysicsHandle<T>, DenseVecStorage<PhysicsHandle<T>>>;
}
struct PhysicsTagContainer<T: PhysicsTag> {
tag: T,
garbage_collector: Arc<RwLock<PhysicsGarbageCollector>>,
}
impl<T: PhysicsTag> std::ops::Drop for PhysicsTagContainer<T> {
fn drop(&mut self) {
let mut gc = self.garbage_collector.write().unwrap();
self.tag.request_resource_removal(&mut gc);
}
}
#[derive(Debug)]
pub struct PhysicsGarbageCollector {
pub bodies: Vec<PhysicsRigidBodyTag>,
pub areas: Vec<PhysicsAreaTag>,
pub shapes: Vec<PhysicsShapeTag>,
pub joints: Vec<PhysicsJointTag>,
}
impl Default for PhysicsGarbageCollector {
fn default() -> Self {
PhysicsGarbageCollector {
bodies: Vec::new(),
areas: Vec::new(),
shapes: Vec::new(),
joints: Vec::new(),
}
}
}
#[allow(missing_debug_implementations)]
pub struct PhysicsAttachment<N: PtReal> {
pub(crate) cache_world_transform: Isometry3<N>,
}
impl<N: PtReal> Component for PhysicsAttachment<N> {
type Storage = DenseVecStorage<Self>;
}
impl<N: PtReal> Default for PhysicsAttachment<N> {
fn default() -> Self {
PhysicsAttachment {
cache_world_transform: Isometry3::identity(),
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct CollisionGroup(u8);
impl Default for CollisionGroup {
fn default() -> Self {
CollisionGroup(1u8)
}
}
impl CollisionGroup {
pub fn new(group: u8) -> Self {
assert!(group < 29);
CollisionGroup(group)
}
pub fn get(self) -> u8 {
self.0
}
}