Editor: first steps

This commit is contained in:
Pascal Engélibert 2022-08-26 00:33:44 +02:00
commit 09138229ca
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
8 changed files with 339 additions and 80 deletions

View file

@ -1,5 +1,9 @@
#![allow(clippy::too_many_arguments)]
#[cfg(not(target_arch = "wasm32"))]
mod audio;
#[cfg(not(target_arch = "wasm32"))]
mod editor;
mod filters;
mod game;
mod levels;
@ -7,6 +11,7 @@ mod menu;
mod particle_effect;
use bevy::{
asset::{Asset, HandleId, LoadState},
prelude::*,
window::{WindowId, WindowMode},
};
@ -15,9 +20,22 @@ use bevy_rapier2d::prelude::*;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum AppState {
Loading,
Menu,
Game,
Win,
Editor,
}
struct UseEditor(bool);
struct LoadingAssets(Vec<HandleId>);
impl LoadingAssets {
fn add<T: Asset>(&mut self, handle: Handle<T>) -> Handle<T> {
self.0.push(handle.id);
handle
}
}
fn main() {
@ -28,32 +46,45 @@ fn main() {
std::thread::spawn(move || audio::setup(audio_event_receiver));
#[cfg(not(target_arch = "wasm32"))]
let first_level = game::LevelId(
std::env::args()
.nth(1)
.map_or(0, |s| s.parse().unwrap_or(0)),
);
let (first_level, use_editor) = {
let mut args = std::env::args().skip(1);
(
game::LevelId(args.next().map_or(0, |s| s.parse().unwrap_or(0))),
args.next().map_or(false, |s| s == "e"),
)
};
#[cfg(target_arch = "wasm32")]
let first_level = game::LevelId(0);
let (first_level, use_editor) = (game::LevelId(0), false);
App::new()
.insert_resource(Msaa { samples: 4 })
let mut app = App::new();
app.insert_resource(Msaa { samples: 4 })
.insert_resource(audio_event_sender)
.add_state(AppState::Menu)
.insert_resource(UseEditor(use_editor))
.add_state(AppState::Loading)
.insert_resource(game::FirstLevel(first_level))
.insert_resource(ClearColor(Color::BLACK))
.add_plugins(DefaultPlugins)
//.add_plugin(RapierDebugRenderPlugin::default())
//.add_plugin(bevy_inspector_egui::WorldInspectorPlugin::new())
.add_plugin(JsonAssetPlugin::<levels::StoredLevels>::new(&[
"levels.json",
]))
.add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(64.0))
//.add_plugin(RapierDebugRenderPlugin::default())
.add_plugin(menu::MenuPlugin)
.add_plugin(game::GamePlugin)
.add_plugin(particle_effect::ParticleEffectPlugin)
//.add_plugin(bevy_inspector_egui::WorldInspectorPlugin::new())
.add_system(keyboard_util_system)
]));
if !use_editor {
app.add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(64.0))
.add_plugin(menu::MenuPlugin)
.add_plugin(game::GamePlugin)
.add_plugin(particle_effect::ParticleEffectPlugin);
}
#[cfg(not(target_arch = "wasm32"))]
if use_editor {
app.add_plugin(editor::EditorPlugin);
}
app.add_system(keyboard_util_system)
.add_startup_system(setup)
.add_system_set(SystemSet::on_update(AppState::Loading).with_system(loading_system))
.run();
}
@ -63,9 +94,13 @@ fn setup(mut commands: Commands, mut windows: ResMut<Windows>, asset_server: Res
.unwrap()
.set_title(String::from("Bevyjam"));
commands.insert_resource(asset_server.load::<levels::StoredLevels, _>("game.levels.json"));
commands.insert_resource(asset_server.load::<Font, _>("UacariLegacy-Thin.ttf"));
commands.insert_resource(asset_server.load::<Image, _>("bevy.png"));
let mut assets = LoadingAssets(Vec::new());
commands.insert_resource(
assets.add(asset_server.load::<levels::StoredLevels, _>("game.levels.json")),
);
commands.insert_resource(assets.add(asset_server.load::<Font, _>("UacariLegacy-Thin.ttf")));
commands.insert_resource(assets.add(asset_server.load::<Image, _>("bevy.png")));
commands.insert_resource(assets);
commands.spawn_bundle(Camera2dBundle::default());
commands.insert_resource(AmbientLight {
@ -74,6 +109,23 @@ fn setup(mut commands: Commands, mut windows: ResMut<Windows>, asset_server: Res
});
}
fn loading_system(
asset_server: Res<AssetServer>,
use_editor: Res<UseEditor>,
assets: Res<LoadingAssets>,
mut app_state: ResMut<State<AppState>>,
) {
if asset_server.get_group_load_state(assets.0.iter().copied()) == LoadState::Loaded {
app_state
.replace(if use_editor.0 {
AppState::Editor
} else {
AppState::Menu
})
.ok();
}
}
fn keyboard_util_system(keyboard_input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
#[cfg(not(target_arch = "wasm32"))]
{