work on player not moving due to bad query

This commit is contained in:
Daniel Flanagan 2024-07-30 13:21:46 -05:00
parent f2c604e489
commit d42483f166

View file

@ -24,28 +24,26 @@ pub fn startup(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
) { ) {
let listener = SpatialListener::new(1.);
commands commands
.spawn((Player, YSortable, Watched)) .spawn((
.with_children(|player| { // Player,
player.spawn(( // YSortable,
SpriteBundle { // Watched,
texture: assets.images.player.clone().into(), // SpriteBundle {
..default() // texture: assets.images.player.clone().into(),
}, // ..default()
TextureAtlas { // },
layout: assets.layouts.player.clone().into(), // TextureAtlas {
..default() // layout: assets.layouts.player.clone().into(),
}, // ..default()
Mover { // },
velocity: Velocity(Vec2::ZERO), // Mover {
heading: Heading(Vec2::ZERO), // velocity: Velocity(Vec2::ZERO),
speed: Speed(PLAYER_SPEED), // heading: Heading(Vec2::ZERO),
}, // speed: Speed(PLAYER_SPEED),
)); // },
player.spawn((SpatialBundle::default(), listener.clone())); // (SpatialBundle::default(), SpatialListener::new(1.)),
player.spawn(Text2dBundle { Text2dBundle {
text: Text::from_section( text: Text::from_section(
"You", "You",
TextStyle { TextStyle {
@ -57,34 +55,35 @@ pub fn startup(
), ),
transform: Transform::from_translation(Vec3::new(30.0, -30.0, 0.)), transform: Transform::from_translation(Vec3::new(30.0, -30.0, 0.)),
..default() ..default()
}); },
))
.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 mesh = Mesh2dHandle(meshes.add(Capsule2d::new(3.0, 25.0))); // let transform = Transform::from_xyz(
let material = materials.add(Color::hsl(360. * 1 as f32 / 3 as f32, 0.95, 0.7)); // 0.0, 0.0,
// let global_transform = GlobalTransform::from_xyz(
// 0.0, 20.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.,
// ); // );
let transform = Transform::from_xyz( // player.spawn((
0.0, 0.0, // Crosshair,
// TODO: need some way to ensure this draws above everything else? // MaterialMesh2dBundle {
// a UI layer or something? // // global_transform,
1000., // mesh,
); // material,
// transform,
player.spawn(( // ..default()
Crosshair, // },
MaterialMesh2dBundle { // ));
// global_transform,
mesh,
material,
transform,
..default()
},
));
}); });
} }
@ -99,82 +98,79 @@ pub fn controls(
mut next_state: ResMut<NextState<crate::View>>, mut next_state: ResMut<NextState<crate::View>>,
mut crosshair_q: Query<&mut Transform, With<Crosshair>>, mut crosshair_q: Query<&mut Transform, With<Crosshair>>,
) { ) {
if let Ok((mut sprite, mut texture, mut heading, player_entity)) = player_q.get_single_mut() { let (mut sprite, mut texture, mut heading, player_entity) = player_q.single_mut();
let (camera, camera_transform) = cam_q.single(); let (camera, camera_transform) = cam_q.single();
let mouse_world_position = win_q let mouse_world_position = win_q
.single() .single()
.cursor_position() .cursor_position()
.and_then(|cursor| { .and_then(|cursor| camera.viewport_to_world(&GlobalTransform::from_xyz(0., 0., 0.), 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_q.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();
}
} }
}
**heading = Vec2::ZERO; **heading = Vec2::ZERO;
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_q
.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))
.map(|ray| ray.origin.truncate()) .map(|ray| ray.origin.truncate())
{ {
let spos = world_position.extend(-world_position.y); let spos = world_position.extend(-world_position.y);
commands.spawn(statue::statue_at_position(&assets, spos)); commands.spawn(statue::statue_at_position(&assets, spos));
}
} }
_ => {}
} }
_ => {}
} }
for key in input.get_just_pressed() { }
match key { for key in input.get_just_pressed() {
KeyCode::Escape => next_state.set(crate::View::MainMenu), match key {
KeyCode::Enter => next_state.set(crate::View::MainMenu), KeyCode::Escape => next_state.set(crate::View::MainMenu),
KeyCode::Space => { KeyCode::Enter => next_state.set(crate::View::MainMenu),
let child = commands KeyCode::Space => {
.spawn(AudioSourceBundle { let child = commands
source: assets.sounds.meow.clone(), .spawn(AudioSourceBundle {
settings: PlaybackSettings::DESPAWN.with_spatial(false), source: assets.sounds.meow.clone(),
}) settings: PlaybackSettings::DESPAWN.with_spatial(false),
.id(); })
commands.entity(player_entity).push_children(&[child]); .id();
} commands.entity(player_entity).push_children(&[child]);
_ => {}
} }
_ => {}
} }
for key in input.get_pressed() { }
match key { for key in input.get_pressed() {
KeyCode::KeyA | KeyCode::ArrowLeft => { match key {
**heading -= Vec2::X; KeyCode::KeyA | KeyCode::ArrowLeft => {
} **heading -= Vec2::X;
KeyCode::KeyD | KeyCode::ArrowRight => {
**heading += Vec2::X;
}
KeyCode::KeyW | KeyCode::ArrowUp => {
**heading += Vec2::Y;
}
KeyCode::KeyS | KeyCode::ArrowDown => {
**heading -= Vec2::Y;
}
_ => {}
} }
KeyCode::KeyD | KeyCode::ArrowRight => {
**heading += Vec2::X;
}
KeyCode::KeyW | KeyCode::ArrowUp => {
**heading += Vec2::Y;
}
KeyCode::KeyS | KeyCode::ArrowDown => {
**heading -= Vec2::Y;
}
_ => {}
} }
if heading.y < 0. { }
texture.index = 0; if heading.y < 0. {
} else if heading.y > 0. { texture.index = 0;
texture.index = 1; } else if heading.y > 0. {
} else if heading.x != 0. { texture.index = 1;
texture.index = 2; } else if heading.x != 0. {
sprite.flip_x = heading.x > 0.; texture.index = 2;
} sprite.flip_x = heading.x > 0.;
} }
} }