Fixed formatting and started settings.json

main
Johannes Hendrik Gerard van der Weide 2023-11-30 13:24:53 +01:00
parent 8a77d1d9b8
commit a6e9a8345e
2 changed files with 61 additions and 37 deletions

1
data/settings.json Normal file
View File

@ -0,0 +1 @@
{"up":32,"down":28,"left":10,"right":13}

View File

@ -1,7 +1,15 @@
use bevy::prelude::*;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use std::fs;
#[derive(Component, Serialize, Deserialize)]
pub struct Settings {
pub up: u32,
pub down: u32,
pub left: u32,
pub right: u32,
}
#[derive(Component, Serialize, Deserialize)]
pub struct Player {
pub speed: f32,
@ -35,10 +43,26 @@ fn main() {
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
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_json5::from_str::<Player>(&json_file).expect("Error serializing to JSON");
let json_settings = Settings {
up: 32,
down: 28,
left: 10,
right: 13,
};
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 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_json5::from_str::<Player>(&json_file).expect("Error serializing to JSON");
commands.spawn((
SpriteBundle {
sprite: Sprite {
@ -52,18 +76,15 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
Player {
speed: player_data.speed,
movement: Vec2::new(player_data.movement[0], player_data.movement[1]),
direction: Vec2::new(0.0, 0.0),
dash: false
direction: Vec2::new(player_data.direction[0], player_data.direction[1]),
dash: player_data.dash,
},
));
}
//TODO: make it so keycodes can be changed (maby through JSON at first and later make a UI menu to edit said JSON)
fn get_input (
mut input_recievers: Query<(&mut Transform, &mut Player)>,
input: Res<Input<KeyCode>>,
) {
for (_transform, mut player) in &mut input_recievers {
fn get_input(mut input_recievers: Query<&mut Player>, input: Res<Input<KeyCode>>) {
for mut player in &mut input_recievers {
if input.just_pressed(KeyCode::W) {
player.direction.y += 1.0;
}
@ -94,12 +115,8 @@ fn get_input (
}
}
fn character_movement(
mut characters: Query<(&mut Transform, &mut Player)>,
time: Res<Time>,
) {
fn character_movement(mut characters: Query<(&mut Transform, &mut Player)>, time: Res<Time>) {
for (mut transform, mut player) in &mut characters {
let current_speed: f32 = player.speed;
let current_movement: Vec2 = player.movement;
@ -110,10 +127,16 @@ fn character_movement(
player.movement -= current_movement * current_speed;
}
if player.direction.x == 1.0 {
player.movement = Vec2::new(player.movement.x - player.movement.y * 0.1, player.movement.y + player.movement.x * 0.1);
player.movement = Vec2::new(
player.movement.x - player.movement.y * 0.1,
player.movement.y + player.movement.x * 0.1,
);
}
if player.direction.x == -1.0 {
player.movement = Vec2::new(player.movement.x + player.movement.y * 0.1, player.movement.y - player.movement.x * 0.1);
player.movement = Vec2::new(
player.movement.x + player.movement.y * 0.1,
player.movement.y - player.movement.x * 0.1,
);
}
//TODO: make locking the player within bounds not ass and use the screensize instead of magic numbers