Player data is now gotten from JSON (this is a suprise tool that will help us later :3)

main
Johannes Hendrik Gerard van der Weide 2023-11-28 14:50:34 +01:00
parent c243404bba
commit e752bcd86e
3 changed files with 13 additions and 3 deletions

View File

@ -5,3 +5,5 @@ edition = "2021"
[dependencies] [dependencies]
bevy = "0.12.0" bevy = "0.12.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

4
data/player_data.json Normal file
View File

@ -0,0 +1,4 @@
{
"speed":0.3,
"movement":[10.0,0.0]
}

View File

@ -1,6 +1,8 @@
use bevy::prelude::*; use bevy::prelude::*;
use serde::{Serialize, Deserialize};
use std::fs;
#[derive(Component)] #[derive(Component, Serialize, Deserialize)]
pub struct Player { pub struct Player {
pub speed: f32, pub speed: f32,
pub movement: Vec2, pub movement: Vec2,
@ -31,6 +33,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default()); commands.spawn(Camera2dBundle::default());
let texture = asset_server.load("blobcat_hertog.png"); let texture = asset_server.load("blobcat_hertog.png");
let json_file = fs::read_to_string("./data/player_data.json").expect("Error Reading Player Data");
let player_data: Player = serde_json::from_str::<Player>(&json_file).expect("Error serializing to JSON");
commands.spawn(( commands.spawn((
SpriteBundle { SpriteBundle {
@ -42,8 +46,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default() ..default()
}, },
Player { Player {
speed: 0.3, speed: player_data.speed,
movement: Vec2::new( 10.0, 0.0 ) movement: Vec2::new( player_data.movement[0], player_data.movement[1] )
}, },
)); ));
} }