forked from BlackPhlox/bevy_dolly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
switch.rs
150 lines (132 loc) · 4.24 KB
/
switch.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#![allow(clippy::type_complexity)]
use std::f32::consts::PI;
use bevy::prelude::*;
use bevy_dolly::prelude::*;
#[derive(Component)]
struct MainCamera;
// In this example we are going to switch our look at target
// All you need to do is set a LookAt driver target_entity
// and its will track it
#[derive(States, Default, Clone, Eq, PartialEq, Debug, Hash)]
enum Camera {
#[default]
FollowPlayer,
FollowSheep,
}
fn main() {
App::new()
.insert_resource(Msaa::default())
.add_plugins((DefaultPlugins, DollyPosCtrl))
.add_systems(Startup, setup)
.add_systems(
Update,
(
Dolly::<MainCamera>::update_active,
rotator_system,
switch_camera_rig,
follow_player.run_if(in_state(Camera::FollowPlayer)),
follow_sheep.run_if(in_state(Camera::FollowSheep)),
),
)
.add_state::<Camera>()
.run();
}
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
..Default::default()
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
let start_pos = Vec3::new(0., 0., 0.);
commands.spawn((
Rotates,
SceneBundle {
scene: asset_server.load("poly_dolly.gltf#Scene0"),
transform: Transform {
translation: Vec3::new(0., 0.2, 0.),
..default()
},
..default()
},
));
commands.spawn((
MainCamera,
Rig::builder()
.with(Position::new(start_pos))
.with(Rotation::new(Quat::IDENTITY))
.with(Smooth::new_position(1.25).predictive(true))
.with(Arm::new(Vec3::new(0.0, 1.5, -3.5)))
.with(Smooth::new_position(2.5))
.with(
LookAt::new(start_pos + Vec3::Y)
.tracking_smoothness(1.25)
.tracking_predictive(true),
)
.build(),
));
commands.spawn((
MainCamera,
Camera3dBundle {
transform: Transform::from_xyz(-2.0, 1., 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
));
// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
//info!(" Use 1, 2, 3, 4 to target different sheep");
//info!(" Use Q and E to turn the sheep");
info!("Press C to toggle between the default player and the sheep");
}
fn follow_player(query: Query<(&Transform, With<DollyPosCtrlMove>)>, mut q: Query<&mut Rig>) {
if let Ok((p, _)) = query.get_single() {
let mut rig = q.single_mut();
rig.driver_mut::<Position>().position = p.translation;
rig.driver_mut::<Rotation>().rotation = p.rotation;
rig.driver_mut::<LookAt>().target = p.translation + Vec3::Y + Vec3::new(0., -1., 0.);
}
}
fn follow_sheep(query: Query<&Transform, With<Rotates>>, mut rig_q: Query<&mut Rig>) {
if let Ok(p) = query.get_single() {
let mut rig = rig_q.single_mut();
rig.driver_mut::<Position>().position = p.translation;
rig.driver_mut::<Rotation>().rotation = p.rotation;
rig.driver_mut::<LookAt>().target = p.translation + Vec3::Y;
}
}
#[derive(Component)]
struct Rotates;
fn rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<Rotates>>) {
for mut transform in query.iter_mut() {
*transform = Transform::from_rotation(Quat::from_rotation_y(
(4.0 * PI / 20.0) * time.delta_seconds(),
)) * *transform;
}
}
fn switch_camera_rig(
camera: Res<State<Camera>>,
mut next_camera: ResMut<NextState<Camera>>,
keyboard_input: Res<Input<KeyCode>>,
) {
if keyboard_input.just_pressed(KeyCode::C) {
let result = if *camera == Camera::FollowPlayer {
Camera::FollowSheep
} else {
Camera::FollowPlayer
};
println!("{result:?}");
next_camera.set(result);
}
}