From af393f6a30c684ea6d1cf57861ca5fc06f1170c4 Mon Sep 17 00:00:00 2001 From: Johannes Hendrik Gerard van der Weide Date: Wed, 6 Dec 2023 13:32:16 +0100 Subject: [PATCH] Displayer input --- data/settings.yml | 11 +++++++- src/main.rs | 66 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/data/settings.yml b/data/settings.yml index 0c51f15..48949c8 100644 --- a/data/settings.yml +++ b/data/settings.yml @@ -3,5 +3,14 @@ up: W down: S left: A - right: D + right: D + input_sprite_path: neocat.png + input_sprite_size: [ + 30.0, + 30.0 + ] + input_sprite_location: [ + -200.0, + -200.0 + ] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8cbcb9a..be39315 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,9 @@ pub struct Settings { pub down: String, pub left: String, pub right: String, + pub input_sprite_path: String, + pub input_sprite_size: Vec2, + pub input_sprite_location: Vec2, } #[derive(Serialize, Deserialize)] @@ -30,6 +33,12 @@ pub struct Player { pub dash_modifier: f32, } +#[derive(Component)] +pub struct InputVisual { + pub location: Vec2, + pub input_direction: Vec2, +} + fn main() { App::new() .add_plugins( @@ -48,6 +57,7 @@ fn main() { ) .add_systems(Startup, setup) .add_systems(Update, get_input) + .add_systems(Update, draw_input) .add_systems(Update, character_movement) .run(); } @@ -60,7 +70,16 @@ fn setup(mut commands: Commands, asset_server: Res) { up: W down: S left: A - right: D + right: D + input_sprite_path: neocat.png + input_sprite_size: [ + 30.0, + 30.0 + ] + input_sprite_location: [ + -200.0, + -200.0 + ] " .to_string(); let settings_file = Path::new("./data/settings.yml"); @@ -74,6 +93,27 @@ fn setup(mut commands: Commands, asset_server: Res) { let settings_data = serde_yaml::from_str::(&settings_yaml).expect("Error serializing to YAML"); + commands.spawn(( + SpriteBundle { + sprite: Sprite { + custom_size: Some(Vec2::new( + settings_data.input_sprite_size.x, + settings_data.input_sprite_size.y, + )), + ..default() + }, + texture: asset_server.load(settings_data.input_sprite_path), + ..default() + }, + InputVisual { + location: Vec2::new( + settings_data.input_sprite_location.x, + settings_data.input_sprite_location.y, + ), + input_direction: Vec2::new(0.0, 0.0), + }, + )); + //Default player_data let mut player_data_yaml: String = " #These values you can just go edit no problem @@ -162,6 +202,17 @@ fn get_input(mut input_recievers: Query<&mut Player>, input: Res> } } +fn draw_input(mut visuals: Query<(&mut Transform, &mut InputVisual)>, input: Query<&mut Player>) { + for player in &input { + for (mut transform, mut visual) in &mut visuals { + visual.input_direction.x = player.direction.x; + visual.input_direction.y = player.direction.y; + transform.translation.x = visual.location.x + (visual.input_direction.x * 10.0); + transform.translation.y = visual.location.y + (visual.input_direction.y * 10.0); + } + } +} + fn character_movement(mut characters: Query<(&mut Transform, &mut Player)>, time: Res