forked from jbuehler23/jackdaw
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomponents_in_editor.rs
More file actions
129 lines (116 loc) · 3.69 KB
/
Copy pathcomponents_in_editor.rs
File metadata and controls
129 lines (116 loc) · 3.69 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
//! Custom game components in the editor.
//!
//! `reflect_documentation` (enabled in the workspace `bevy`
//! dep) captures `///` doc comments into the type registry; the
//! picker uses them as tooltip text. Override with
//! `#[reflect(@EditorDescription("..."))]`, or bucket into a
//! named group with `#[reflect(@EditorCategory("Gameplay"))]`.
//!
//! Run: `cargo run --example components_in_editor`
use avian3d::prelude::PhysicsPlugins;
use bevy::prelude::*;
use bevy_enhanced_input::prelude::EnhancedInputPlugin;
use jackdaw::prelude::*;
fn main() -> AppExit {
App::new()
// log errors instead of panicking
.set_error_handler(bevy::ecs::error::error)
// Ambient plugins at the binary boundary. Editor crates
// assert presence, so user plugins can add the same
// plugins without a duplicate panic.
.add_plugins((
DefaultPlugins,
PhysicsPlugins::default(),
EnhancedInputPlugin,
EditorPlugins::default(),
))
.add_systems(Startup, spawn_scene)
.run()
}
// Gameplay components below: no `register_type` calls;
// `reflect_auto_register` (default-on in bevy 0.18) handles it.
/// Tracks entity health points.
#[derive(Component, Reflect, Default)]
#[reflect(Component, Default, @EditorCategory::new("Gameplay"))]
struct Health {
pub current: f32,
pub max: f32,
}
/// Movement speed multiplier.
#[derive(Component, Reflect, Default)]
#[reflect(Component, Default, @EditorCategory::new("Gameplay"))]
struct Speed {
pub value: f32,
}
/// Applies damage each second for a duration.
#[derive(Component, Reflect, Default)]
#[reflect(Component, Default, @EditorCategory::new("Gameplay"))]
struct DamageOverTime {
pub damage_per_second: f32,
pub duration: f32,
}
// --- AI components ---
/// Faction/team assignment for AI.
#[derive(Component, Reflect, Default)]
#[reflect(Component, Default, @EditorCategory::new("AI"))]
struct Team {
pub id: u32,
}
// --- Component without category override (still works, appears under "Game") ---
/// Range within which the player can interact with this entity.
#[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 {
shadow_maps_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,
},
Interactable { radius: 1.5 },
));
}