YAML > JSON also writes .yml files if they don't exist yet

main
Johannes Hendrik Gerard van der Weide 2023-12-02 12:59:00 +01:00
parent cb0fb3d5b0
commit 253e1654d8
6 changed files with 62 additions and 31 deletions

View File

@ -12,4 +12,4 @@ readme = "README.md"
[dependencies] [dependencies]
bevy = "0.12.0" bevy = "0.12.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json5 = "0.1.0" serde_yaml = "0.9.27"

View File

@ -1,8 +0,0 @@
{
//These values go hard feel free to modify tho
"speed":0.3,
"movement":[10.0,0.0],
//Stay the fuck away from these, please I worked very hard on them :(
"direction":[0.0,0.0],
"dash":false
}

14
data/player_data.yml Normal file
View File

@ -0,0 +1,14 @@
#These values you can just go edit no problem
speed: 0.3
movement: [
10.0,
0.0
]
#These you should leave alone
direction: [
0.0,
0.0
]
dash: false

View File

@ -1 +0,0 @@
{"up":"W","down":"S","left":"A","right":"D"}

7
data/settings.yml Normal file
View File

@ -0,0 +1,7 @@
# Change these to whatever you want
up: W
down: S
left: A
right: D

View File

@ -1,6 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fs; use std::fs;
use std::path::Path;
#[derive(Component, Serialize, Deserialize)] #[derive(Component, Serialize, Deserialize)]
pub struct Settings { pub struct Settings {
@ -43,25 +44,43 @@ fn main() {
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default()); commands.spawn(Camera2dBundle::default());
let json_settings = Settings { let mut settings_yaml: String = "
up: "W".to_string(), # Change these to whatever you want
down: "S".to_string(), up: W
left: "A".to_string(), down: S
right: "D".to_string(), left: A
}; right: D
let _ = fs::write( ".to_string();
"./data/settings.json", let settings_file = Path::new("./data/settings.yml");
serde_json5::to_string(&json_settings).expect("Error writing settings"), if settings_file.exists() {
); settings_yaml = fs::read_to_string("./data/settings.yml").expect("Error reading data/settings.yml");
let settings_json = } else {
fs::read_to_string("./data/settings.json").expect("Error Reading Settings Data"); fs::write("./data/settings.yml", settings_yaml.clone()).expect("Error writing data/settings.yml");
let settings_data: Settings = }
serde_json5::from_str::<Settings>(&settings_json).expect("Error serializing to JSON"); let settings_data = serde_yaml::from_str::<Settings>(&settings_yaml).expect("Error serializing to YAML");
let json_file = //Default player_data
fs::read_to_string("./data/player_data.json").expect("Error Reading Player Data"); let mut player_data_yaml: String = "
let player_data: Player = #These values you can just go edit no problem
serde_json5::from_str::<Player>(&json_file).expect("Error serializing to JSON"); speed: 0.3
movement: [
10.0,
0.0
]
#These you should leave alone
direction: [
0.0,
0.0
]
dash: false
".to_string();
let player_data_file = Path::new("./data/player_data.yml");
if player_data_file.exists() {
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 = serde_yaml::from_str::<Player>(&player_data_yaml).expect("Error serializing to YAML");
commands.spawn(( commands.spawn((
SpriteBundle { SpriteBundle {
sprite: Sprite { sprite: Sprite {
@ -74,8 +93,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
//TODO: find out how to use vectors in json so I don't have to use this array fuckery //TODO: find out how to use vectors in json so I don't have to use this array fuckery
Player { Player {
speed: player_data.speed, speed: player_data.speed,
movement: Vec2::new(player_data.movement[0], player_data.movement[1]), movement: Vec2::new(player_data.movement.x, player_data.movement.y),
direction: Vec2::new(player_data.direction[0], player_data.direction[1]), direction: Vec2::new(player_data.direction.x, player_data.direction.y),
dash: player_data.dash, dash: player_data.dash,
}, },
)); ));