use beetmash_scene::utils::CliSceneLoadPlugin;
use bevy::asset::AssetMetaCheck;
use bevy::prelude::*;
use bevy::window::WindowPlugin;
const DEFAULT_ASSET_PATH: &str = "assets";
pub struct BeetmashDefaultPlugins {
#[allow(unused)]
pub native_asset_path: String,
#[allow(unused)]
pub wasm_asset_path: String,
pub assert_local_assets: bool,
}
impl Default for BeetmashDefaultPlugins {
fn default() -> Self {
Self {
native_asset_path: DEFAULT_ASSET_PATH.into(),
wasm_asset_path: DEFAULT_ASSET_PATH.into(),
assert_local_assets: false,
}
}
}
impl Plugin for BeetmashDefaultPlugins {
fn build(&self, app: &mut App) {
self.assert_local_assets();
app.add_plugins((
DefaultPlugins
.set(beetmash_window_plugin())
.set(AssetPlugin {
file_path: self.asset_path(),
meta_check: AssetMetaCheck::Never,
..default()
})
.build(),
CliSceneLoadPlugin,
))
.add_systems(Update, close_on_esc);
}
}
impl BeetmashDefaultPlugins {
pub fn with_wasm_asset_path(wasm_asset_path: impl Into<String>) -> Self {
Self {
wasm_asset_path: wasm_asset_path.into(),
..default()
}
}
pub fn with_native_asset_path(path: impl Into<String>) -> Self {
Self {
native_asset_path: path.into(),
..default()
}
}
pub fn asset_path(&self) -> String {
#[cfg(target_arch = "wasm32")]
return self.wasm_asset_path.clone();
#[cfg(not(target_arch = "wasm32"))]
return self.native_asset_path.clone();
}
pub fn with_beetmash_assets() -> Self {
Self {
wasm_asset_path:
"https://beetmash-public.s3.us-west-2.amazonaws.com/assets"
.into(),
assert_local_assets: true,
..default()
}
}
pub fn assert_local_assets(&self) {
#[cfg(target_arch = "wasm32")]
return;
#[allow(unreachable_code)]
if self.assert_local_assets
&& !std::path::Path::new("assets/README.md").exists()
{
panic!(
r#"
🥁🥁🥁
Welcome! Beetmash examples use large assets that are stored remotely.
Unix:
curl -o ./assets.tar.gz https://beetmash-public.s3.us-west-2.amazonaws.com/assets.tar.gz
tar -xzvf ./assets.tar.gz
rm ./assets.tar.gz
Windows / manual:
1. Download https://beetmash-public.s3.us-west-2.amazonaws.com/assets.tar.gz
2. Unzip into `./assets`
🥁🥁🥁
"#
);
}
}
}
fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<ButtonInput<KeyCode>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}
if input.just_pressed(KeyCode::Escape) {
commands.entity(window).despawn();
}
}
}
pub fn beetmash_window_plugin() -> WindowPlugin {
WindowPlugin {
primary_window: Some(Window {
fit_canvas_to_parent: true,
canvas: Some("#beetmash-canvas".to_string()),
resizable: true,
..default()
}),
..default()
}
}