Skip to content

Commit

Permalink
v1.1.1: QoL ig
Browse files Browse the repository at this point in the history
  • Loading branch information
hiimsergey authored Nov 12, 2023
1 parent 76425c5 commit 396208d
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "flappyspace"
version = "1.1.0"
version = "1.1.1"
edition = "2021"
description = "A small game about a little UFO dodging asteroids built in Bevy"
readme = "README.md"
Expand Down
6 changes: 3 additions & 3 deletions src/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ fn spawn_about_text(mut commands: Commands, assets: Res<AssetServer>) {
commands.spawn((
text_from_str(
&assets,
"Press Enter to exit",
"Press X to exit",
INPUT_HINT_FONT_SIZE,
ABOUT_TEXT_COLOR,
INPUT_HINT_ONE_Y
), OnAboutScreen, TextRotation
));
}

/// Checks for user input (Enter) to launch main menu
/// Checks for user input (spacebar) to launch main menu
fn about_input(
mut game_state: ResMut<NextState<GameState>>,
key: Res<Input<KeyCode>>
) {
if key.just_pressed(KeyCode::Return) {
if key.just_pressed(KeyCode::X) {
game_state.set(GameState::Menu);
}
}
2 changes: 1 addition & 1 deletion src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub const ABOUT_TEXT: &str = "Flappy Space
Built with Bevy Engine
<https://bevyengine.org>
v1.1.0 GPL-3.0 License";
v1.1.1 GPL-3.0 License";

/// Font size of content in About screen
pub const ABOUT_TEXT_FONT_SIZE: f32 = 30.;
Expand Down
15 changes: 11 additions & 4 deletions src/crashed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::prelude::*;
use flappyspace::*;
use crate::game::*;

/// Custom game plugin for all things on the Game Over screen
pub struct CrashedPlugin;
Expand All @@ -13,20 +14,26 @@ impl Plugin for CrashedPlugin {
app
.add_systems(
OnEnter(GameState::Crashed),
(spawn_crashed_text, spawn_highscore)
(spawn_crashed_text, spawn_highscore, move_ship_y)
)
.add_systems(
Update,
(lobby_input, rotate_text).run_if(in_state(GameState::Crashed))
(lobby_input, rotate_text, move_rocks).run_if(in_state(GameState::Crashed))
)
.add_systems(OnExit(GameState::Crashed), (
cleanup::<Rock>,
cleanup::<Scoreboard>,
cleanup::<OnCrashedScreen>
cleanup::<OnCrashedScreen>,
spawn_ship
));
}
}

/// Marks the ship as Rock so that it moves with them after the crash
fn move_ship_y(mut commands: Commands, query: Query<Entity, With<Ship>>) {
commands.entity(query.single()).insert(Rock);
}

/// Spawns text on Game Over screen: heading, two input hints
fn spawn_crashed_text(mut commands: Commands, assets: Res<AssetServer>) {
commands.spawn((
Expand All @@ -41,7 +48,7 @@ fn spawn_crashed_text(mut commands: Commands, assets: Res<AssetServer>) {
commands.spawn((
text_from_str(
&assets,
"Press Enter to restart",
"Press X to restart",
INPUT_HINT_FONT_SIZE,
Color::RED,
INPUT_HINT_UPPER_Y
Expand Down
84 changes: 42 additions & 42 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ impl Plugin for GamePlugin {
}
}

/// Moves every present Rock using ROCK_VELOCITY, despawns it if ROCK_DESPAWN_X
/// coordinate is crossed
pub fn move_rocks(
mut commands: Commands,
mut rock_query: Query<(Entity, &mut Transform), With<Rock>>,
time: Res<Time>
) {
for (rock_entity, mut transform) in rock_query.iter_mut() {
transform.translation.x -= ROCK_VELOCITY * time.delta_seconds();

if transform.translation.x < ROCK_DESPAWN_X {
commands.entity(rock_entity).despawn();
}
}
}

/// Loads RockTimer with a random duration defined by ROCK_SPAWN_RATE
fn load_rock_timer(mut commands: Commands) {
commands.insert_resource(RockTimer(Timer::from_seconds(
Expand All @@ -34,38 +50,6 @@ fn load_rock_timer(mut commands: Commands) {
)));
}

/// Spawns invisible scoreboard on screen, gets turned visible later
///
/// See also: [`periodic_waves`]
fn spawn_scoreboard_and_rock(
mut commands: Commands,
assets: Res<AssetServer>
) {
commands.spawn((
text_from_str(
&assets, "-2", HEADING_FONT_SIZE, Color::BLACK, HEADING_Y
), Scoreboard { score: -2 }
));

spawn_rock(commands, assets, BOTTOM_BOUND);
}

/// Makes score visible by turning the color white
///
/// Since it takes some time for the rock waves to come closer, I see no
/// meaning in starting to count the score instanty. Thus, the scoreboard
/// gets initialised with the value -2.
///
/// See also: [`spawn_scoreboard_and_rock`]
fn unhide_scoreboard(
mut score_query: Query<&mut Scoreboard>,
mut score_text_query: Query<&mut Text, With<Scoreboard>>
) {
if score_query.single_mut().score == 0 {
score_text_query.single_mut().sections[0].style.color = Color::WHITE;
}
}

/// Tracks RockTimer, increments score and launches new wave
///
/// See also: [`spawn_scoreboard_and_rock`]
Expand Down Expand Up @@ -95,19 +79,35 @@ fn periodic_waves(
}
}

/// Moves every present Rock using ROCK_VELOCITY, despawns it if ROCK_DESPAWN_X
/// coordinate is crossed
fn move_rocks(
/// Spawns invisible scoreboard on screen, gets turned visible later
///
/// See also: [`periodic_waves`]
fn spawn_scoreboard_and_rock(
mut commands: Commands,
mut rock_query: Query<(Entity, &mut Transform), With<Rock>>,
time: Res<Time>
assets: Res<AssetServer>
) {
for (rock_entity, mut transform) in rock_query.iter_mut() {
transform.translation.x -= ROCK_VELOCITY * time.delta_seconds();
commands.spawn((
text_from_str(
&assets, "-2", HEADING_FONT_SIZE, Color::BLACK, HEADING_Y
), Scoreboard { score: -2 }
));

if transform.translation.x < ROCK_DESPAWN_X {
commands.entity(rock_entity).despawn();
}
spawn_rock(commands, assets, BOTTOM_BOUND);
}

/// Makes score visible by turning the color white
///
/// Since it takes some time for the rock waves to come closer, I see no
/// meaning in starting to count the score instanty. Thus, the scoreboard
/// gets initialised with the value -2.
///
/// See also: [`spawn_scoreboard_and_rock`]
fn unhide_scoreboard(
mut score_query: Query<&mut Scoreboard>,
mut score_text_query: Query<&mut Text, With<Scoreboard>>
) {
if score_query.single_mut().score == 0 {
score_text_query.single_mut().sections[0].style.color = Color::WHITE;
}
}

Expand Down
43 changes: 35 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,13 @@ pub fn cleanup<T: Component>(
/// Is applied in menu and game over screen
pub fn lobby_input(
mut commands: Commands,
mut ship_query: Query<(&mut TextureAtlasSprite, &mut Ship)>,
mut game_state: ResMut<NextState<GameState>>,
assets: Res<AssetServer>,
key: Res<Input<KeyCode>>
) {
let (mut sprite, mut ship) = ship_query.single_mut();

// If user presses Enter, launches game
if key.just_pressed(KeyCode::Return) {
// If user presses X, launches game
if key.just_pressed(KeyCode::X) {
play_sound(&mut commands, &assets, "start");
sprite.index = 1;
ship.velocity = JUMP_VELOCITY;
game_state.set(GameState::Game);
}

Expand Down Expand Up @@ -141,7 +136,7 @@ pub fn spawn_rock(
y: f32
) {
// Base case
if y > TOP_BOUND { return }
if y > TOP_BOUND + 64. { return }

// Spawns rock
commands.spawn((
Expand All @@ -165,6 +160,38 @@ pub fn spawn_rock(
spawn_rock(commands, assets, y + y_distance);
}

/// Spawns the only Ship entity in the game at the center of the screen
///
/// The ship will be shown in the menu screen and in the gameplay.
pub fn spawn_ship(
mut commands: Commands,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
assets: Res<AssetServer>
) {
// Loads the sprite sheet
let texture_atlas = TextureAtlas::from_grid(
assets.load("sprites/ship.png"),
Vec2::new(12., 10.),
6, 1, None, None
);
let texture_atlas_handle = texture_atlases.add(texture_atlas);

// Spawns the entity
commands.spawn((
SpriteSheetBundle {
texture_atlas: texture_atlas_handle,
sprite: TextureAtlasSprite::new(1),
// Sets the spawn coordinates
// z = 1.1 lets the ship appear before the rocks and score
transform: Transform::from_xyz(0., 0., 1.1)
.with_scale(Vec3::splat(3.)),
..default()
},
ShipAnimationTimer(Timer::from_seconds(0.4, TimerMode::Repeating)),
Ship { velocity: JUMP_VELOCITY }
));
}

/// Returns a Text2dBundle to be spawned later
pub fn text_from_str(
assets: &Res<AssetServer>,
Expand Down
34 changes: 1 addition & 33 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn spawn_menu_text(mut commands: Commands, assets: Res<AssetServer>) {
commands.spawn((
text_from_str(
&assets,
"Press Enter to start",
"Press X to start",
INPUT_HINT_FONT_SIZE,
Color::WHITE,
INPUT_HINT_UPPER_Y
Expand All @@ -51,35 +51,3 @@ fn spawn_menu_text(mut commands: Commands, assets: Res<AssetServer>) {
), OnMenuScreen, TextRotation
));
}

/// Spawns the only Ship entity in the game at the center of the screen
///
/// The ship will be shown in the menu screen and in the gameplay.
fn spawn_ship(
mut commands: Commands,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
assets: Res<AssetServer>
) {
// Loads the sprite sheet
let texture_atlas = TextureAtlas::from_grid(
assets.load("sprites/ship.png"),
Vec2::new(12., 10.),
6, 1, None, None
);
let texture_atlas_handle = texture_atlases.add(texture_atlas);

// Spawns the entity
commands.spawn((
SpriteSheetBundle {
texture_atlas: texture_atlas_handle,
sprite: TextureAtlasSprite::new(1),
// Sets the spawn coordinates
// z = 1.1 lets the ship appear before the rocks and score
transform: Transform::from_xyz(0., 0., 1.1)
.with_scale(Vec3::splat(3.)),
..default()
},
ShipAnimationTimer(Timer::from_seconds(0.4, TimerMode::Repeating)),
Ship { velocity: JUMP_VELOCITY }
));
}

0 comments on commit 396208d

Please sign in to comment.