Input visual moved to it's own lil file :3
parent
3eae9f5896
commit
9f49fb4acc
101
src/main.rs
101
src/main.rs
|
@ -1,32 +1,14 @@
|
|||
use bevy::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
use crate::objects::player::Player;
|
||||
use crate::objects::player::PlayerPlugin;
|
||||
use crate::levelgen::LevelgenPlugin;
|
||||
use crate::setup::config_loader::load_yaml_folder;
|
||||
|
||||
#[derive(Component, Serialize, Deserialize)]
|
||||
pub struct Settings {
|
||||
pub up: String,
|
||||
pub down: String,
|
||||
pub left: String,
|
||||
pub right: String,
|
||||
pub input_lower_sprite_path: String,
|
||||
pub input_upper_sprite_path: String,
|
||||
pub input_sprite_size: Vec2,
|
||||
pub input_sprite_location: Vec2,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct InputVisual {
|
||||
pub location: Vec2,
|
||||
pub input_direction: Vec2,
|
||||
}
|
||||
use crate::user_interface::input_visual::InputVisualPlugin;
|
||||
|
||||
pub mod objects;
|
||||
pub mod setup;
|
||||
mod levelgen;
|
||||
mod user_interface;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
|
@ -45,79 +27,15 @@ fn main() {
|
|||
.build(),
|
||||
)
|
||||
.add_plugins(PlayerPlugin)
|
||||
.add_plugins(InputVisualPlugin)
|
||||
.add_plugins(LevelgenPlugin)
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, get_input)
|
||||
.add_systems(Update, draw_input)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
fn setup(mut commands: Commands) {
|
||||
commands.spawn(Camera2dBundle::default());
|
||||
|
||||
let settings_yaml_default: String = "
|
||||
# Change these to whatever you want
|
||||
up: W
|
||||
down: S
|
||||
left: A
|
||||
right: D
|
||||
input_lower_sprite_path: circle_empty.png
|
||||
input_upper_sprite_path: circle_full.png
|
||||
input_sprite_size: [
|
||||
30.0,
|
||||
30.0
|
||||
]
|
||||
input_sprite_location: [
|
||||
-200.0,
|
||||
-200.0
|
||||
]
|
||||
"
|
||||
.to_string();
|
||||
|
||||
let settings_yaml = load_yaml_folder(settings_yaml_default, Path::new("./data/settings"));
|
||||
|
||||
let settings_data =
|
||||
serde_yaml::from_str::<Settings>(&settings_yaml[0]).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,
|
||||
)),
|
||||
anchor:
|
||||
bevy::sprite::Anchor::Custom(Vec2::new(
|
||||
6.658,
|
||||
6.658,
|
||||
)),
|
||||
|
||||
..default()
|
||||
},
|
||||
texture: asset_server.load(settings_data.input_lower_sprite_path),
|
||||
..default()
|
||||
|
||||
}
|
||||
);
|
||||
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_upper_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),
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
//TODO: make it so keycodes can be changed (maby through JSON at first and later make a UI menu to edit said JSON)
|
||||
|
@ -159,14 +77,3 @@ fn get_input(mut input_recievers: Query<&mut Player>, input: Res<Input<KeyCode>>
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
pub mod input_visual;
|
|
@ -0,0 +1,110 @@
|
|||
use bevy::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::setup::config_loader::load_yaml_folder;
|
||||
use std::path::Path;
|
||||
use crate::Player;
|
||||
|
||||
pub struct InputVisualPlugin;
|
||||
|
||||
#[derive(Component, Serialize, Deserialize)]
|
||||
pub struct Settings {
|
||||
pub up: String,
|
||||
pub down: String,
|
||||
pub left: String,
|
||||
pub right: String,
|
||||
pub input_lower_sprite_path: String,
|
||||
pub input_upper_sprite_path: String,
|
||||
pub input_sprite_size: Vec2,
|
||||
pub input_sprite_location: Vec2,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct InputVisual {
|
||||
pub location: Vec2,
|
||||
pub input_direction: Vec2,
|
||||
}
|
||||
|
||||
impl Plugin for InputVisualPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, setup_visual)
|
||||
.add_systems(Update, draw_input);
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_visual(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let settings_yaml_default: String = "
|
||||
# Change these to whatever you want
|
||||
up: W
|
||||
down: S
|
||||
left: A
|
||||
right: D
|
||||
input_lower_sprite_path: circle_empty.png
|
||||
input_upper_sprite_path: circle_full.png
|
||||
input_sprite_size: [
|
||||
30.0,
|
||||
30.0
|
||||
]
|
||||
input_sprite_location: [
|
||||
-200.0,
|
||||
-200.0
|
||||
]
|
||||
"
|
||||
.to_string();
|
||||
|
||||
let settings_yaml = load_yaml_folder(settings_yaml_default, Path::new("./data/settings"));
|
||||
|
||||
let settings_data =
|
||||
serde_yaml::from_str::<Settings>(&settings_yaml[0]).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,
|
||||
)),
|
||||
anchor:
|
||||
bevy::sprite::Anchor::Custom(Vec2::new(
|
||||
6.658,
|
||||
6.658,
|
||||
)),
|
||||
|
||||
..default()
|
||||
},
|
||||
texture: asset_server.load(settings_data.input_lower_sprite_path),
|
||||
..default()
|
||||
|
||||
}
|
||||
);
|
||||
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_upper_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),
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue