WHEEEeeeee but now in Rust :333

main
Johannes Hendrik Gerard van der Weide 2023-11-26 18:23:41 +01:00
parent e29be70a93
commit 9022bd5007
1 changed files with 33 additions and 14 deletions

View File

@ -1,5 +1,11 @@
use bevy::prelude::*; use bevy::prelude::*;
#[derive(Component)]
pub struct Player {
pub speed: f32,
pub movement: Vec2,
}
fn main() { fn main() {
App::new() App::new()
.add_plugins( .add_plugins(
@ -26,33 +32,46 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let texture = asset_server.load("blobcat_hertog.png"); let texture = asset_server.load("blobcat_hertog.png");
commands.spawn(SpriteBundle { commands.spawn((
SpriteBundle {
sprite: Sprite { sprite: Sprite {
custom_size: Some(Vec2::new(100.0, 100.0)), custom_size: Some(Vec2::new(100.0, 100.0)),
..default() ..default()
}, },
texture, texture,
..default() ..default()
}); },
Player {
speed: 0.3,
movement: Vec2::new( 10.0, 0.0 )
},
));
} }
fn character_movement( fn character_movement(
mut characters: Query<(&mut Transform, &Sprite)>, mut characters: Query<(&mut Transform, &mut Player)>,
input: Res<Input<KeyCode>>, input: Res<Input<KeyCode>>,
time: Res<Time>, time: Res<Time>,
) { ) {
for (mut transform, _) in &mut characters { for (mut transform, mut player) in &mut characters {
let current_speed: f32 = player.speed;
let current_movement: Vec2 = player.movement;
if input.pressed(KeyCode::W) { if input.pressed(KeyCode::W) {
transform.translation.y += 100.0 * time.delta_seconds(); player.movement += current_movement * current_speed;
} }
if input.pressed(KeyCode::S) { if input.pressed(KeyCode::S) {
transform.translation.y -= 100.0 * time.delta_seconds(); player.movement -= current_movement * current_speed;
} }
if input.pressed(KeyCode::D) { if input.pressed(KeyCode::D) {
transform.translation.x += 100.0 * time.delta_seconds(); player.movement = Vec2::new(player.movement.x - player.movement.y, player.movement.y + player.movement.x);
} }
if input.pressed(KeyCode::A) { if input.pressed(KeyCode::A) {
transform.translation.x -= 100.0 * time.delta_seconds(); player.movement = Vec2::new(player.movement.x + player.movement.y, player.movement.y - player.movement.x);
} }
transform.translation.x += player.movement.x * time.delta_seconds();
transform.translation.y += player.movement.y * time.delta_seconds();
} }
} }