server/
server.rs

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