From da92c7d2ebec3bd093882b62209c07987d4c7482 Mon Sep 17 00:00:00 2001 From: Sycrosity <72102935+Sycrosity@users.noreply.github.com> Date: Thu, 8 Jun 2023 19:16:17 +0100 Subject: [PATCH] feat: basic first app with boilerplate --- src/lib.rs | 27 ++++++++++++++++++++ src/loading.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 37 ++++++++++++++++++++++++---- src/player.rs | 7 ++++++ src/polar.rs | 7 ++++++ src/state.rs | 16 ++++++++++++ src/utils.rs | 9 +++++++ src/world.rs | 30 ++++++++++++++++++++++ 8 files changed, 195 insertions(+), 5 deletions(-) create mode 100644 src/lib.rs create mode 100644 src/loading.rs create mode 100644 src/player.rs create mode 100644 src/polar.rs create mode 100644 src/state.rs create mode 100644 src/utils.rs create mode 100644 src/world.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..16dc227 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,27 @@ +#![allow(unused)] + +pub mod loading; +pub mod player; +pub mod polar; +pub mod state; +pub mod utils; +pub mod world; + +pub mod prelude { + + pub use std::f32::consts::*; + + pub use anyhow::{anyhow, bail, ensure, Result}; + pub use bevy::prelude::*; + + pub use crate::loading::*; + pub use crate::player::*; + pub use crate::polar::*; + pub use crate::state::*; + pub use crate::utils::*; + pub use crate::world::*; + pub use crate::{PLANETS, PLANET_PARTS}; +} + +pub const PLANETS: &str = "planets/planets"; +pub const PLANET_PARTS: &str = "planets/parts"; diff --git a/src/loading.rs b/src/loading.rs new file mode 100644 index 0000000..2e46e67 --- /dev/null +++ b/src/loading.rs @@ -0,0 +1,67 @@ +use crate::prelude::*; + +///Will be used to load assets when the game starts, so they are all pre-loaded +/// before the game starts. +pub struct GalaxyLoadingPlugin; + +impl Plugin for GalaxyLoadingPlugin { + fn build(&self, app: &mut App) { + app.add_system( + load_assets + .in_schedule(OnEnter(EngineState::LoadingAssets)) + .run_if(run_once()), + ); + // // While in this state, run the `countdown` system + // .add_system(splash_screen. + // in_set(OnUpdate(EngineState::LoadingAssets))) // When exiting + // the state, despawn everything that was spawned for this screen + // .add_system( + // teardown::. + // in_schedule(OnExit(EngineState::LoadingAssets)), ); + } +} + +// #[derive(Component)] +// struct SplashScreen; + +fn load_assets( + mut commands: Commands, + asset_server: Res, + mut game_state: ResMut>, +) { + // asset_server.load("planets/planets/planet09.png"); + + commands.spawn(Camera2dBundle::default()); + + game_state.set(EngineState::InGame); +} + +// fn splash_screen(mut commands: Commands, asset_server: Res) { + +// let icon = asset_server.load("planets/planets/planet09.png"); + +// commands +// .spawn(( +// NodeBundle { +// style: Style { +// align_items: AlignItems::Center, +// justify_content: JustifyContent::Center, +// size: Size::new(Val::Percent(100.0), +// Val::Percent(100.0)), ..default() +// }, +// ..default() +// }, +// SplashScreen, +// )) +// .with_children(|parent| { +// parent.spawn(ImageBundle { +// style: Style { +// // This will set the logo to be 200px wide, and auto +// adjust its height size: Size::new(Val::Px(200.0), +// Val::Auto), ..default() +// }, +// image: UiImage::new(icon), +// ..default() +// }); +// }); +// } diff --git a/src/main.rs b/src/main.rs index 15fd92b..587ec9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,36 @@ -use bevy::prelude::*; +use bevy_inspector_egui::quick::{StateInspectorPlugin, WorldInspectorPlugin}; +use galaxy::prelude::*; fn main() { - App::new().add_system(hello_world).run(); -} + App::new() + .insert_resource(ClearColor(Color::BLACK)) + .add_state::() + .add_state::() + .add_plugins( + DefaultPlugins + .set(WindowPlugin { + primary_window: Some(Window { + title: "Cosmic Crew: Galaxy".to_string(), + fit_canvas_to_parent: true, + ..default() + }), -fn hello_world() { - println!("hello world!"); + ..default() + }) + .set(AssetPlugin { + watch_for_changes: true, + ..default() + }), + ) + .add_plugin(WorldInspectorPlugin::default().run_if( + bevy::input::common_conditions::input_toggle_active(true, KeyCode::Slash), + )) + .add_plugin(StateInspectorPlugin::::default().run_if( + bevy::input::common_conditions::input_toggle_active(true, KeyCode::Slash), + )) + .add_plugin(GalaxyPlayerPlugin) + .add_plugin(GalaxyLoadingPlugin) + .add_plugin(GalaxyPolarPlugin) + .add_plugin(GalaxyWorldPlugin) + .run(); } diff --git a/src/player.rs b/src/player.rs new file mode 100644 index 0000000..4bef43e --- /dev/null +++ b/src/player.rs @@ -0,0 +1,7 @@ +use crate::prelude::*; + +pub struct GalaxyPlayerPlugin; + +impl Plugin for GalaxyPlayerPlugin { + fn build(&self, app: &mut App) {} +} diff --git a/src/polar.rs b/src/polar.rs new file mode 100644 index 0000000..2f172d0 --- /dev/null +++ b/src/polar.rs @@ -0,0 +1,7 @@ +use crate::prelude::*; + +pub struct GalaxyPolarPlugin; + +impl Plugin for GalaxyPolarPlugin { + fn build(&self, app: &mut App) {} +} diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..dec1f24 --- /dev/null +++ b/src/state.rs @@ -0,0 +1,16 @@ +use crate::prelude::*; + +#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States, Reflect)] +pub enum EngineState { + #[default] + LoadingAssets, + // MainMenu, + InGame, +} + +#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States, Reflect)] +pub enum GameState { + #[default] + Playing, + Paused, +} diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..4110c1b --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,9 @@ +use crate::prelude::*; + +///generic system that takes a component as a parameter, and will despawn +/// (teardown) all entities with that component +pub fn teardown(to_despawn: Query>, mut commands: Commands) { + for entity in &to_despawn { + commands.entity(entity).despawn_recursive(); + } +} diff --git a/src/world.rs b/src/world.rs new file mode 100644 index 0000000..6c0b928 --- /dev/null +++ b/src/world.rs @@ -0,0 +1,30 @@ +use crate::prelude::*; + +pub struct GalaxyWorldPlugin; + +#[derive(Component)] +pub struct Loaded; + +impl Plugin for GalaxyWorldPlugin { + fn build(&self, app: &mut App) { + app.add_systems(( + setup.in_schedule(OnEnter(EngineState::InGame)), + // game.in_set(OnUpdate(GameState::Game)), + teardown::.in_schedule(OnExit(EngineState::InGame)), + )); + } +} + +fn setup(mut commands: Commands, asset_server: Res) { + commands.spawn(( + SpriteBundle { + sprite: Sprite { + custom_size: Some(Vec2::new(500., 500.)), + ..default() + }, + texture: asset_server.load("planets/planets/planet00.png"), + ..default() + }, + Loaded, + )); +}