bevyjam/src/main.rs
2022-08-26 18:38:52 +08:00

109 lines
2.8 KiB
Rust

#[cfg(not(target_arch = "wasm32"))]
mod audio;
mod filters;
mod game;
mod levels;
mod menu;
mod particle_effect;
use bevy::{
prelude::*,
window::{WindowId, WindowMode},
};
use bevy_common_assets::json::JsonAssetPlugin;
use bevy_rapier2d::prelude::*;
#[cfg(not(target_arch = "wasm32"))]
use std::thread;
#[cfg(target_arch = "wasm32")]
use wasm_thread as thread;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum AppState {
Menu,
Game,
Win,
}
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(start)] pub fn dummy_main() {}
#[wasm_bindgen]
pub fn run() {
main();
}
fn main() {
let (audio_event_sender, audio_event_receiver) =
crossbeam_channel::bounded::<game::AudioMsg>(512);
#[cfg(not(target_arch = "wasm32"))]
thread::spawn(move || audio::setup(audio_event_receiver));
thread::spawn(|| {/* ... */});
#[cfg(not(target_arch = "wasm32"))]
let first_level = game::LevelId(
std::env::args()
.nth(1)
.map_or(0, |s| s.parse().unwrap_or(0)),
);
#[cfg(target_arch = "wasm32")]
let first_level = game::LevelId(0);
App::new()
.insert_resource(Msaa { samples: 4 })
.insert_resource(audio_event_sender)
.add_state(AppState::Menu)
.insert_resource(game::FirstLevel(first_level))
.insert_resource(ClearColor(Color::BLACK))
.add_plugins(DefaultPlugins)
.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)
.add_startup_system(setup)
.run();
}
fn setup(mut commands: Commands, mut windows: ResMut<Windows>, asset_server: Res<AssetServer>) {
windows
.get_mut(WindowId::primary())
.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"));
commands.spawn_bundle(Camera2dBundle::default());
commands.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 0.6,
});
}
fn keyboard_util_system(keyboard_input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
#[cfg(not(target_arch = "wasm32"))]
{
if keyboard_input.just_released(KeyCode::Escape) {
std::process::exit(0);
}
if keyboard_input.just_pressed(KeyCode::F11) {
if let Some(window) = windows.get_primary_mut() {
window.set_mode(match window.mode() {
WindowMode::Windowed => WindowMode::Fullscreen,
_ => WindowMode::Windowed,
});
}
}
}
}