Player can move around

This commit is contained in:
Daniel Flanagan 2023-12-20 22:08:40 -06:00
parent 58a7b56f43
commit 529258a7d4
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
3 changed files with 43 additions and 49 deletions

View file

@ -15,7 +15,12 @@ fn main() {
}), }),
..Default::default() ..Default::default()
})) }))
.add_plugins((camera::Camera, assets::AssetLoader)) .add_plugins((
camera::Camera,
assets::AssetLoader,
player::Player,
movement::Movement,
))
.add_systems(Update, keyboard_input_system) .add_systems(Update, keyboard_input_system)
.insert_resource(ClearColor(Color::rgb(0.3, 0., 0.5))) .insert_resource(ClearColor(Color::rgb(0.3, 0., 0.5)))
.insert_resource(AmbientLight { .insert_resource(AmbientLight {

View file

@ -1,43 +1,37 @@
use bevy::prelude::*; use bevy::prelude::*;
pub struct MovementPlugin; #[derive(Component, Deref, DerefMut, Debug, Default)]
#[derive(Component, Debug, Default)]
pub struct Velocity(pub Vec2); pub struct Velocity(pub Vec2);
impl Velocity { #[derive(Component, Deref, DerefMut, Debug, Default)]
pub fn from_speed_heading(heading: Vec2, speed: f32) -> Self {
Self(heading.normalize_or_zero() * speed)
}
}
#[derive(Component, Debug, Default)]
pub struct Heading(pub Vec2); pub struct Heading(pub Vec2);
#[derive(Component, Debug, Default)] #[derive(Component, Deref, DerefMut, Debug, Default)]
pub struct Speed(pub f32); pub struct Speed(pub f32);
impl Heading {
pub fn new(x: f32, y: f32) -> Self {
Self(Vec2::new(x, y))
}
}
#[derive(Bundle, Debug, Default)]
pub struct MovementBundle {
pub velocity: Velocity,
pub heading: Heading,
pub speed: Speed,
}
impl Plugin for MovementPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, update_position);
}
}
fn update_position(mut query: Query<(&Velocity, &mut Transform)>, time: Res<Time>) { fn update_position(mut query: Query<(&Velocity, &mut Transform)>, time: Res<Time>) {
for (velocity, mut transform) in query.iter_mut() { for (velocity, mut transform) in query.iter_mut() {
transform.translation += (velocity.0 * time.delta_seconds()).extend(0.); transform.translation += (velocity.0 * time.delta_seconds()).extend(0.);
} }
} }
fn mover_movement(mut query: Query<(&mut Velocity, &Speed, &Heading)>, input: Res<Input<KeyCode>>) {
let (mut velocity, speed, heading) = query.single_mut();
**velocity = heading.normalize_or_zero() * (**speed);
}
#[derive(Bundle, Debug, Default)]
pub struct Mover {
pub velocity: Velocity,
pub heading: Heading,
pub speed: Speed,
}
pub struct Movement;
impl Plugin for Movement {
fn build(&self, app: &mut App) {
// TODO: these systems probably needs ordering?
app.add_systems(Update, (mover_movement, update_position));
}
}

View file

@ -2,23 +2,21 @@ use bevy::prelude::*;
use crate::{ use crate::{
assets::Assets, assets::Assets,
movement::{Heading, MovementBundle, Speed, Velocity}, movement::{Heading, Mover, Speed, Velocity},
}; };
const PLAYER_SPEED: f32 = 50.; const PLAYER_SPEED: f32 = 500.;
pub struct PlayerPlugin; #[derive(Component, Debug)]
pub struct Player;
impl Plugin for PlayerPlugin { impl Plugin for Player {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(PostStartup, spawn_player) app.add_systems(PostStartup, spawn_player)
.add_systems(Update, player_movement); .add_systems(Update, player_movement);
} }
} }
#[derive(Component, Debug)]
pub struct Player;
fn spawn_player(mut commands: Commands, assets: Res<Assets>) { fn spawn_player(mut commands: Commands, assets: Res<Assets>) {
commands.spawn(( commands.spawn((
Player, Player,
@ -27,7 +25,7 @@ fn spawn_player(mut commands: Commands, assets: Res<Assets>) {
transform: Transform::from_xyz(0., 0., 0.), transform: Transform::from_xyz(0., 0., 0.),
..Default::default() ..Default::default()
}, },
MovementBundle { Mover {
velocity: Velocity(Vec2::ZERO), velocity: Velocity(Vec2::ZERO),
heading: Heading(Vec2::ZERO), heading: Heading(Vec2::ZERO),
speed: Speed(PLAYER_SPEED), speed: Speed(PLAYER_SPEED),
@ -35,25 +33,22 @@ fn spawn_player(mut commands: Commands, assets: Res<Assets>) {
)); ));
} }
fn player_movement( fn player_movement(mut query: Query<&mut Heading, With<Player>>, input: Res<Input<KeyCode>>) {
mut query: Query<(&mut Velocity, &Speed, &mut Heading), With<Player>>, let mut heading = query.single_mut();
input: Res<Input<KeyCode>>, **heading = Vec2::ZERO;
) {
let (mut velocity, speed, mut heading) = query.single_mut();
heading = Vec2::ZERO;
for key in input.get_pressed() { for key in input.get_pressed() {
match key { match key {
KeyCode::W | KeyCode::Up => { KeyCode::W | KeyCode::Up => {
heading += Vec3::Y; **heading += Vec2::Y;
} }
KeyCode::S | KeyCode::Down => { KeyCode::S | KeyCode::Down => {
heading -= Vec3::Y; **heading -= Vec2::Y;
} }
KeyCode::A | KeyCode::Left => { KeyCode::A | KeyCode::Left => {
heading -= Vec3::X; **heading -= Vec2::X;
} }
KeyCode::D | KeyCode::Right => { KeyCode::D | KeyCode::Right => {
heading += Vec3::X; **heading += Vec2::X;
} }
_ => {} _ => {}
} }