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 materials: ResMut<Assets<ColorMaterial>>,
) {
let listener = SpatialListener::new(1.);
commands
.spawn((Player, YSortable, Watched))
.with_children(|player| {
player.spawn((
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),
},
));
player.spawn((SpatialBundle::default(), listener.clone()));
player.spawn(Text2dBundle {
.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 {
@ -57,34 +55,35 @@ pub fn startup(
),
transform: Transform::from_translation(Vec3::new(30.0, -30.0, 0.)),
..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 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,
// 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.,
// );
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,
MaterialMesh2dBundle {
// global_transform,
mesh,
material,
transform,
..default()
},
));
// player.spawn((
// Crosshair,
// MaterialMesh2dBundle {
// // global_transform,
// mesh,
// material,
// transform,
// ..default()
// },
// ));
});
}
@ -99,82 +98,79 @@ pub fn controls(
mut next_state: ResMut<NextState<crate::View>>,
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 (camera, camera_transform) = cam_q.single();
let (mut sprite, mut texture, mut heading, player_entity) = player_q.single_mut();
let (camera, camera_transform) = cam_q.single();
let mouse_world_position = win_q
.single()
.cursor_position()
.and_then(|cursor| {
camera.viewport_to_world(&GlobalTransform::from_xyz(0., 0., 0.), cursor)
})
.map(|ray| ray.origin.truncate());
let mouse_world_position = win_q
.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() {
t.look_at(Vec3::ZERO, pos.extend(0.));
t.translation = camera_transform.translation();
}
if let Some(pos) = mouse_world_position {
if let Ok(mut t) = crosshair_q.get_single_mut() {
t.look_at(Vec3::ZERO, pos.extend(0.));
t.translation = camera_transform.translation();
}
}
**heading = Vec2::ZERO;
for button in mouse_input.get_just_pressed() {
match button {
MouseButton::Left => {
if let Some(world_position) = win_q
.single()
.cursor_position()
.and_then(|cursor| camera.viewport_to_world(camera_transform, cursor))
.map(|ray| ray.origin.truncate())
{
let spos = world_position.extend(-world_position.y);
commands.spawn(statue::statue_at_position(&assets, spos));
}
**heading = Vec2::ZERO;
for button in mouse_input.get_just_pressed() {
match button {
MouseButton::Left => {
if let Some(world_position) = win_q
.single()
.cursor_position()
.and_then(|cursor| camera.viewport_to_world(camera_transform, cursor))
.map(|ray| ray.origin.truncate())
{
let spos = world_position.extend(-world_position.y);
commands.spawn(statue::statue_at_position(&assets, spos));
}
_ => {}
}
_ => {}
}
for key in input.get_just_pressed() {
match key {
KeyCode::Escape => next_state.set(crate::View::MainMenu),
KeyCode::Enter => next_state.set(crate::View::MainMenu),
KeyCode::Space => {
let child = commands
.spawn(AudioSourceBundle {
source: assets.sounds.meow.clone(),
settings: PlaybackSettings::DESPAWN.with_spatial(false),
})
.id();
commands.entity(player_entity).push_children(&[child]);
}
_ => {}
}
for key in input.get_just_pressed() {
match key {
KeyCode::Escape => next_state.set(crate::View::MainMenu),
KeyCode::Enter => next_state.set(crate::View::MainMenu),
KeyCode::Space => {
let child = commands
.spawn(AudioSourceBundle {
source: assets.sounds.meow.clone(),
settings: PlaybackSettings::DESPAWN.with_spatial(false),
})
.id();
commands.entity(player_entity).push_children(&[child]);
}
_ => {}
}
for key in input.get_pressed() {
match key {
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;
}
_ => {}
}
for key in input.get_pressed() {
match key {
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;
}
_ => {}
}
if heading.y < 0. {
texture.index = 0;
} else if heading.y > 0. {
texture.index = 1;
} else if heading.x != 0. {
texture.index = 2;
sprite.flip_x = heading.x > 0.;
}
}
if heading.y < 0. {
texture.index = 0;
} else if heading.y > 0. {
texture.index = 1;
} else if heading.x != 0. {
texture.index = 2;
sprite.flip_x = heading.x > 0.;
}
}