docs/bevy/examples/remote/server.rs 2.5 K · 91 lines · raw · history

1 //! A Bevy app that you can connect to with the BRP and edit.
2 //! Run this example with the `remote` feature enabled:
3 //! ```bash
4 //! cargo run --example server --features="bevy_remote"
5 //! ```
6
7 use bevy::math::ops::cos;
8 use bevy::{
9 input::common_conditions::input_just_pressed,
10 prelude::*,
11 remote::{http::RemoteHttpPlugin, RemotePlugin},
12 };
13 use serde::{Deserialize, Serialize};
14
15 fn main() {
16 App::new()
17 .add_plugins(DefaultPlugins)
18 .add_plugins(RemotePlugin::default())
19 .add_plugins(RemoteHttpPlugin::default())
20 .add_systems(Startup, setup)
21 .add_systems(Update, remove.run_if(input_just_pressed(KeyCode::Space)))
22 .add_systems(Update, move_cube)
23 .run();
24 }
25
26 fn setup(
27 mut commands: Commands,
28 mut meshes: ResMut<Assets<Mesh>>,
29 mut materials: ResMut<Assets<StandardMaterial>>,
30 ) {
31 // circular base
32 commands.spawn((
33 Mesh3d(meshes.add(Circle::new(4.0))),
34 MeshMaterial3d(materials.add(Color::WHITE)),
35 Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
36 ));
37
38 // cube
39 commands.spawn((
40 Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
41 MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
42 Transform::from_xyz(0.0, 0.5, 0.0),
43 Cube(1.0),
44 ));
45
46 // test resource
47 commands.insert_resource(TestResource {
48 foo: Vec2::new(1.0, -1.0),
49 bar: false,
50 });
51
52 // light
53 commands.spawn((
54 PointLight {
55 shadow_maps_enabled: true,
56 ..default()
57 },
58 Transform::from_xyz(4.0, 8.0, 4.0),
59 ));
60
61 // camera
62 commands.spawn((
63 Camera3d::default(),
64 Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
65 ));
66 }
67
68 /// An arbitrary resource that can be inspected and manipulated with remote methods.
69 #[derive(Resource, Reflect, Serialize, Deserialize)]
70 #[reflect(Resource, Serialize, Deserialize)]
71 pub struct TestResource {
72 /// An arbitrary field of the test resource.
73 pub foo: Vec2,
74
75 /// Another arbitrary field.
76 pub bar: bool,
77 }
78
79 fn move_cube(mut query: Query<&mut Transform, With<Cube>>, time: Res<Time>) {
80 for mut transform in &mut query {
81 transform.translation.y = -cos(time.elapsed_secs()) + 1.5;
82 }
83 }
84
85 fn remove(mut commands: Commands, cube_entity: Single<Entity, With<Cube>>) {
86 commands.entity(*cube_entity).remove::<Cube>();
87 }
88
89 #[derive(Component, Reflect, Serialize, Deserialize)]
90 #[reflect(Component, Serialize, Deserialize)]
91 struct Cube(f32);