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
266
267
268
269
270
271
272
273
274
275
276
277
//! Declarative behavior definitions — data-driven entity animations.
//!
//! All 7 behavior types from the current gen crate are preserved exactly.
//! The only structural change is that entity references use [`EntityRef`]
//! instead of raw `String`.
use serde::{Deserialize, Serialize};
use crate::identity::EntityRef;
/// Declarative behavior definition — data, not code.
/// Each variant fully describes a continuous animation that the tick system evaluates.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum BehaviorDef {
/// Orbit around a center entity or point.
Orbit {
/// Entity to orbit around (mutually exclusive with `center_point`).
#[serde(default, skip_serializing_if = "Option::is_none")]
center: Option<EntityRef>,
/// Fixed point to orbit around [x,y,z] (used if `center` is None).
#[serde(default, skip_serializing_if = "Option::is_none")]
center_point: Option<[f32; 3]>,
/// Orbit radius.
#[serde(default = "default_orbit_radius")]
radius: f32,
/// Orbital speed in degrees per second.
#[serde(default = "default_orbit_speed")]
speed: f32,
/// Orbit axis (normalized). Default: Y-up.
#[serde(default = "default_y_axis")]
axis: [f32; 3],
/// Initial phase angle in degrees.
#[serde(default)]
phase: f32,
/// Orbit tilt in degrees (inclination from the axis plane).
#[serde(default)]
tilt: f32,
},
/// Spin (rotate) around a local axis.
Spin {
/// Local axis to spin around.
#[serde(default = "default_y_axis")]
axis: [f32; 3],
/// Rotation speed in degrees per second.
#[serde(default = "default_spin_speed")]
speed: f32,
},
/// Bob up and down (sinusoidal oscillation along an axis).
Bob {
/// Axis of oscillation.
#[serde(default = "default_y_axis")]
axis: [f32; 3],
/// Amplitude (distance from center in each direction).
#[serde(default = "default_bob_amplitude")]
amplitude: f32,
/// Oscillation frequency in Hz.
#[serde(default = "default_bob_frequency")]
frequency: f32,
/// Phase offset in degrees.
#[serde(default)]
phase: f32,
},
/// Continuously look at / follow another entity.
LookAt {
/// Entity to look at.
target: EntityRef,
},
/// Scale pulsation (breathing effect).
Pulse {
/// Minimum scale multiplier.
#[serde(default = "default_pulse_min")]
min_scale: f32,
/// Maximum scale multiplier.
#[serde(default = "default_pulse_max")]
max_scale: f32,
/// Pulse frequency in Hz.
#[serde(default = "default_bob_frequency")]
frequency: f32,
},
/// Follow a path of waypoints in sequence.
PathFollow {
/// Ordered waypoints [[x,y,z], ...].
waypoints: Vec<[f32; 3]>,
/// Movement speed in units per second.
#[serde(default = "default_path_speed")]
speed: f32,
/// Loop mode: "loop" wraps back to start, "ping_pong" reverses.
#[serde(default = "default_path_mode")]
mode: PathMode,
/// Smoothly interpolate rotation toward movement direction.
#[serde(default)]
orient_to_path: bool,
},
/// Bouncing on a surface with gravity.
Bounce {
/// Height of initial/max bounce.
#[serde(default = "default_bounce_height")]
height: f32,
/// Gravity acceleration (units/s^2).
#[serde(default = "default_bounce_gravity")]
gravity: f32,
/// Energy retained per bounce (0.0-1.0).
#[serde(default = "default_bounce_damping")]
damping: f32,
/// Surface Y level to bounce on.
#[serde(default)]
surface_y: f32,
},
}
/// Path follow loop mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum PathMode {
#[default]
Loop,
PingPong,
Once,
}
// ---- Default value helpers (match current gen crate exactly) ----
pub(crate) fn default_orbit_radius() -> f32 {
5.0
}
pub(crate) fn default_orbit_speed() -> f32 {
36.0
}
pub(crate) fn default_y_axis() -> [f32; 3] {
[0.0, 1.0, 0.0]
}
pub(crate) fn default_spin_speed() -> f32 {
90.0
}
pub(crate) fn default_bob_amplitude() -> f32 {
0.5
}
pub(crate) fn default_bob_frequency() -> f32 {
0.5
}
pub(crate) fn default_pulse_min() -> f32 {
0.9
}
pub(crate) fn default_pulse_max() -> f32 {
1.1
}
pub(crate) fn default_path_speed() -> f32 {
2.0
}
pub(crate) fn default_path_mode() -> PathMode {
PathMode::Loop
}
pub(crate) fn default_bounce_height() -> f32 {
3.0
}
pub(crate) fn default_bounce_gravity() -> f32 {
9.8
}
pub(crate) fn default_bounce_damping() -> f32 {
0.7
}
impl BehaviorDef {
/// Returns the behavior kind as a string.
pub fn kind(&self) -> &'static str {
match self {
BehaviorDef::Orbit { .. } => "orbit",
BehaviorDef::Spin { .. } => "spin",
BehaviorDef::Bob { .. } => "bob",
BehaviorDef::LookAt { .. } => "look_at",
BehaviorDef::Pulse { .. } => "pulse",
BehaviorDef::PathFollow { .. } => "path_follow",
BehaviorDef::Bounce { .. } => "bounce",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn behavior_orbit_roundtrip() {
let b = BehaviorDef::Orbit {
center: Some(EntityRef::name("sun")),
center_point: None,
radius: 10.0,
speed: 45.0,
axis: [0.0, 1.0, 0.0],
phase: 0.0,
tilt: 15.0,
};
let json = serde_json::to_string(&b).unwrap();
let back: BehaviorDef = serde_json::from_str(&json).unwrap();
assert_eq!(b, back);
}
#[test]
fn behavior_look_at_with_entity_ref() {
let b = BehaviorDef::LookAt {
target: EntityRef::name("player"),
};
let json = serde_json::to_string(&b).unwrap();
assert!(json.contains("player"));
let back: BehaviorDef = serde_json::from_str(&json).unwrap();
assert_eq!(b, back);
}
#[test]
fn all_behavior_kinds() {
let behaviors = [
BehaviorDef::Orbit {
center: None,
center_point: Some([0.0, 0.0, 0.0]),
radius: 5.0,
speed: 36.0,
axis: [0.0, 1.0, 0.0],
phase: 0.0,
tilt: 0.0,
},
BehaviorDef::Spin {
axis: [0.0, 1.0, 0.0],
speed: 90.0,
},
BehaviorDef::Bob {
axis: [0.0, 1.0, 0.0],
amplitude: 0.5,
frequency: 0.5,
phase: 0.0,
},
BehaviorDef::LookAt {
target: EntityRef::name("cam"),
},
BehaviorDef::Pulse {
min_scale: 0.9,
max_scale: 1.1,
frequency: 0.5,
},
BehaviorDef::PathFollow {
waypoints: vec![[0.0, 0.0, 0.0], [5.0, 0.0, 0.0]],
speed: 2.0,
mode: PathMode::PingPong,
orient_to_path: true,
},
BehaviorDef::Bounce {
height: 3.0,
gravity: 9.8,
damping: 0.7,
surface_y: 0.0,
},
];
let kinds: Vec<&str> = behaviors.iter().map(|b| b.kind()).collect();
assert_eq!(
kinds,
vec![
"orbit",
"spin",
"bob",
"look_at",
"pulse",
"path_follow",
"bounce"
]
);
}
#[test]
fn path_mode_roundtrip() {
for mode in [PathMode::Loop, PathMode::PingPong, PathMode::Once] {
let json = serde_json::to_string(&mode).unwrap();
let back: PathMode = serde_json::from_str(&json).unwrap();
assert_eq!(mode, back);
}
}
}