game states

This commit is contained in:
Christian Nieves
2024-12-12 14:57:21 -06:00
parent 12acaf0301
commit fbb48036b5
16 changed files with 675 additions and 1 deletions

77
src/bin/states.rs Normal file
View File

@ -0,0 +1,77 @@
use bevy::prelude::*;
#[derive(States, Clone, Copy, Eq, PartialEq, Debug, Hash)]
enum AppState {
Splash,
Menu,
Game,
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_state(AppState::Splash)
.add_systems(Startup, setup)
// define transitions :
.add_systems(OnEnter(AppState::Splash), setup_splash)
.add_systems(OnEnter(AppState::Menu), setup_menu)
.add_systems(Update, splash_screen.run_if(in_state(AppState::Splash)))
.run();
}
#[derive(Component)]
struct SplashScreenTimer(Timer);
#[derive(Component)]
struct GameFont(Handle<Font>);
fn splash_screen(
mut commands: Commands,
time: ResMut<Time>,
mut timer: Single<&mut SplashScreenTimer>,
text: Single<Entity, With<Text2d>>,
mut next_state: ResMut<NextState<AppState>>,
) {
if timer.0.tick(time.delta()).just_finished() {
commands.entity(text.into_inner()).despawn();
next_state.set(AppState::Menu);
}
}
fn setup_splash(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.load("fonts/monogram/ttf/monogram.ttf");
let text_font = TextFont {
font: font.clone(),
font_size: 50.0,
..default()
};
let text_justification = JustifyText::Center;
commands.spawn((
Text2d::new("Splash Screen lol"),
text_font,
TextLayout::new_with_justify(text_justification),
));
commands.spawn(SplashScreenTimer(Timer::from_seconds(2.0, TimerMode::Once)));
}
fn setup_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.load("fonts/monogram/ttf/monogram.ttf");
let text_font = TextFont {
font: font.clone(),
font_size: 50.0,
..default()
};
let text_justification = JustifyText::Center;
commands.spawn((
Text2d::new("Game Menu"),
text_font,
TextLayout::new_with_justify(text_justification),
));
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2d::default());
}

View File

@ -1,3 +1,5 @@
use bevy::prelude::*;
fn main() {
todo!();
App::new().add_plugins(DefaultPlugins).run();
}