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
.spawn((
// Player,
// YSortable,
// Watched,
// SpriteBundle {
// texture: assets.images.player.clone().into(),
// ..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,
Player,
YSortable,
Watched,
SpriteBundle {
texture: assets.images.player.clone(),
..default()
},
),
transform: Transform::from_translation(Vec3::new(30.0, -30.0, 0.)),
TextureAtlas {
layout: assets.layouts.player.clone(),
..default()
},
Mover {
velocity: Velocity(Vec2::ZERO),
heading: Heading(Vec2::ZERO),
speed: Speed(PLAYER_SPEED),
},
))
.with_children(|player| {
// 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 global_transform = GlobalTransform::from_xyz(
// // 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,
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 global_transform = GlobalTransform::from_xyz(
// 0.0, 20.0,
// // TODO: need some way to ensure this draws above everything else?
// // a UI layer or something?
// 1000.,
// );
// player.spawn((
// Crosshair,
let transform = Transform::from_xyz(
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 {
// // global_transform,
// mesh,
@ -83,32 +71,46 @@ pub fn startup(
// transform,
// ..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(
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>>,
mouse_input: Res<ButtonInput<MouseButton>>,
assets: Res<AssetLoader>,
win_q: Query<&Window, With<PrimaryWindow>>,
cam_q: Query<(&Camera, &GlobalTransform), With<Camera>>,
win: Query<&Window, With<PrimaryWindow>>,
cam: Query<(&Camera, &GlobalTransform), With<Camera>>,
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 (camera, camera_transform) = cam_q.single();
let (mut sprite, mut texture, mut heading, player_entity) =
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()
.cursor_position()
.and_then(|cursor| camera.viewport_to_world(&GlobalTransform::from_xyz(0., 0., 0.), cursor))
.map(|ray| ray.origin.truncate());
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.translation = camera_transform.translation();
}
@ -118,7 +120,7 @@ pub fn controls(
for button in mouse_input.get_just_pressed() {
match button {
MouseButton::Left => {
if let Some(world_position) = win_q
if let Some(world_position) = win
.single()
.cursor_position()
.and_then(|cursor| camera.viewport_to_world(camera_transform, cursor))