bevyjam/src/main.rs

148 lines
3.7 KiB
Rust
Raw Normal View History

2022-08-25 22:33:44 +00:00
#![allow(clippy::too_many_arguments)]
2022-08-24 09:31:17 +00:00
#[cfg(not(target_arch = "wasm32"))]
2022-08-23 15:02:13 +00:00
mod audio;
2022-08-25 22:33:44 +00:00
#[cfg(not(target_arch = "wasm32"))]
mod editor;
2022-08-24 10:21:08 +00:00
mod filters;
2022-08-21 17:17:55 +00:00
mod game;
2022-08-23 10:02:58 +00:00
mod levels;
2022-08-21 17:17:55 +00:00
mod menu;
2022-08-23 15:14:32 +00:00
mod particle_effect;
2022-08-21 17:17:55 +00:00
2022-08-25 06:20:13 +00:00
use bevy::{
2022-08-25 22:33:44 +00:00
asset::{Asset, HandleId, LoadState},
2022-08-25 06:20:13 +00:00
prelude::*,
window::{WindowId, WindowMode},
};
2022-08-25 17:55:34 +00:00
use bevy_common_assets::json::JsonAssetPlugin;
2022-08-21 17:17:55 +00:00
use bevy_rapier2d::prelude::*;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum AppState {
2022-08-25 22:33:44 +00:00
Loading,
2022-08-21 17:17:55 +00:00
Menu,
Game,
2022-08-23 10:02:58 +00:00
Win,
2022-08-25 22:33:44 +00:00
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
}
2022-08-21 17:17:55 +00:00
}
fn main() {
2022-08-24 09:31:17 +00:00
let (audio_event_sender, audio_event_receiver) =
crossbeam_channel::bounded::<game::AudioMsg>(512);
2022-08-23 15:02:13 +00:00
2022-08-24 09:31:17 +00:00
#[cfg(not(target_arch = "wasm32"))]
2022-08-23 15:02:13 +00:00
std::thread::spawn(move || audio::setup(audio_event_receiver));
2022-08-25 13:52:28 +00:00
#[cfg(not(target_arch = "wasm32"))]
2022-08-25 22:33:44 +00:00
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"),
)
};
2022-08-25 13:52:28 +00:00
#[cfg(target_arch = "wasm32")]
2022-08-25 22:33:44 +00:00
let (first_level, use_editor) = (game::LevelId(0), false);
2022-08-25 13:52:28 +00:00
2022-08-25 22:33:44 +00:00
let mut app = App::new();
app.insert_resource(Msaa { samples: 4 })
2022-08-23 15:02:13 +00:00
.insert_resource(audio_event_sender)
2022-08-25 22:33:44 +00:00
.insert_resource(UseEditor(use_editor))
.add_state(AppState::Loading)
2022-08-25 13:52:28 +00:00
.insert_resource(game::FirstLevel(first_level))
2022-08-24 10:21:08 +00:00
.insert_resource(ClearColor(Color::BLACK))
2022-08-21 17:17:55 +00:00
.add_plugins(DefaultPlugins)
2022-08-24 10:21:08 +00:00
//.add_plugin(RapierDebugRenderPlugin::default())
//.add_plugin(bevy_inspector_egui::WorldInspectorPlugin::new())
2022-08-25 22:33:44 +00:00
.add_plugin(JsonAssetPlugin::<levels::StoredLevels>::new(&[
"levels.json",
]));
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)
2022-08-21 17:17:55 +00:00
.add_startup_system(setup)
2022-08-25 22:33:44 +00:00
.add_system_set(SystemSet::on_update(AppState::Loading).with_system(loading_system))
2022-08-21 17:17:55 +00:00
.run();
}
2022-08-25 06:20:13 +00:00
fn setup(mut commands: Commands, mut windows: ResMut<Windows>, asset_server: Res<AssetServer>) {
windows
.get_mut(WindowId::primary())
.unwrap()
.set_title(String::from("Bevyjam"));
2022-08-25 22:33:44 +00:00
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")));
2022-08-26 21:54:49 +00:00
commands.insert_resource([
assets.add(asset_server.load::<Image, _>("bevy.png")),
assets.add(asset_server.load("melty.png")),
]);
2022-08-25 22:33:44 +00:00
commands.insert_resource(assets);
2022-08-25 07:30:47 +00:00
2022-08-24 10:21:08 +00:00
commands.spawn_bundle(Camera2dBundle::default());
2022-08-21 17:17:55 +00:00
commands.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 0.6,
});
}
2022-08-25 06:20:13 +00:00
2022-08-25 22:33:44 +00:00
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();
}
}
2022-08-25 06:20:13 +00:00
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,
});
}
}
}
}