Skip to content

Commit

Permalink
feat: basic first app with boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Sycrosity committed Sep 1, 2023
1 parent b292f55 commit da92c7d
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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";
67 changes: 67 additions & 0 deletions src/loading.rs
Original file line number Diff line number Diff line change
@@ -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::<SplashScreen>.
// in_schedule(OnExit(EngineState::LoadingAssets)), );
}
}

// #[derive(Component)]
// struct SplashScreen;

fn load_assets(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut game_state: ResMut<NextState<EngineState>>,
) {
// 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<AssetServer>) {

// 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()
// });
// });
// }
37 changes: 32 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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::<EngineState>()
.add_state::<GameState>()
.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::<EngineState>::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();
}
7 changes: 7 additions & 0 deletions src/player.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::prelude::*;

pub struct GalaxyPlayerPlugin;

impl Plugin for GalaxyPlayerPlugin {
fn build(&self, app: &mut App) {}
}
7 changes: 7 additions & 0 deletions src/polar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::prelude::*;

pub struct GalaxyPolarPlugin;

impl Plugin for GalaxyPolarPlugin {
fn build(&self, app: &mut App) {}
}
16 changes: 16 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -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,
}
9 changes: 9 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
for entity in &to_despawn {
commands.entity(entity).despawn_recursive();
}
}
30 changes: 30 additions & 0 deletions src/world.rs
Original file line number Diff line number Diff line change
@@ -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::<Loaded>.in_schedule(OnExit(EngineState::InGame)),
));
}
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(500., 500.)),
..default()
},
texture: asset_server.load("planets/planets/planet00.png"),
..default()
},
Loaded,
));
}

0 comments on commit da92c7d

Please sign in to comment.