Making progress toward 0.12 upgrade completion

main
Elijah C. Voigt 2 years ago
parent 0a79c8a53a
commit 837c345ffe

@ -1,3 +1,5 @@
use std::path::Path;
use crate::{ use crate::{
game::{Board, BoardIndex, Piece, Side}, game::{Board, BoardIndex, Piece, Side},
prelude::*, prelude::*,
@ -20,7 +22,7 @@ use bevy::{
render_resource::{TextureViewDescriptor, TextureViewDimension}, render_resource::{TextureViewDescriptor, TextureViewDimension},
view::ColorGrading, view::ColorGrading,
}, },
window::PrimaryWindow, window::PrimaryWindow, asset::AssetPath,
}; };
use serde::Deserialize; use serde::Deserialize;
@ -106,7 +108,7 @@ pub(crate) struct Display3d;
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct TilesComponent; struct TilesComponent;
#[derive(Debug, Resource)] #[derive(Debug, Resource, Clone)]
struct AssetsMap { struct AssetsMap {
models: Handle<Gltf>, models: Handle<Gltf>,
skybox: Handle<Image>, skybox: Handle<Image>,
@ -122,10 +124,9 @@ fn load_assets(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
tweaks: Res<Assets<Tweakfile>>, tweak_file: Res<crate::tweak::GameTweakfile>,
tweak_files: Res<Assets<Tweakfile>>,
) { ) {
let handle = server.load("martian.tweak.toml");
if let Some(tweak) = tweaks.get(&handle) {
let hitbox_shape = meshes.add(shape::Box::new(1.0, 0.1, 1.0).into()); let hitbox_shape = meshes.add(shape::Box::new(1.0, 0.1, 1.0).into());
let hitbox_material = materials.add(StandardMaterial { let hitbox_material = materials.add(StandardMaterial {
base_color: Color::NONE, base_color: Color::NONE,
@ -134,13 +135,16 @@ fn load_assets(
alpha_mode: AlphaMode::Blend, alpha_mode: AlphaMode::Blend,
..default() ..default()
}); });
commands.insert_resource(AssetsMap {
models: server.load(tweak.display3d.models.assets_file.as_str().clone()), let handle = server.load("martian.tweak.toml");
skybox: server.load(tweak.display3d.models.skybox_file.as_str().clone()), let tweak_file = tweak_files.get(handle).unwrap();
let asset_map = AssetsMap {
models: tweak_file.display3d.models.assets_handle.clone(),
skybox: tweak_file.display3d.models.skybox_handle.clone(),
hitbox_shape, hitbox_shape,
hitbox_material, hitbox_material,
}); };
} commands.insert_resource(asset_map.clone());
} }
/// Initialize the 3d board /// Initialize the 3d board
@ -866,7 +870,7 @@ fn switch_sides(
pub(crate) mod tweaks { pub(crate) mod tweaks {
use bevy::{ use bevy::{
core_pipeline::tonemapping::Tonemapping, pbr::ScreenSpaceAmbientOcclusionQualityLevel, core_pipeline::tonemapping::Tonemapping, pbr::ScreenSpaceAmbientOcclusionQualityLevel, asset::LoadContext,
}; };
use super::*; use super::*;
@ -889,6 +893,36 @@ pub(crate) mod tweaks {
pub lights: LightTweaks, pub lights: LightTweaks,
} }
impl Display3dTweaks {
pub fn load_dependencies(&mut self, load_context: &mut LoadContext) {
self.models.assets_handle = load_context.load(self.models.assets_file.clone());
self.models.skybox_handle = load_context.load(self.models.skybox_file.clone());
// self.models.scenes.queen_handle = load_context.load(self.models.scenes.queen.clone());
// self.models.scenes.drone_handle = load_context.load(self.models.scenes.drone.clone());
// self.models.scenes.pawn_handle = load_context.load(self.models.scenes.pawn.clone());
// self.models.scenes.board_handle = load_context.load(self.models.scenes.board.clone());
// self.models.scenes.valid_move_handle = load_context.load(self.models.scenes.valid_move.clone());
// self.models.animations.intro_a_handle = load_context.load(self.models.animations.intro_a.clone());
// self.models.animations.intro_b_handle = load_context.load(self.models.animations.xxx.clone());
// self.models.animations.turn_a_handle = load_context.load(self.models.animations.turn_a.clone());
// self.models.animations.turn_b_handle = load_context.load(self.models.animations.turn_b.clone());
// self.models.animations.pick_up_handle = load_context.load(self.models.animations.pick_up.clone());
// self.models.animations.put_down_handle = load_context.load(self.models.animations.put_down.clone());
// self.models.animations.idle_handle = load_context.load(self.models.animations.idle.clone());
// self.models.materials.queen_red_handle = load_context.load(self.models.materials.queen_red.clone());
// self.models.materials.queen_blue_handle = load_context.load(self.models.materials.queen_blue.clone());
// self.models.materials.drone_red_handle = load_context.load(self.models.materials.drone_red.clone());
// self.models.materials.drone_blue_handle = load_context.load(self.models.materials.drone_blue.clone());
// self.models.materials.pawn_red_handle = load_context.load(self.models.materials.xxx.clone());
// self.models.materials.pawn_blue_handle = load_context.load(self.models.materials.pawn_blue.clone());
// self.models.materials.dots_red_handle = load_context.load(self.models.materials.dots_red.clone());
// self.models.materials.dots_blue_handle = load_context.load(self.models.materials.dots_blue.clone());
}
}
#[derive(Debug, Deserialize, Default)] #[derive(Debug, Deserialize, Default)]
pub(crate) struct ColorTweaks { pub(crate) struct ColorTweaks {
#[serde(default)] #[serde(default)]
@ -1089,7 +1123,11 @@ pub(crate) mod tweaks {
#[derive(Debug, Deserialize, Default, Clone)] #[derive(Debug, Deserialize, Default, Clone)]
pub(crate) struct ModelTweaks { pub(crate) struct ModelTweaks {
pub assets_file: String, pub assets_file: String,
#[serde(skip)]
pub assets_handle: Handle<Gltf>,
pub skybox_file: String, pub skybox_file: String,
#[serde(skip)]
pub skybox_handle: Handle<Image>,
pub scenes: SceneTweaks, pub scenes: SceneTweaks,
pub animations: AnimationTweaks, pub animations: AnimationTweaks,
pub materials: MaterialTweaks, pub materials: MaterialTweaks,
@ -1098,33 +1136,73 @@ pub(crate) mod tweaks {
#[derive(Debug, Deserialize, Default, Clone)] #[derive(Debug, Deserialize, Default, Clone)]
pub(crate) struct SceneTweaks { pub(crate) struct SceneTweaks {
pub queen: String, pub queen: String,
// #[serde(skip)]
// pub queen_handle: Handle<Scene>,
pub drone: String, pub drone: String,
// #[serde(skip)]
// pub drone_handle: Handle<Scene>,
pub pawn: String, pub pawn: String,
// #[serde(skip)]
// pub pawn_handle: Handle<Scene>,
pub board: String, pub board: String,
// #[serde(skip)]
// pub board_handle: Handle<Scene>,
pub valid_move: String, pub valid_move: String,
// #[serde(skip)]
// pub valid_move_handle: Handle<Scene>,
} }
#[derive(Debug, Deserialize, Default, Clone)] #[derive(Debug, Deserialize, Default, Clone)]
pub(crate) struct AnimationTweaks { pub(crate) struct AnimationTweaks {
pub intro_a: String, pub intro_a: String,
// #[serde(skip)]
// pub intro_a_handle: Handle<AnimationClip>,
pub intro_b: String, pub intro_b: String,
// #[serde(skip)]
// pub intro_b_handle: Handle<AnimationClip>,
pub turn_a: String, pub turn_a: String,
// #[serde(skip)]
// pub turn_a_handle: Handle<AnimationClip>,
pub turn_b: String, pub turn_b: String,
// #[serde(skip)]
// pub turn_b_handle: Handle<AnimationClip>,
pub pick_up: String, pub pick_up: String,
// #[serde(skip)]
// pub pick_up_handle: Handle<AnimationClip>,
pub put_down: String, pub put_down: String,
// #[serde(skip)]
// pub put_down_handle: Handle<AnimationClip>,
pub idle: String, pub idle: String,
// #[serde(skip)]
// pub idle_handle: Handle<AnimationClip>,
} }
#[derive(Debug, Deserialize, Default, Clone)] #[derive(Debug, Deserialize, Default, Clone)]
pub(crate) struct MaterialTweaks { pub(crate) struct MaterialTweaks {
pub queen_red: String, pub queen_red: String,
// #[serde(skip)]
// pub queen_red_handle: Handle<StandardMaterial>,
pub queen_blue: String, pub queen_blue: String,
// #[serde(skip)]
// pub queen_blue_handle: Handle<StandardMaterial>,
pub drone_red: String, pub drone_red: String,
// #[serde(skip)]
// pub drone_red_handle: Handle<StandardMaterial>,
pub drone_blue: String, pub drone_blue: String,
// #[serde(skip)]
// pub drone_blue_handle: Handle<StandardMaterial>,
pub pawn_red: String, pub pawn_red: String,
// #[serde(skip)]
// pub pawn_red_handle: Handle<StandardMaterial>,
pub pawn_blue: String, pub pawn_blue: String,
// #[serde(skip)]
// pub pawn_blue_handle: Handle<StandardMaterial>,
pub dots_red: String, pub dots_red: String,
// #[serde(skip)]
// pub dots_red_handle: Handle<StandardMaterial>,
pub dots_blue: String, pub dots_blue: String,
// #[serde(skip)]
// pub dots_blue_handle: Handle<StandardMaterial>,
} }
#[derive(Debug, Deserialize, Default, Clone)] #[derive(Debug, Deserialize, Default, Clone)]

@ -67,6 +67,8 @@ fn loading(
.ids() .ids()
.all(|id| server.is_loaded_with_dependencies(id)); .all(|id| server.is_loaded_with_dependencies(id));
info!("s {} g {} t {}", s, g, t);
if s && g && t { if s && g && t {
next_state.set(GameState::Menu) next_state.set(GameState::Menu)
} }

@ -2,7 +2,7 @@ use crate::prelude::*;
use bevy::asset::AsyncReadExt; use bevy::asset::AsyncReadExt;
use bevy::{ use bevy::{
asset::{io::Reader, AssetLoader, LoadContext}, asset::{io::Reader, AssetLoader, LoadContext},
reflect::{TypePath, TypeUuid}, reflect::TypePath,
utils::BoxedFuture, utils::BoxedFuture,
}; };
use serde::Deserialize; use serde::Deserialize;
@ -17,23 +17,23 @@ impl Plugin for TweakPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::Loading), load_tweakfile); app.add_systems(OnEnter(GameState::Loading), load_tweakfile);
app.register_asset_loader(TweakfileLoader) app.init_asset::<Tweakfile>().register_asset_loader(TweakfileLoader);
.init_asset::<Tweakfile>();
} }
} }
fn load_tweakfile(server: Res<AssetServer>, mut commands: Commands) { fn load_tweakfile(server: Res<AssetServer>, mut commands: Commands) {
let handle: Handle<Tweakfile> = server.load("martian.tweak.toml"); let handle: Handle<Tweakfile> = server.load("martian.tweak.toml");
commands.insert_resource(GameTweakfile(handle)); commands.insert_resource(GameTweakfile { handle });
} }
#[derive(Debug, Resource)] #[derive(Debug, Resource)]
struct GameTweakfile(Handle<Tweakfile>); pub(crate) struct GameTweakfile {
pub handle: Handle<Tweakfile>,
}
/// Tweakfile contains tweaks made to other parts of the game /// Tweakfile contains tweaks made to other parts of the game
#[derive(Debug, Deserialize, TypeUuid, TypePath, Asset)] #[derive(Debug, Deserialize, TypePath, Asset)]
#[uuid = "e5768efe-edce-4267-bdf4-dd8f8ca613c7"] pub struct Tweakfile {
pub(crate) struct Tweakfile {
#[serde(default)] #[serde(default)]
pub audio: audio::AudioTweaks, pub audio: audio::AudioTweaks,
#[serde(default)] #[serde(default)]
@ -46,7 +46,7 @@ pub(crate) struct Tweakfile {
pub struct TweakfileLoader; pub struct TweakfileLoader;
#[derive(Debug, Error)] #[derive(Debug, Error)]
enum TweakfileError { pub enum TweakfileError {
#[error("Failed to read file")] #[error("Failed to read file")]
IO(#[from] std::io::Error), IO(#[from] std::io::Error),
#[error("Failed to decode file")] #[error("Failed to decode file")]
@ -64,13 +64,14 @@ impl AssetLoader for TweakfileLoader {
&'a self, &'a self,
reader: &'a mut Reader, reader: &'a mut Reader,
_settings: &'a Self::Settings, _settings: &'a Self::Settings,
_load_context: &'a mut LoadContext, load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> { ) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move { Box::pin(async move {
let mut bytes = Vec::new(); let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?; reader.read_to_end(&mut bytes).await?;
let s = std::str::from_utf8(bytes.as_slice())?; let s = std::str::from_utf8(bytes.as_slice())?;
let result = toml::from_str::<Tweakfile>(s)?; let mut result = toml::from_str::<Tweakfile>(s)?;
result.display3d.load_dependencies(load_context);
Ok(result) Ok(result)
}) })
} }

Loading…
Cancel
Save