Skip to main content

full/
full.rs

1use bevy::sprite::MaterialMesh2dBundle;
2use bevy::{prelude::*, DefaultPlugins};
3use bevy_cameraman::{CameraBundle, CameraDebugPlugin, CameraPlugin, Cameraman, Target};
4
5fn main() {
6    let mut app = App::new();
7
8    app.add_plugins(DefaultPlugins)
9        // --- camera ---
10        .add_plugins((
11            CameraPlugin,
12            // CameraDebugPlugin, // uncomment this to see debug mode
13        ))
14        // --- systems ---
15        .add_systems(Startup, (setup_example, setup_rectangles))
16        .add_systems(Update, (keyboard_movements, update_axes))
17        .run();
18}
19
20#[derive(Component)]
21struct LocalPlayer;
22
23fn setup_example(
24    mut commands: Commands,
25    mut meshes: ResMut<Assets<Mesh>>,
26    mut materials: ResMut<Assets<ColorMaterial>>,
27) {
28    // 1. spawn your entity to follow
29    let entity = commands
30        .spawn((
31            MaterialMesh2dBundle {
32                mesh: meshes.add(shape::Circle::new(30.).into()).into(),
33                material: materials.add(ColorMaterial::from(Color::rgb(0.8, 0.3, 0.3))),
34                transform: Transform::from_translation(Vec3::new(-150., 0., 0.)),
35                ..default()
36            },
37            LocalPlayer {},
38            // you need this to tell the camera to focus this entity
39            Target,
40        ))
41        .id();
42
43    // 2. spawn your cameraman and make it follow previou entity
44    // you should play with the cameraman values!
45    commands.spawn(CameraBundle::new(
46        Cameraman::new(entity, Vec2::new(50.0, 20.0), Vec3::ONE * 0.8),
47        Camera2dBundle::default(),
48    ));
49}
50
51// -- for the demo -- //
52fn setup_rectangles(
53    mut commands: Commands,
54    mut meshes: ResMut<Assets<Mesh>>,
55    mut materials: ResMut<Assets<ColorMaterial>>,
56) {
57    commands.spawn(MaterialMesh2dBundle {
58        mesh: meshes.add(shape::Circle::new(30.).into()).into(),
59        material: materials.add(ColorMaterial::from(Color::rgb(0.3, 0.3, 0.8))),
60        transform: Transform::from_translation(Vec3::new(-150., -200., 0.)),
61        ..default()
62    });
63
64    commands.spawn(MaterialMesh2dBundle {
65        mesh: meshes.add(shape::Circle::new(30.).into()).into(),
66        material: materials.add(ColorMaterial::from(Color::rgb(0.3, 0.3, 0.8))),
67        transform: Transform::from_translation(Vec3::new(300., 120., 0.)),
68        ..default()
69    });
70}
71
72fn keyboard_movements(
73    time: Res<Time>,
74    keyboard_input: Res<Input<KeyCode>>,
75    mut query_player: Query<&mut Transform, With<LocalPlayer>>,
76) {
77    for mut transform in &mut query_player {
78        if keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A) {
79            transform.translation.x -= 200. * time.delta_seconds();
80        }
81        if keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::D) {
82            transform.translation.x += 200. * time.delta_seconds();
83        }
84        if keyboard_input.pressed(KeyCode::Down) || keyboard_input.pressed(KeyCode::S) {
85            transform.translation.y -= 200. * time.delta_seconds();
86        }
87        if keyboard_input.pressed(KeyCode::Up) || keyboard_input.pressed(KeyCode::W) {
88            transform.translation.y += 200. * time.delta_seconds();
89        }
90    }
91}
92
93fn update_axes(
94    time: Res<Time>,
95    gamepads: Res<Gamepads>,
96    axes: Res<Axis<GamepadAxis>>,
97    mut query: Query<&mut Transform, With<LocalPlayer>>,
98) {
99    for gamepad in gamepads.iter() {
100        let left_stick_x = axes
101            .get(GamepadAxis::new(gamepad, GamepadAxisType::LeftStickX))
102            .unwrap();
103        let mut moved = false;
104        if left_stick_x.abs() > 0.1 {
105            moved = true;
106            for mut transform in &mut query {
107                transform.translation.x += left_stick_x * 200. * time.delta_seconds();
108            }
109        }
110
111        let left_stick_y = axes
112            .get(GamepadAxis::new(gamepad, GamepadAxisType::LeftStickY))
113            .unwrap();
114        if left_stick_y.abs() > 0.1 {
115            moved = true;
116            for mut transform in &mut query {
117                transform.translation.y += left_stick_y * 200. * time.delta_seconds();
118            }
119        }
120
121        if moved {
122            for mut transform in &mut query {
123                transform.rotation = Quat::from_axis_angle(
124                    Vec3::new(0., 0., 1.),
125                    (-left_stick_x).atan2(left_stick_y),
126                );
127            }
128        }
129    }
130}