forked from jbuehler23/jackdaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_components.rs
More file actions
148 lines (131 loc) · 3.62 KB
/
custom_components.rs
File metadata and controls
148 lines (131 loc) · 3.62 KB
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
//! Example showing how to register custom game components with the editor.
//!
//! Components that implement `EditorMeta` get descriptions and categories in the
//! component picker. Components without it still appear under "Game" automatically.
//!
//! Run with: `cargo run --example custom_components`
use bevy::prelude::*;
use jackdaw::{EditorMeta, EditorPlugin, ReflectEditorMeta};
fn main() -> AppExit {
App::new()
.add_plugins((DefaultPlugins, EditorPlugin))
.register_type::<Health>()
.register_type::<Speed>()
.register_type::<Team>()
.register_type::<DamageOverTime>()
.register_type::<Interactable>()
.add_systems(Startup, spawn_scene)
.run()
}
// --- Gameplay components ---
#[derive(Component, Reflect, Default)]
#[reflect(Component, Default, EditorMeta)]
struct Health {
pub current: f32,
pub max: f32,
}
impl EditorMeta for Health {
fn description() -> &'static str {
"Tracks entity health points"
}
fn category() -> &'static str {
"Gameplay"
}
}
#[derive(Component, Reflect, Default)]
#[reflect(Component, Default, EditorMeta)]
struct Speed {
pub value: f32,
}
impl EditorMeta for Speed {
fn description() -> &'static str {
"Movement speed multiplier"
}
fn category() -> &'static str {
"Gameplay"
}
}
#[derive(Component, Reflect, Default)]
#[reflect(Component, Default, EditorMeta)]
struct DamageOverTime {
pub damage_per_second: f32,
pub duration: f32,
}
impl EditorMeta for DamageOverTime {
fn description() -> &'static str {
"Applies damage each second for a duration"
}
fn category() -> &'static str {
"Gameplay"
}
}
// --- AI components ---
#[derive(Component, Reflect, Default)]
#[reflect(Component, Default, EditorMeta)]
struct Team {
pub id: u32,
}
impl EditorMeta for Team {
fn description() -> &'static str {
"Faction/team assignment for AI"
}
fn category() -> &'static str {
"AI"
}
}
// --- Component without EditorMeta (still works, appears under "Game") ---
#[derive(Component, Reflect, Default)]
#[reflect(Component, Default)]
struct Interactable {
pub radius: f32,
}
fn spawn_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
Name::new("Sun"),
DirectionalLight {
shadows_enabled: true,
illuminance: 10000.0,
..default()
},
Transform::from_xyz(10.0, 20.0, 10.0).with_rotation(Quat::from_euler(
EulerRot::XYZ,
-0.8,
0.4,
0.0,
)),
));
// Player: visible cube with custom components pre-attached
commands.spawn((
Name::new("Player"),
Mesh3d(meshes.add(Cuboid::new(1.0, 2.0, 1.0))),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::srgb(0.2, 0.6, 1.0),
..default()
})),
Transform::from_xyz(0.0, 1.0, 0.0),
Health {
current: 100.0,
max: 100.0,
},
Speed { value: 5.0 },
));
// Enemy: second entity showing different custom components
commands.spawn((
Name::new("Enemy"),
Mesh3d(meshes.add(Cuboid::new(1.0, 1.5, 1.0))),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::srgb(0.9, 0.2, 0.2),
..default()
})),
Transform::from_xyz(4.0, 0.75, 0.0),
Team { id: 2 },
DamageOverTime {
damage_per_second: 10.0,
duration: 5.0,
},
));
}