-
Notifications
You must be signed in to change notification settings - Fork 0
/
win_screen.rs
90 lines (81 loc) · 2.68 KB
/
win_screen.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
use crate::state::GameState;
use bevy::prelude::*;
use bevy_ecs_ldtk::LdtkAsset;
pub struct WinScreenPlugin;
impl Plugin for WinScreenPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::Win), spawn_win_screen)
.add_systems(OnExit(GameState::Win), despawn_win_screen);
#[cfg(debug_assertions)]
app.add_systems(Update, return_to_title.run_if(in_state(GameState::Win)));
}
}
#[derive(Component)]
struct WinScreen;
fn spawn_win_screen(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut camera_query: Query<(&mut OrthographicProjection, &mut Transform)>,
) {
commands
.spawn((
WinScreen,
NodeBundle {
style: Style {
width: Val::Percent(100.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..Default::default()
},
..Default::default()
},
))
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text::from_section(
"Thank you for playing!",
TextStyle {
font: asset_server.load("fonts/PeaberryMono.ttf"),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
),
style: Style {
position_type: PositionType::Absolute,
bottom: Val::Percent(10.0),
..Default::default()
},
..Default::default()
});
});
commands.spawn((
WinScreen,
SpriteBundle {
texture: asset_server.load("atlas/win_screen.png"),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
..Default::default()
},
));
let (mut orthographic_projection, mut camera_transform) = camera_query.single_mut();
*orthographic_projection = Camera2dBundle::default().projection;
*camera_transform = Transform::default();
}
fn despawn_win_screen(mut commands: Commands, q: Query<Entity, With<WinScreen>>) {
for e in &q {
commands.entity(e).despawn_recursive();
}
}
#[cfg(debug_assertions)]
fn return_to_title(
mut commands: Commands,
input: Res<Input<KeyCode>>,
mut state: ResMut<NextState<GameState>>,
ldtk_query: Query<Entity, With<Handle<LdtkAsset>>>,
) {
if input.just_pressed(KeyCode::Space) {
for entity in &ldtk_query {
commands.entity(entity).despawn_recursive();
}
state.set(GameState::Title)
}
}