camera ui
This commit is contained in:
@@ -17,8 +17,12 @@ pub struct CameraRotation(pub f32);
|
||||
#[reflect(Resource)]
|
||||
pub struct CameraState {
|
||||
pub cutscene: bool,
|
||||
pub look_around: bool,
|
||||
}
|
||||
|
||||
#[derive(Component, Reflect, Debug, Default)]
|
||||
struct CameraUi;
|
||||
|
||||
#[derive(Component, Reflect, Debug)]
|
||||
#[reflect(Component)]
|
||||
pub struct MainCamera {
|
||||
@@ -40,7 +44,16 @@ pub fn plugin(app: &mut App) {
|
||||
|
||||
app.init_resource::<CameraState>();
|
||||
app.add_systems(Startup, startup);
|
||||
app.add_systems(Update, (update, rotate_view_keyboard, rotate_view_gamepad));
|
||||
app.add_systems(
|
||||
Update,
|
||||
(
|
||||
update,
|
||||
update_ui,
|
||||
update_look_around,
|
||||
rotate_view_keyboard,
|
||||
rotate_view_gamepad,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
fn startup(mut commands: Commands) {
|
||||
@@ -51,6 +64,54 @@ fn startup(mut commands: Commands) {
|
||||
));
|
||||
}
|
||||
|
||||
fn update_look_around(controls: Res<Controls>, mut cam_state: ResMut<CameraState>) {
|
||||
let look_around =
|
||||
controls.keyboard_state.view_mode || controls.gamepad_state.map_or(false, |g| g.view_mode);
|
||||
|
||||
if look_around != cam_state.look_around {
|
||||
cam_state.look_around = look_around;
|
||||
}
|
||||
}
|
||||
|
||||
fn update_ui(
|
||||
mut commands: Commands,
|
||||
cam_state: Res<CameraState>,
|
||||
asset_server: Res<AssetServer>,
|
||||
query: Query<Entity, With<CameraUi>>,
|
||||
) {
|
||||
if cam_state.is_changed() {
|
||||
let show_ui = cam_state.look_around || cam_state.cutscene;
|
||||
|
||||
if show_ui {
|
||||
let image = asset_server.load("ui/camera.png");
|
||||
|
||||
commands
|
||||
.spawn((
|
||||
CameraUi,
|
||||
Node {
|
||||
margin: UiRect::top(Val::Px(20.))
|
||||
.with_left(Val::Auto)
|
||||
.with_right(Val::Auto),
|
||||
justify_content: JustifyContent::Center,
|
||||
..default()
|
||||
},
|
||||
))
|
||||
.with_child((
|
||||
Node {
|
||||
display: Display::Block,
|
||||
position_type: PositionType::Absolute,
|
||||
..default()
|
||||
},
|
||||
ImageNode::new(image),
|
||||
));
|
||||
} else {
|
||||
for entity in query.iter() {
|
||||
commands.entity(entity).despawn_recursive();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update(
|
||||
mut cam: Query<
|
||||
(&MainCamera, &mut Transform, &CameraRotation),
|
||||
|
||||
Reference in New Issue
Block a user