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]
bevy = "0.12.0"
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 serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
#[derive(Component, Serialize, Deserialize)]
pub struct Settings {
@ -43,25 +44,43 @@ fn main() {
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
let json_settings = Settings {
up: "W".to_string(),
down: "S".to_string(),
left: "A".to_string(),
right: "D".to_string(),
};
let _ = fs::write(
"./data/settings.json",
serde_json5::to_string(&json_settings).expect("Error writing settings"),
);
let settings_json =
fs::read_to_string("./data/settings.json").expect("Error Reading Settings Data");
let settings_data: Settings =
serde_json5::from_str::<Settings>(&settings_json).expect("Error serializing to JSON");
let mut settings_yaml: String = "
# Change these to whatever you want
up: W
down: S
left: A
right: D
".to_string();
let settings_file = Path::new("./data/settings.yml");
if settings_file.exists() {
settings_yaml = fs::read_to_string("./data/settings.yml").expect("Error reading data/settings.yml");
} else {
fs::write("./data/settings.yml", settings_yaml.clone()).expect("Error writing data/settings.yml");
}
let settings_data = serde_yaml::from_str::<Settings>(&settings_yaml).expect("Error serializing to YAML");
let json_file =
fs::read_to_string("./data/player_data.json").expect("Error Reading Player Data");
let player_data: Player =
serde_json5::from_str::<Player>(&json_file).expect("Error serializing to JSON");
//Default player_data
let mut player_data_yaml: String = "
#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
".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((
SpriteBundle {
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
Player {
speed: player_data.speed,
movement: Vec2::new(player_data.movement[0], player_data.movement[1]),
direction: Vec2::new(player_data.direction[0], player_data.direction[1]),
movement: Vec2::new(player_data.movement.x, player_data.movement.y),
direction: Vec2::new(player_data.direction.x, player_data.direction.y),
dash: player_data.dash,
},
));