Player movement is back

This commit is contained in:
Daniel Flanagan 2024-07-31 11:54:05 -05:00
parent 1e8281eedd
commit 7d2e46c263

View file

@ -26,56 +26,44 @@ pub fn startup(
) { ) {
commands commands
.spawn(( .spawn((
// Player, Player,
// YSortable, YSortable,
// Watched, Watched,
// SpriteBundle { SpriteBundle {
// texture: assets.images.player.clone().into(), texture: assets.images.player.clone(),
// ..default()
// },
// TextureAtlas {
// layout: assets.layouts.player.clone().into(),
// ..default()
// },
// Mover {
// velocity: Velocity(Vec2::ZERO),
// heading: Heading(Vec2::ZERO),
// speed: Speed(PLAYER_SPEED),
// },
// (SpatialBundle::default(), SpatialListener::new(1.)),
Text2dBundle {
text: Text::from_section(
"You",
TextStyle {
font_size: 11.0,
font: assets.fonts.iosevkalytemin.clone(),
color: Color::WHITE,
..default() ..default()
}, },
), TextureAtlas {
transform: Transform::from_translation(Vec3::new(30.0, -30.0, 0.)), layout: assets.layouts.player.clone(),
..default() ..default()
}, },
Mover {
velocity: Velocity(Vec2::ZERO),
heading: Heading(Vec2::ZERO),
speed: Speed(PLAYER_SPEED),
},
)) ))
.with_children(|player| { .with_children(|player| {
// let mesh = Mesh2dHandle(meshes.add(Capsule2d::new(3.0, 25.0))); let mesh = Mesh2dHandle(meshes.add(Capsule2d::new(3.0, 25.0)));
// let material = materials.add(Color::hsl(360. * 1 as f32 / 3 as f32, 0.95, 0.7)); let material = materials.add(Color::hsl(360. * 1 as f32 / 3 as f32, 0.95, 0.7));
// // let global_transform = GlobalTransform::from_xyz( // let global_transform = GlobalTransform::from_xyz(
// // 0.0, 20.0, // 0.0, 20.0,
// // // TODO: need some way to ensure this draws above everything else?
// // // a UI layer or something?
// // 1000.,
// // );
// let transform = Transform::from_xyz(
// 0.0, 0.0,
// // TODO: need some way to ensure this draws above everything else? // // TODO: need some way to ensure this draws above everything else?
// // a UI layer or something? // // a UI layer or something?
// 1000., // 1000.,
// ); // );
// player.spawn(( let transform = Transform::from_xyz(
// Crosshair, 0.0, 0.0,
// TODO: need some way to ensure this draws above everything else?
// a UI layer or something?
1000.,
);
player.spawn((
Crosshair,
// SpatialBundle::default(),
// SpatialListener::new(1.),
// MaterialMesh2dBundle { // MaterialMesh2dBundle {
// // global_transform, // // global_transform,
// mesh, // mesh,
@ -83,32 +71,46 @@ pub fn startup(
// transform, // transform,
// ..default() // ..default()
// }, // },
// )); // Text2dBundle {
// text: Text::from_section(
// "You",
// TextStyle {
// font_size: 11.0,
// font: assets.fonts.iosevkalytemin.clone(),
// color: Color::WHITE,
// ..default()
// },
// ),
// transform: Transform::from_translation(Vec3::new(30.0, -30.0, 0.)),
// ..default()
// },
));
}); });
} }
pub fn controls( pub fn controls(
mut commands: Commands, mut commands: Commands,
mut player_q: Query<(&mut Sprite, &mut TextureAtlas, &mut Heading, Entity), With<Player>>, mut player: Query<(&mut Sprite, &mut TextureAtlas, &mut Heading, Entity), With<Player>>,
input: Res<ButtonInput<KeyCode>>, input: Res<ButtonInput<KeyCode>>,
mouse_input: Res<ButtonInput<MouseButton>>, mouse_input: Res<ButtonInput<MouseButton>>,
assets: Res<AssetLoader>, assets: Res<AssetLoader>,
win_q: Query<&Window, With<PrimaryWindow>>, win: Query<&Window, With<PrimaryWindow>>,
cam_q: Query<(&Camera, &GlobalTransform), With<Camera>>, cam: Query<(&Camera, &GlobalTransform), With<Camera>>,
mut next_state: ResMut<NextState<crate::View>>, mut next_state: ResMut<NextState<crate::View>>,
mut crosshair_q: Query<&mut Transform, With<Crosshair>>, mut crosshair: Query<&mut Transform, With<Crosshair>>,
) { ) {
let (mut sprite, mut texture, mut heading, player_entity) = player_q.single_mut(); let (mut sprite, mut texture, mut heading, player_entity) =
let (camera, camera_transform) = cam_q.single(); player.get_single_mut().expect("no player_q entities");
let (camera, camera_transform) = cam.single();
let mouse_world_position = win_q let mouse_world_position = win
.single() .single()
.cursor_position() .cursor_position()
.and_then(|cursor| camera.viewport_to_world(&GlobalTransform::from_xyz(0., 0., 0.), cursor)) .and_then(|cursor| camera.viewport_to_world(&GlobalTransform::from_xyz(0., 0., 0.), cursor))
.map(|ray| ray.origin.truncate()); .map(|ray| ray.origin.truncate());
if let Some(pos) = mouse_world_position { if let Some(pos) = mouse_world_position {
if let Ok(mut t) = crosshair_q.get_single_mut() { if let Ok(mut t) = crosshair.get_single_mut() {
t.look_at(Vec3::ZERO, pos.extend(0.)); t.look_at(Vec3::ZERO, pos.extend(0.));
t.translation = camera_transform.translation(); t.translation = camera_transform.translation();
} }
@ -118,7 +120,7 @@ pub fn controls(
for button in mouse_input.get_just_pressed() { for button in mouse_input.get_just_pressed() {
match button { match button {
MouseButton::Left => { MouseButton::Left => {
if let Some(world_position) = win_q if let Some(world_position) = win
.single() .single()
.cursor_position() .cursor_position()
.and_then(|cursor| camera.viewport_to_world(camera_transform, cursor)) .and_then(|cursor| camera.viewport_to_world(camera_transform, cursor))