Loading in YAML files is now per folder and from one function

Development
Johannes Hendrik Gerard van der Weide 2023-12-12 16:49:54 +01:00
parent f05a7268bd
commit b8e067f4e4
5 changed files with 27 additions and 12 deletions

View File

@ -22,6 +22,7 @@ pub struct InputVisual {
pub input_direction: Vec2, pub input_direction: Vec2,
} }
pub mod setup;
mod player; mod player;
fn main() { fn main() {

View File

@ -1,7 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Serialize, Deserialize};
use std::fs;
use std::path::Path; use std::path::Path;
use crate::setup::config_loader::load_yaml_folder;
pub struct PlayerPlugin; pub struct PlayerPlugin;
@ -33,7 +33,7 @@ pub struct Player {
fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) { fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
//Default player_data //Default player_data
let mut player_data_yaml: String = " let player_data_default: String = "
#These values you can just go edit no problem #These values you can just go edit no problem
sprite_path: blobcat_hertog.png sprite_path: blobcat_hertog.png
speed: 75.0 speed: 75.0
@ -50,16 +50,11 @@ fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
dash_modifier: 0.3 dash_modifier: 0.3
" "
.to_string(); .to_string();
let player_data_file = Path::new("./data/player_data.yml");
if player_data_file.exists() { let player_data_yaml: Vec<String> = load_yaml_folder(player_data_default, Path::new("./data/player"));
player_data_yaml = fs::read_to_string("./data/player_data.yml")
.expect("Error reading data/player_data.yml");
} else {
fs::write("./data/player_data.yml", player_data_yaml.clone())
.expect("Error writing data/player_data.yml");
}
let player_data = let player_data =
serde_yaml::from_str::<PlayerYAML>(&player_data_yaml).expect("Error serializing to YAML"); serde_yaml::from_str::<PlayerYAML>(&player_data_yaml[0]).expect("Error serializing to YAML");
commands.spawn(( commands.spawn((
SpriteBundle { SpriteBundle {
sprite: Sprite { sprite: Sprite {

1
src/setup.rs Normal file
View File

@ -0,0 +1 @@
pub mod config_loader;

View File

@ -0,0 +1,18 @@
use std::fs;
use std::path::Path;
pub fn load_yaml_folder(default: String, folder: &Path) -> Vec<String> {
let folder_path = fs::read_dir(folder).unwrap();
let mut return_yaml: Vec<String> = Vec::new();
for file in folder_path {
if file.as_ref().unwrap().path().with_extension("yml").exists() {
let yaml_file = fs::read_to_string(file.unwrap().path()).expect("Error reading config file");
return_yaml.push(yaml_file);
}
}
if return_yaml.capacity() < 1 {
return_yaml.push(default);
}
return return_yaml;
}