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
//! Multiple network-able types for transforms, all containing different combinations of fields.
//!
//! Types in this file:
//! - [NetTransform]
//! - [NetTransformTR]
//! - [NetTransformT]
//! - [NetTransform2d]
//! - [NetTransform2dTR]
//! - [NetTransform2dT]

use bevy::math::Vec3Swizzles;
use bevy::prelude::*;
use serde::{Deserialize, Serialize};

/// The network-able version of [Transform].
///
/// Contains all fields.
///
/// Several different versions with different fields are available:
/// - [NetTransform]
/// - [NetTransformTR]
/// - [NetTransformT]
/// - [NetTransform2d]
/// - [NetTransform2dTR]
/// - [NetTransform2dT]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct NetTransform {
    /// Position of the entity. In 2d, the last value of the `Vec3` is used for z-ordering.
    pub translation: Vec3,
    /// Rotation of the entity.
    pub rotation: Quat,
    /// Scale of the entity.
    pub scale: Vec3,
}

impl From<Transform> for NetTransform {
    fn from(o: Transform) -> Self {
        NetTransform {
            translation: o.translation,
            rotation: o.rotation,
            scale: o.scale,
        }
    }
}

impl From<NetTransform> for Transform {
    fn from(o: NetTransform) -> Self {
        Transform {
            translation: o.translation,
            rotation: o.rotation,
            scale: o.scale,
        }
    }
}

/// The network-able version of [Transform].
///
/// Contains translation and rotation.
///
/// Only works if scale is always `Vec3::ONE`.
///
/// Several different versions with different fields are available:
/// - [NetTransform]
/// - [NetTransformTR]
/// - [NetTransformT]
/// - [NetTransform2d]
/// - [NetTransform2dTR]
/// - [NetTransform2dT]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct NetTransformTR {
    /// Position of the entity. In 2d, the last value of the `Vec3` is used for z-ordering.
    pub translation: Vec3,
    /// Rotation of the entity.
    pub rotation: Quat,
}

impl From<Transform> for NetTransformTR {
    fn from(o: Transform) -> Self {
        NetTransformTR {
            translation: o.translation,
            rotation: o.rotation,
        }
    }
}

impl From<NetTransformTR> for Transform {
    fn from(o: NetTransformTR) -> Self {
        Transform {
            translation: o.translation,
            rotation: o.rotation,
            ..default()
        }
    }
}

/// The network-able version of [Transform].
///
/// Contains only translation.
///
/// Only works if rotation always is `Quat::identity()` and scale is always `Vec3::ONE`.
///
/// Several different versions with different fields are available:
/// - [NetTransform]
/// - [NetTransformTR]
/// - [NetTransformT]
/// - [NetTransform2d]
/// - [NetTransform2dTR]
/// - [NetTransform2dT]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct NetTransformT {
    /// Position of the entity. In 2d, the last value of the `Vec3` is used for z-ordering.
    pub translation: Vec3,
}

impl From<Transform> for NetTransformT {
    fn from(o: Transform) -> Self {
        NetTransformT {
            translation: o.translation,
        }
    }
}

impl From<NetTransformT> for Transform {
    fn from(o: NetTransformT) -> Self {
        Transform {
            translation: o.translation,
            ..default()
        }
    }
}

/// The network-able version of [Transform].
///
/// Contains all fields, using `Vec2`s instead of `Vec3`s.
///
/// Only works if translation.z is always `0`, and scale.z is always `1`.
///
/// Several different versions with different fields are available:
/// - [NetTransform]
/// - [NetTransformTR]
/// - [NetTransformT]
/// - [NetTransform2d]
/// - [NetTransform2dTR]
/// - [NetTransform2dT]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct NetTransform2d {
    /// Position of the entity. In 2d, the last value of the `Vec3` is used for z-ordering.
    pub translation: Vec2,
    /// Rotation of the entity.
    pub rotation: Quat,
    /// Scale of the entity.
    pub scale: Vec2,
}

impl From<Transform> for NetTransform2d {
    fn from(o: Transform) -> Self {
        NetTransform2d {
            translation: o.translation.xy(),
            rotation: o.rotation,
            scale: o.scale.xy(),
        }
    }
}

impl From<NetTransform2d> for Transform {
    fn from(o: NetTransform2d) -> Self {
        Transform {
            translation: o.translation.extend(0.0),
            rotation: o.rotation,
            scale: o.scale.extend(0.0),
        }
    }
}

/// The network-able version of [Transform].
///
/// Contains only translation and rotation, using `Vec2`s instead of `Vec3`s.
///
/// Only works if translation.z is always `0`, and scale is `Vec3::ONE`.
///
/// Several different versions with different fields are available:
/// - [NetTransform]
/// - [NetTransformTR]
/// - [NetTransformT]
/// - [NetTransform2d]
/// - [NetTransform2dTR]
/// - [NetTransform2dT]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct NetTransform2dTR {
    /// Position of the entity. In 2d, the last value of the `Vec3` is used for z-ordering.
    pub translation: Vec2,
    /// Rotation of the entity.
    pub rotation: Quat,
}

impl From<Transform> for NetTransform2dTR {
    fn from(o: Transform) -> Self {
        NetTransform2dTR {
            translation: o.translation.xy(),
            rotation: o.rotation,
        }
    }
}

impl From<NetTransform2dTR> for Transform {
    fn from(o: NetTransform2dTR) -> Self {
        Transform {
            translation: o.translation.extend(0.0),
            rotation: o.rotation,
            ..default()
        }
    }
}

/// The network-able version of [Transform].
///
/// Contains only translation, using `Vec2`s instead of `Vec3`s.
///
/// Only works if rotation always is `Quat::identity()`, translation.z is always `0`,
/// and scale is `Vec3::ONE`.
///
/// Several different versions with different fields are available:
/// - [NetTransform]
/// - [NetTransformTR]
/// - [NetTransformT]
/// - [NetTransform2d]
/// - [NetTransform2dTR]
/// - [NetTransform2dT]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct NetTransform2dT {
    /// Position of the entity. In 2d, the last value of the `Vec3` is used for z-ordering.
    pub translation: Vec2,
}

impl From<Transform> for NetTransform2dT {
    fn from(o: Transform) -> Self {
        NetTransform2dT {
            translation: o.translation.xy(),
        }
    }
}

impl From<NetTransform2dT> for Transform {
    fn from(o: NetTransform2dT) -> Self {
        Transform {
            translation: o.translation.extend(0.0),
            ..default()
        }
    }
}