WIP movement component stuff
This commit is contained in:
parent
4371f02949
commit
58a7b56f43
2 changed files with 29 additions and 21 deletions
|
@ -3,19 +3,21 @@ use bevy::prelude::*;
|
||||||
pub struct MovementPlugin;
|
pub struct MovementPlugin;
|
||||||
|
|
||||||
#[derive(Component, Debug, Default)]
|
#[derive(Component, Debug, Default)]
|
||||||
pub struct Velocity(Vec2);
|
pub struct Velocity(pub Vec2);
|
||||||
|
|
||||||
#[derive(Component, Debug, Default)]
|
|
||||||
pub struct Heading(Vec2);
|
|
||||||
|
|
||||||
#[derive(Component, Debug, Default)]
|
|
||||||
pub struct Speed(f32);
|
|
||||||
|
|
||||||
impl Velocity {
|
impl Velocity {
|
||||||
pub fn from_speed_heading(heading: Vec2, speed: f32) -> Self {
|
pub fn from_speed_heading(heading: Vec2, speed: f32) -> Self {
|
||||||
Self(heading.normalize_or_zero() * speed)
|
Self(heading.normalize_or_zero() * speed)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Default)]
|
||||||
|
pub struct Heading(pub Vec2);
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Default)]
|
||||||
|
pub struct Speed(pub f32);
|
||||||
|
|
||||||
|
impl Heading {
|
||||||
pub fn new(x: f32, y: f32) -> Self {
|
pub fn new(x: f32, y: f32) -> Self {
|
||||||
Self(Vec2::new(x, y))
|
Self(Vec2::new(x, y))
|
||||||
}
|
}
|
||||||
|
@ -24,6 +26,8 @@ impl Velocity {
|
||||||
#[derive(Bundle, Debug, Default)]
|
#[derive(Bundle, Debug, Default)]
|
||||||
pub struct MovementBundle {
|
pub struct MovementBundle {
|
||||||
pub velocity: Velocity,
|
pub velocity: Velocity,
|
||||||
|
pub heading: Heading,
|
||||||
|
pub speed: Speed,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Plugin for MovementPlugin {
|
impl Plugin for MovementPlugin {
|
||||||
|
|
|
@ -2,7 +2,7 @@ use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
assets::Assets,
|
assets::Assets,
|
||||||
movement::{MovementBundle, Velocity},
|
movement::{Heading, MovementBundle, Speed, Velocity},
|
||||||
};
|
};
|
||||||
|
|
||||||
const PLAYER_SPEED: f32 = 50.;
|
const PLAYER_SPEED: f32 = 50.;
|
||||||
|
@ -19,39 +19,43 @@ impl Plugin for PlayerPlugin {
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct Player;
|
pub struct Player;
|
||||||
|
|
||||||
fn spawn_player(mut commands: Commands, sprite_assets: Res<SpriteAssets>) {
|
fn spawn_player(mut commands: Commands, assets: Res<Assets>) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Player,
|
Player,
|
||||||
SpriteBundle {
|
SpriteBundle {
|
||||||
texture: sprite_assets.knight.clone(),
|
texture: assets.sprites.square.clone(),
|
||||||
transform: Transform::from_xyz(0., 0., 0.),
|
transform: Transform::from_xyz(0., 0., 0.),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
MovementBundle {
|
MovementBundle {
|
||||||
velocity: Velocity::new(0., 0.),
|
velocity: Velocity(Vec2::ZERO),
|
||||||
|
heading: Heading(Vec2::ZERO),
|
||||||
|
speed: Speed(PLAYER_SPEED),
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn player_movement(mut query: Query<&mut Velocity, With<Player>>, input: Res<Input<KeyCode>>) {
|
fn player_movement(
|
||||||
let mut direction = Vec3::ZERO;
|
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;
|
||||||
for key in input.get_pressed() {
|
for key in input.get_pressed() {
|
||||||
match key {
|
match key {
|
||||||
KeyCode::W | KeyCode::Up => {
|
KeyCode::W | KeyCode::Up => {
|
||||||
direction += Vec3::Y;
|
heading += Vec3::Y;
|
||||||
}
|
|
||||||
KeyCode::A | KeyCode::Left => {
|
|
||||||
direction += Vec3::NEG_X;
|
|
||||||
}
|
}
|
||||||
KeyCode::S | KeyCode::Down => {
|
KeyCode::S | KeyCode::Down => {
|
||||||
direction += Vec3::NEG_Y;
|
heading -= Vec3::Y;
|
||||||
|
}
|
||||||
|
KeyCode::A | KeyCode::Left => {
|
||||||
|
heading -= Vec3::X;
|
||||||
}
|
}
|
||||||
KeyCode::D | KeyCode::Right => {
|
KeyCode::D | KeyCode::Right => {
|
||||||
direction += Vec3::X;
|
heading += Vec3::X;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut velocity = query.single_mut();
|
|
||||||
velocity.change_direction_speed(direction, PLAYER_SPEED);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue