Player can move around
This commit is contained in:
parent
58a7b56f43
commit
529258a7d4
|
@ -15,7 +15,12 @@ fn main() {
|
|||
}),
|
||||
..Default::default()
|
||||
}))
|
||||
.add_plugins((camera::Camera, assets::AssetLoader))
|
||||
.add_plugins((
|
||||
camera::Camera,
|
||||
assets::AssetLoader,
|
||||
player::Player,
|
||||
movement::Movement,
|
||||
))
|
||||
.add_systems(Update, keyboard_input_system)
|
||||
.insert_resource(ClearColor(Color::rgb(0.3, 0., 0.5)))
|
||||
.insert_resource(AmbientLight {
|
||||
|
|
|
@ -1,43 +1,37 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
pub struct MovementPlugin;
|
||||
|
||||
#[derive(Component, Debug, Default)]
|
||||
#[derive(Component, Deref, DerefMut, Debug, Default)]
|
||||
pub struct Velocity(pub Vec2);
|
||||
|
||||
impl Velocity {
|
||||
pub fn from_speed_heading(heading: Vec2, speed: f32) -> Self {
|
||||
Self(heading.normalize_or_zero() * speed)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Debug, Default)]
|
||||
#[derive(Component, Deref, DerefMut, Debug, Default)]
|
||||
pub struct Heading(pub Vec2);
|
||||
|
||||
#[derive(Component, Debug, Default)]
|
||||
#[derive(Component, Deref, DerefMut, Debug, Default)]
|
||||
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>) {
|
||||
for (velocity, mut transform) in query.iter_mut() {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,23 +2,21 @@ use bevy::prelude::*;
|
|||
|
||||
use crate::{
|
||||
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) {
|
||||
app.add_systems(PostStartup, spawn_player)
|
||||
.add_systems(Update, player_movement);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct Player;
|
||||
|
||||
fn spawn_player(mut commands: Commands, assets: Res<Assets>) {
|
||||
commands.spawn((
|
||||
Player,
|
||||
|
@ -27,7 +25,7 @@ fn spawn_player(mut commands: Commands, assets: Res<Assets>) {
|
|||
transform: Transform::from_xyz(0., 0., 0.),
|
||||
..Default::default()
|
||||
},
|
||||
MovementBundle {
|
||||
Mover {
|
||||
velocity: Velocity(Vec2::ZERO),
|
||||
heading: Heading(Vec2::ZERO),
|
||||
speed: Speed(PLAYER_SPEED),
|
||||
|
@ -35,25 +33,22 @@ fn spawn_player(mut commands: Commands, assets: Res<Assets>) {
|
|||
));
|
||||
}
|
||||
|
||||
fn player_movement(
|
||||
mut query: Query<(&mut Velocity, &Speed, &mut Heading), With<Player>>,
|
||||
input: Res<Input<KeyCode>>,
|
||||
) {
|
||||
let (mut velocity, speed, mut heading) = query.single_mut();
|
||||
heading = Vec2::ZERO;
|
||||
fn player_movement(mut query: Query<&mut Heading, With<Player>>, input: Res<Input<KeyCode>>) {
|
||||
let mut heading = query.single_mut();
|
||||
**heading = Vec2::ZERO;
|
||||
for key in input.get_pressed() {
|
||||
match key {
|
||||
KeyCode::W | KeyCode::Up => {
|
||||
heading += Vec3::Y;
|
||||
**heading += Vec2::Y;
|
||||
}
|
||||
KeyCode::S | KeyCode::Down => {
|
||||
heading -= Vec3::Y;
|
||||
**heading -= Vec2::Y;
|
||||
}
|
||||
KeyCode::A | KeyCode::Left => {
|
||||
heading -= Vec3::X;
|
||||
**heading -= Vec2::X;
|
||||
}
|
||||
KeyCode::D | KeyCode::Right => {
|
||||
heading += Vec3::X;
|
||||
**heading += Vec2::X;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue