Skip to content

Commit

Permalink
add countdown; add music to resource; change level music; fix finish …
Browse files Browse the repository at this point in the history
…line
  • Loading branch information
nolanking90 committed Sep 20, 2024
1 parent 91b4b66 commit e851e6c
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target
.DS_Store
/assets/.DS_Store
Binary file added assets/240bps.mp3
Binary file not shown.
Binary file added assets/GO3.wav
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added assets/countdown.wav
Binary file not shown.
Binary file added assets/dui.mp3
Binary file not shown.
File renamed without changes
Binary file added assets/transflag.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 103 additions & 12 deletions src/levels.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use crate::CameraMarker;
use crate::{car::*, menu::*};
use bevy::prelude::*;
Expand All @@ -19,6 +21,7 @@ pub struct LevelAssets {
pub obstacle_speed: f32,
pub y_values: [f32; 4],
pub background_texture: Handle<Image>,
pub music: Handle<AudioSource>,
}

pub fn game_over(
Expand Down Expand Up @@ -56,12 +59,12 @@ pub fn game_over(
pub fn next_level(
mut commands: Commands,
window: Query<&Window>,
camera: Query<&Transform, With<CameraMarker>>,
car: Query<&Transform, With<Car>>,
mut next_state: ResMut<NextState<GameState>>,
mut level: ResMut<Level>,
) {
let width = window.single().width();
let laps = (camera.single().translation.x / (width) / 10.0) as u8;
let laps = (car.single().translation.x / (width) / 10.0) as u8;

if laps == 1 {
commands.spawn((
Expand Down Expand Up @@ -92,13 +95,7 @@ pub fn next_level(
pub fn despawn_level(
mut commands: Commands,
old_assets: Query<Entity, (With<LevelAssetMarker>, Without<MenuText>)>,
menutext: Query<
Entity,
(
With<MenuText>,
Without<LevelAssetMarker>,
),
>,
menutext: Query<Entity, (With<MenuText>, Without<LevelAssetMarker>)>,
mut next_state: ResMut<NextState<GameState>>,
) {
if !old_assets.is_empty() {
Expand All @@ -112,7 +109,6 @@ pub fn despawn_level(
}

next_state.set(GameState::LoadNextLevel);

}

pub fn load_level(
Expand Down Expand Up @@ -146,6 +142,7 @@ pub fn load_level(
],
car_texture: asset_server.load("1084.png"),
background_texture: asset_server.load("1058.png"),
music: asset_server.load("240bps.mp3"),
};
}
2 => {
Expand All @@ -165,6 +162,7 @@ pub fn load_level(
],
car_texture: asset_server.load("1145.png"),
background_texture: asset_server.load("backroads.png"),
music: asset_server.load("dui.mp3"),
};
}
3 => {}
Expand Down Expand Up @@ -203,7 +201,7 @@ pub fn despawn_loading_screen(
asset_server: Res<AssetServer>,
level_assets: Res<LevelAssets>,
mut next_state: ResMut<NextState<GameState>>,
menutext: Query<Entity, With<MenuText>>
menutext: Query<Entity, With<MenuText>>,
) {
if asset_server
.get_load_state(&level_assets.car_texture)
Expand Down Expand Up @@ -237,5 +235,98 @@ pub fn despawn_loading_screen(
if !menutext.is_empty() {
commands.entity(menutext.single()).despawn();
}
next_state.set(GameState::Running);
next_state.set(GameState::Countdown);
}

#[derive(Component)]
pub struct Countdown {
pub frame_timer: Timer,
pub index: usize,
}

#[derive(Resource, Default)]
pub struct CountdownAssets {
pub images: [Handle<Image>; 4],
pub sound: Handle<AudioSource>,
pub go_sound: Handle<AudioSource>,
}

pub fn spawn_countdown_assets(
mut countdown_assets: ResMut<CountdownAssets>,
asset_server: ResMut<AssetServer>,
) {
countdown_assets.images = [
asset_server.load("cd1.png"),
asset_server.load("cd2.png"),
asset_server.load("cd3.png"),
asset_server.load("go.png"),
];
countdown_assets.sound = asset_server.load("countdown.wav");
countdown_assets.go_sound = asset_server.load("go3.wav");

}
pub fn start_countdown(
mut commands: Commands,
countdown_assets: Res<CountdownAssets>,
time: Res<Time>,
mut next_state: ResMut<NextState<GameState>>,
mut timer: Query<(&mut Countdown, &mut UiImage, Entity)>,
) {
if timer.is_empty() {
commands.spawn((
Countdown {
frame_timer: Timer::new(Duration::from_secs_f32(1.0), TimerMode::Repeating),
index: 0,
},
ImageBundle {
style: Style {
position_type: PositionType::Absolute,
margin: UiRect::horizontal(Val::Auto),
top: Val::Percent(25.0),
left: Val::Percent(33.0),
width: Val::Percent(34.0),
height: Val::Percent(50.0),
..Default::default()
},
image: UiImage {
texture: countdown_assets.images[0].clone(),
..default()
},
..default()
},
));
commands.spawn(AudioBundle {
source: countdown_assets.sound.clone(),
settings: PlaybackSettings::DESPAWN,
});

return;
}


if timer.single().0.frame_timer.paused() {
timer.single_mut().0.frame_timer.unpause();
}

timer.single_mut().0.frame_timer.tick(time.delta());

if timer.single().0.frame_timer.just_finished() {

if timer.single().0.index == 3 {
commands.entity(timer.single().2).despawn();
next_state.set(GameState::Running);
return;
}

timer.single_mut().0.index += 1;
timer.single_mut().1.texture = countdown_assets.images[timer.single().0.index].clone();
commands.spawn(AudioBundle {
source: match timer.single().0.index {
3 => countdown_assets.go_sound.clone(),
_ => countdown_assets.sound.clone(),
},
settings: PlaybackSettings::DESPAWN,
});
}

}
23 changes: 12 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ fn main() {
title: "Midnight Motorist".into(),
name: Some("Midnight Motorist".into()),
resolution: (1280.0, 720.0).into(),
// present_mode: PresentMode::AutoNoVsync,
enabled_buttons: bevy::window::EnabledButtons {
maximize: false,
..Default::default()
Expand All @@ -40,29 +39,31 @@ fn main() {
.add_systems(Startup, spawn_ui)
.add_systems(Startup, spawn_score)
.add_systems(Startup, spawn_camera)
//.add_systems(Startup, spawn_menu)
.add_systems(Update, update_menu)
.add_systems(Update, update_score.run_if(in_state(GameState::Running)))
.add_systems(Update, camera_tracking.run_if(in_state(GameState::Running)))
.add_systems(Update, update_laps.run_if(in_state(GameState::Running)))
.add_systems(Update, update_speed.run_if(in_state(GameState::Running)))

.insert_resource(Level { level: 1 })
.insert_resource(LevelAssets { ..default() })
.insert_state(GameState::LoadNextLevel)
//.add_systems(Startup, load_level.after(spawn_camera))
//.add_systems(Startup, (spawn_background, spawn_car, start_music).run_if(in_state(GameState::Running)))
.insert_resource(CountdownAssets { ..default() })
.add_systems(Startup, spawn_countdown_assets)

.insert_state(GameState::LoadNextLevel)
.add_systems(Update, despawn_level.run_if(in_state(GameState::Unloading)))
.add_systems(Update, spawn_loading_screen.run_if(in_state(GameState::LoadNextLevel)))
.add_systems(Update, load_level.run_if(in_state(GameState::LoadNextLevel)))
.add_systems(Update, (spawn_car, spawn_background, start_music).after(load_level).run_if(in_state(GameState::Loading)))
.add_systems(Update, (spawn_car, spawn_background).after(load_level).run_if(in_state(GameState::Loading)))
.add_systems(Update, despawn_loading_screen.run_if(in_state(GameState::Loading)))

.add_systems(Update, start_countdown.run_if(in_state(GameState::Countdown)))

.add_systems(Update, update_score.run_if(in_state(GameState::Running)))
.add_systems(Update, start_music.run_if(in_state(GameState::Running)))
.add_systems(Update, camera_tracking.run_if(in_state(GameState::Running)))
.add_systems(Update, update_laps.run_if(in_state(GameState::Running)))
.add_systems(Update, update_speed.run_if(in_state(GameState::Running)))
.add_systems(Update, update_background.run_if(in_state(GameState::Running)))
.add_systems(Update, update_car.run_if(in_state(GameState::Running)))
.add_systems(Update, update_obstacles.run_if(in_state(GameState::Running)))
.add_systems(Update, spawn_new_obstacles.after(update_obstacles))
.add_systems(Update, spawn_new_obstacles.after(update_obstacles).run_if(in_state(GameState::Running)))
.add_systems(Update, game_over.run_if(in_state(GameState::Running)))
.add_systems(Update, next_level.run_if(in_state(GameState::Running)))
//.add_systems(Update, detect_collision.run_if(in_state(GameState::Running)))
Expand Down
1 change: 1 addition & 0 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub enum GameState {
Paused,
Loading,
Unloading,
Countdown,
}

#[derive(Component)]
Expand Down
28 changes: 15 additions & 13 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::{prelude::*, time::Time};

use crate::{Car, LevelAssets, LevelAssetMarker};
use crate::{Car, LevelAssetMarker, LevelAssets};

#[derive(Component)]
pub struct CameraMarker;
Expand Down Expand Up @@ -52,7 +52,7 @@ pub fn spawn_background(
stretch_value: height / 1080.0,
},
Background,
LevelAssetMarker
LevelAssetMarker,
));
commands.spawn((
SpriteBundle {
Expand All @@ -74,7 +74,7 @@ pub fn spawn_background(
stretch_value: height / 1080.0,
},
Background,
LevelAssetMarker
LevelAssetMarker,
));
commands.spawn((
SpriteBundle {
Expand Down Expand Up @@ -136,7 +136,7 @@ pub fn update_background(
stretch_value: height / 1080.0,
},
Background,
LevelAssetMarker
LevelAssetMarker,
));
}
}
Expand Down Expand Up @@ -349,13 +349,15 @@ pub fn update_speed(
#[derive(Component)]
pub struct MusicMarker;

pub fn start_music(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
AudioBundle {
source: asset_server.load("music.mp3"),
settings: PlaybackSettings::LOOP,
},
MusicMarker,
LevelAssetMarker
));
pub fn start_music(mut commands: Commands, assets: Res<LevelAssets>, music: Query<&MusicMarker>) {
if music.is_empty() {
commands.spawn((
AudioBundle {
source: assets.music.clone(),
settings: PlaybackSettings::LOOP,
},
MusicMarker,
LevelAssetMarker,
));
}
}

0 comments on commit e851e6c

Please sign in to comment.