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