Input is collected in its own function to make things easier later
parent
e752bcd86e
commit
3609505057
|
@ -1,4 +1,6 @@
|
||||||
{
|
{
|
||||||
"speed":0.3,
|
"speed":0.3,
|
||||||
"movement":[10.0,0.0]
|
"movement":[10.0,0.0],
|
||||||
|
"direction":[0.0,0.0],
|
||||||
|
"dash":false
|
||||||
}
|
}
|
||||||
|
|
69
src/main.rs
69
src/main.rs
|
@ -6,6 +6,8 @@ use std::fs;
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
pub speed: f32,
|
pub speed: f32,
|
||||||
pub movement: Vec2,
|
pub movement: Vec2,
|
||||||
|
pub direction: Vec2,
|
||||||
|
pub dash: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -25,6 +27,7 @@ fn main() {
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.add_systems(Startup, setup)
|
.add_systems(Startup, setup)
|
||||||
|
.add_systems(Update, get_input)
|
||||||
.add_systems(Update, character_movement)
|
.add_systems(Update, character_movement)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
@ -39,22 +42,60 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
SpriteBundle {
|
SpriteBundle {
|
||||||
sprite: Sprite {
|
sprite: Sprite {
|
||||||
custom_size: Some(Vec2::new(100.0, 100.0)),
|
custom_size: Some(Vec2::new(50.0, 50.0)),
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
texture,
|
texture,
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
|
//TODO: find out how to use vectors in json so I don't have to use this array fuckery
|
||||||
Player {
|
Player {
|
||||||
speed: player_data.speed,
|
speed: player_data.speed,
|
||||||
movement: Vec2::new( player_data.movement[0], player_data.movement[1] )
|
movement: Vec2::new(player_data.movement[0], player_data.movement[1]),
|
||||||
|
direction: Vec2::new(0.0, 0.0),
|
||||||
|
dash: false
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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 {
|
||||||
|
if input.just_pressed(KeyCode::W) {
|
||||||
|
player.direction.y += 1.0;
|
||||||
|
}
|
||||||
|
if input.just_released(KeyCode::W) {
|
||||||
|
player.direction.y -= 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if input.just_pressed(KeyCode::S) {
|
||||||
|
player.direction.y -= 1.0;
|
||||||
|
}
|
||||||
|
if input.just_released(KeyCode::S) {
|
||||||
|
player.direction.y += 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if input.just_pressed(KeyCode::D) {
|
||||||
|
player.direction.x += 1.0;
|
||||||
|
}
|
||||||
|
if input.just_released(KeyCode::D) {
|
||||||
|
player.direction.x -= 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if input.just_pressed(KeyCode::A) {
|
||||||
|
player.direction.x -= 1.0;
|
||||||
|
}
|
||||||
|
if input.just_released(KeyCode::A) {
|
||||||
|
player.direction.x += 1.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn character_movement(
|
fn character_movement(
|
||||||
mut characters: Query<(&mut Transform, &mut Player)>,
|
mut characters: Query<(&mut Transform, &mut Player)>,
|
||||||
input: Res<Input<KeyCode>>,
|
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
for (mut transform, mut player) in &mut characters {
|
for (mut transform, mut player) in &mut characters {
|
||||||
|
@ -62,20 +103,30 @@ fn character_movement(
|
||||||
let current_speed: f32 = player.speed;
|
let current_speed: f32 = player.speed;
|
||||||
let current_movement: Vec2 = player.movement;
|
let current_movement: Vec2 = player.movement;
|
||||||
|
|
||||||
if input.pressed(KeyCode::W) {
|
if player.direction.y == 1.0 {
|
||||||
player.movement += current_movement * current_speed;
|
player.movement += current_movement * current_speed;
|
||||||
}
|
}
|
||||||
if input.pressed(KeyCode::S) {
|
if player.direction.y == -1.0 {
|
||||||
player.movement -= current_movement * current_speed;
|
player.movement -= current_movement * current_speed;
|
||||||
}
|
}
|
||||||
if input.pressed(KeyCode::D) {
|
if player.direction.x == 1.0 {
|
||||||
player.movement = Vec2::new(player.movement.x - player.movement.y, player.movement.y + player.movement.x);
|
player.movement = Vec2::new(player.movement.x - player.movement.y * 0.1, player.movement.y + player.movement.x * 0.1);
|
||||||
}
|
}
|
||||||
if input.pressed(KeyCode::A) {
|
if player.direction.x == -1.0 {
|
||||||
player.movement = Vec2::new(player.movement.x + player.movement.y, player.movement.y - player.movement.x);
|
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
|
||||||
|
if transform.translation.x < 250.0 && transform.translation.x > -250.0 {
|
||||||
transform.translation.x += player.movement.x * time.delta_seconds();
|
transform.translation.x += player.movement.x * time.delta_seconds();
|
||||||
|
} else {
|
||||||
|
player.movement.x = -player.movement.x;
|
||||||
|
}
|
||||||
|
if transform.translation.y < 250.0 && transform.translation.y > -250.0 {
|
||||||
transform.translation.y += player.movement.y * time.delta_seconds();
|
transform.translation.y += player.movement.y * time.delta_seconds();
|
||||||
|
} else {
|
||||||
|
player.movement.y = -player.movement.y;
|
||||||
|
}
|
||||||
|
info!("Player is at {:#?} !", transform);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue