Skip to content

Commit 6347afd

Browse files
committed
add how to play
closes #64
1 parent 655444f commit 6347afd

File tree

7 files changed

+189
-17
lines changed

7 files changed

+189
-17
lines changed

art/fonts.aseprite

21 Bytes
Binary file not shown.

assets/font.ttf

16 Bytes
Binary file not shown.

src/audio/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ fn fade_out(
175175
audio.set_volume(
176176
current_volume.fade_towards(Volume::Linear(0.0), time.delta_secs() / fade_out.duration),
177177
);
178-
println!("fading out: {:?}", audio.volume().to_linear());
179178
if audio.volume().to_linear() <= 0.15 {
180179
commands.entity(entity).despawn();
181180
}

src/screens/game.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
126126
timer: Timer::from_seconds(0.15, TimerMode::Once),
127127
shield: 2,
128128
shield_regen: Timer::from_seconds(5.0, TimerMode::Once),
129-
shield_hit_cooldown: Timer::from_seconds(1.0, TimerMode::Once),
129+
shield_hit_cooldown: Timer::from_seconds(0.5, TimerMode::Once),
130130
},
131131
Sprite {
132132
image: asset_server.load("sprites/ships/ship3.png"),
@@ -236,7 +236,6 @@ fn stopwatch(
236236
mut game_stats: ResMut<GameStats>,
237237
mut end_game: ResMut<EndGame>,
238238
) {
239-
println!("time played: {:?}", game_stats.time_played);
240239
if game_stats.time_played > 300.0 {
241240
game_stats.time_played = 300.0;
242241
end_game.0 = true;
@@ -635,7 +634,7 @@ fn restart_game(
635634
mut pause_state: ResMut<NextState<PauseState>>,
636635
mut game_state: ResMut<NextState<GameState>>,
637636
mut has_entered: ResMut<HasEntered>,
638-
mut player: Query<&mut Transform, With<Player>>,
637+
mut player: Query<(&mut Transform, &mut Player)>,
639638
asteroids: Query<Entity, With<Asteroid>>,
640639
projectiles: Query<Entity, With<Projectile>>,
641640
mut end_game: ResMut<EndGame>,
@@ -651,8 +650,11 @@ fn restart_game(
651650

652651
end_game.0 = false;
653652
pause_state.set(PauseState::NotPaused);
654-
let mut player_transform = player.single_mut().unwrap();
653+
let (mut player_transform, mut player) = player.single_mut().unwrap();
655654
player_transform.translation = Vec3::new(0.0, -280.0, 0.0);
655+
player.shield = 2;
656+
player.shield_hit_cooldown.reset();
657+
player.shield_regen.reset();
656658

657659
let mut planet_transform = planet.single_mut().unwrap();
658660
planet_transform.translation = Vec3::new(0.0, 400.0, -11.0);

src/screens/howtoplay.rs

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
use bevy::{prelude::*, text::FontSmoothing};
2+
use leafwing_input_manager::prelude::ActionState;
3+
4+
use crate::{PIXEL_PERFECT_LAYERS, ScaleFactor, input::Action, screens::Screen};
5+
6+
pub(super) fn plugin(app: &mut App) {
7+
app.add_systems(OnEnter(Screen::HowToPlay), spawn_howtoplay);
8+
app.add_systems(
9+
Update,
10+
(howtoplay_input, howtoplay_button).run_if(in_state(Screen::HowToPlay)),
11+
);
12+
}
13+
14+
fn spawn_howtoplay(
15+
mut commands: Commands,
16+
scale_factor: Res<ScaleFactor>,
17+
asset_server: Res<AssetServer>,
18+
) {
19+
let scale = scale_factor.0;
20+
let font_handle = asset_server.load("font.ttf");
21+
commands.spawn((
22+
Name::new("HowToPlay container"),
23+
Node {
24+
width: Val::Percent(80.0),
25+
height: Val::Percent(80.0),
26+
margin: UiRect::all(Val::Auto),
27+
padding: UiRect::all(Val::Px(8.0 * scale)),
28+
justify_content: JustifyContent::Center,
29+
align_items: AlignItems::Center,
30+
flex_direction: FlexDirection::Column,
31+
..default()
32+
},
33+
DespawnOnExit(Screen::HowToPlay),
34+
PIXEL_PERFECT_LAYERS,
35+
children![
36+
(
37+
Name::new("HowToPlay"),
38+
Text::new("HOW TO PLAY"),
39+
TextFont {
40+
font_size: 16.0 * scale,
41+
font: font_handle.clone(),
42+
font_smoothing: FontSmoothing::None,
43+
..default()
44+
},
45+
Node {
46+
position_type: PositionType::Absolute,
47+
top: Val::Px(0.0),
48+
margin: UiRect {
49+
left: Val::Auto,
50+
right: Val::Auto,
51+
..default()
52+
},
53+
..default()
54+
}
55+
),
56+
(
57+
Text::new("MOVE WITH WASD / ARROWS / ANALOG STICK / D PAD"),
58+
TextFont {
59+
font_size: 12.0 * scale,
60+
font: font_handle.clone(),
61+
font_smoothing: FontSmoothing::None,
62+
..default()
63+
},
64+
Node {
65+
padding: UiRect {
66+
top: Val::Px(8.0 * scale),
67+
bottom: Val::Px(8.0 * scale),
68+
..default()
69+
},
70+
..default()
71+
},
72+
),
73+
(
74+
Text::new("SHOOT WITH SPACE / BOTTOM BUTTON ON GAMEPAD CLUSTER"),
75+
TextFont {
76+
font_size: 12.0 * scale,
77+
font: font_handle.clone(),
78+
font_smoothing: FontSmoothing::None,
79+
..default()
80+
},
81+
Node {
82+
padding: UiRect {
83+
top: Val::Px(8.0 * scale),
84+
bottom: Val::Px(8.0 * scale),
85+
..default()
86+
},
87+
..default()
88+
},
89+
),
90+
(
91+
Text::new("PAUSE WITH ESCAPE / GAMEPAD START / GAMEPAD SELECT"),
92+
TextFont {
93+
font_size: 12.0 * scale,
94+
font: font_handle.clone(),
95+
font_smoothing: FontSmoothing::None,
96+
..default()
97+
},
98+
Node {
99+
padding: UiRect {
100+
top: Val::Px(8.0 * scale),
101+
bottom: Val::Px(8.0 * scale),
102+
..default()
103+
},
104+
..default()
105+
},
106+
),
107+
(
108+
Name::new("Back to menu button"),
109+
Button,
110+
Node {
111+
position_type: PositionType::Absolute,
112+
bottom: Val::Px(0.0),
113+
margin: UiRect {
114+
left: Val::Auto,
115+
right: Val::Auto,
116+
..default()
117+
},
118+
..default()
119+
},
120+
children![(
121+
Name::new("Back to menu text"),
122+
Text::new("BACK TO MENU"),
123+
TextFont {
124+
font_size: 16.0 * scale,
125+
font: font_handle.clone(),
126+
font_smoothing: FontSmoothing::None,
127+
..default()
128+
}
129+
)]
130+
),
131+
],
132+
));
133+
}
134+
135+
fn howtoplay_button(
136+
interaction_query: Query<&Interaction, Changed<Interaction>>,
137+
mut screen: ResMut<NextState<Screen>>,
138+
) {
139+
for interaction in interaction_query.iter() {
140+
if *interaction == Interaction::Pressed {
141+
screen.set(Screen::Title)
142+
}
143+
}
144+
}
145+
146+
fn howtoplay_input(
147+
action_state: Single<&ActionState<Action>>,
148+
mut screen: ResMut<NextState<Screen>>,
149+
) {
150+
if action_state.just_pressed(&Action::Select) {
151+
screen.set(Screen::Title);
152+
}
153+
}

src/screens/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod credits;
44
mod endgame;
55
mod game;
66
mod gameover;
7+
mod howtoplay;
78
mod pause;
89
mod splash;
910
mod title;
@@ -19,13 +20,15 @@ pub(super) fn plugin(app: &mut App) {
1920
pause::plugin,
2021
gameover::plugin,
2122
endgame::plugin,
23+
howtoplay::plugin,
2224
));
2325
}
2426

2527
#[derive(States, Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
2628
pub enum Screen {
2729
#[default]
2830
Splash,
31+
HowToPlay,
2932
Title,
3033
Game,
3134
Credits,

src/screens/title.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub(super) fn plugin(app: &mut App) {
2020

2121
#[derive(Component)]
2222
enum MenuButton {
23+
HowToPlay,
2324
NewGame,
2425
// Settings,
2526
Credits,
@@ -68,9 +69,16 @@ fn spawn_title_screen(
6869
}
6970
let font_handle: Handle<Font> = asset_server.load("font.ttf");
7071

72+
let how_to_play = title_menu_button(
73+
"HOW TO PLAY",
74+
0,
75+
MenuButton::HowToPlay,
76+
font_handle.clone(),
77+
scale,
78+
);
7179
let new_game = title_menu_button(
7280
"NEW GAME",
73-
0,
81+
1,
7482
MenuButton::NewGame,
7583
font_handle.clone(),
7684
scale,
@@ -84,18 +92,18 @@ fn spawn_title_screen(
8492
// );
8593
let credits = title_menu_button(
8694
"CREDITS",
87-
1,
95+
2,
8896
MenuButton::Credits,
8997
font_handle.clone(),
9098
scale,
9199
);
92100

93101
#[cfg(target_arch = "wasm32")]
94-
let menu_buttons = children![new_game, credits];
102+
let menu_buttons = children![how_to_play, new_game, credits];
95103
#[cfg(not(target_arch = "wasm32"))]
96-
let quit = title_menu_button("QUIT", 2, MenuButton::Quit, font_handle.clone(), scale);
104+
let quit = title_menu_button("QUIT", 3, MenuButton::Quit, font_handle.clone(), scale);
97105
#[cfg(not(target_arch = "wasm32"))]
98-
let menu_buttons = children![new_game, credits, quit];
106+
let menu_buttons = children![how_to_play, new_game, credits, quit];
99107

100108
commands.spawn((
101109
Node {
@@ -134,9 +142,9 @@ fn input_system(
134142
let action_state = input_query.single().unwrap();
135143

136144
#[cfg(not(target_arch = "wasm32"))]
137-
let max_index = 2;
145+
let max_index = 3;
138146
#[cfg(target_arch = "wasm32")]
139-
let max_index = 1;
147+
let max_index = 2;
140148

141149
if action_state.just_pressed(&Action::Up) {
142150
if active_index.0 == 0 {
@@ -165,13 +173,16 @@ fn input_system(
165173
if action_state.just_pressed(&Action::Select) {
166174
match active_index.0 {
167175
0 => {
176+
screen_state.set(Screen::HowToPlay);
177+
}
178+
1 => {
168179
screen_state.set(Screen::Game);
169180
commands.trigger(RestartGame);
170181
}
171-
1 => {
182+
2 => {
172183
screen_state.set(Screen::Credits);
173184
}
174-
2 => {
185+
3 => {
175186
message_writer.write(AppExit::Success);
176187
}
177188
_ => {}
@@ -189,6 +200,9 @@ fn button_system(
189200
for (interaction, menu_button, children) in interaction_query {
190201
match *interaction {
191202
Interaction::Pressed => match menu_button {
203+
MenuButton::HowToPlay => {
204+
screen_state.set(Screen::HowToPlay);
205+
}
192206
MenuButton::NewGame => {
193207
screen_state.set(Screen::Game);
194208
commands.trigger(RestartGame);
@@ -207,13 +221,14 @@ fn button_system(
207221
.entity(*children.first().unwrap())
208222
.insert(TextColor(WHITE));
209223
match menu_button {
210-
MenuButton::NewGame => active_index.0 = 0,
211-
MenuButton::Credits => active_index.0 = 1,
224+
MenuButton::HowToPlay => active_index.0 = 0,
225+
MenuButton::NewGame => active_index.0 = 1,
226+
MenuButton::Credits => active_index.0 = 2,
212227
// MenuButton::Settings => {
213228
// active_index.0 = 1;
214229
// }
215230
#[cfg(not(target_arch = "wasm32"))]
216-
MenuButton::Quit => active_index.0 = 2,
231+
MenuButton::Quit => active_index.0 = 3,
217232
}
218233
}
219234
Interaction::None => {

0 commit comments

Comments
 (0)