forked from BlackPhlox/bevy_dolly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
2d_cam_move.rs
96 lines (87 loc) · 3.04 KB
/
2d_cam_move.rs
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
use bevy::prelude::*;
use bevy_dolly::prelude::*;
#[derive(Component)]
struct MainCamera;
const SPEED: f32 = 4.5;
fn main() {
App::new()
.insert_resource(Msaa::default())
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
//If large amount of smoothing is used, where camera movement is expected beyond the time of input
//Ie. motion smoothing beyond 0.25, use update_2d_active_continuous instead
.add_systems(
Update,
(Dolly::<MainCamera>::update_2d_active, update_camera),
)
.run();
}
/// set up a simple 3D scene
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(SpriteBundle {
texture: asset_server.load("bevy_dolly.png"),
transform: Transform::from_xyz(100., 0., 0.),
sprite: Sprite {
custom_size: Some(Vec2::new(128., 128.)),
..Default::default()
},
..Default::default()
});
commands.spawn((
Camera2dBundle::default(),
MainCamera,
Rig::builder()
.with(Position::default())
.with(Smooth::new_position(0.25))
.build(),
));
commands.spawn(SpriteBundle {
texture: asset_server.load("room.png"),
transform: Transform::from_xyz(100., 0., 0.),
sprite: Sprite {
custom_size: Some(Vec2::new(2.6 * 800., 800.)),
..Default::default()
},
..Default::default()
});
info!("Use W, A, S, D for movement");
info!("Use Z & X zooming in and out");
}
fn update_camera(keys: Res<Input<KeyCode>>, mut query: Query<&mut Rig>) {
for mut rig in &mut query {
for &key in keys.get_pressed() {
let pos_driver = rig.try_driver_mut::<Position>();
if let Some(pos) = pos_driver {
if key == KeyCode::W {
pos.translate(SPEED * Vec3::Y);
}
if key == KeyCode::A {
pos.translate(SPEED * -Vec3::X);
}
if key == KeyCode::S {
pos.translate(SPEED * -Vec3::Y);
}
if key == KeyCode::D {
pos.translate(SPEED * Vec3::X);
}
if key == KeyCode::Z {
pos.translate(SPEED * -Vec3::Z);
}
if key == KeyCode::X {
pos.translate(SPEED * Vec3::Z);
}
}
let smooth_driver = rig.try_driver_mut::<Smooth>();
if let Some(smooth) = smooth_driver {
if key == KeyCode::C {
smooth.position_smoothness = (smooth.position_smoothness - 0.001).abs();
println!("Smoothness {}", smooth.position_smoothness);
}
if key == KeyCode::V {
smooth.position_smoothness = (smooth.position_smoothness + 0.001).abs();
println!("Smoothness {}", smooth.position_smoothness);
}
};
}
}
}