From 7d2e46c263468fbd82aec6ff3c008152a8f8389b Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Wed, 31 Jul 2024 11:54:05 -0500 Subject: [PATCH] Player movement is back --- src/player.rs | 118 +++++++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 58 deletions(-) diff --git a/src/player.rs b/src/player.rs index ab2b392..b8fce3e 100644 --- a/src/player.rs +++ b/src/player.rs @@ -26,89 +26,91 @@ 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, - ..default() - }, - ), - transform: Transform::from_translation(Vec3::new(30.0, -30.0, 0.)), + Player, + YSortable, + Watched, + SpriteBundle { + texture: assets.images.player.clone(), ..default() }, + 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, - // MaterialMesh2dBundle { - // // global_transform, - // mesh, - // material, - // transform, - // ..default() - // }, - // )); + 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, + // material, + // 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>, + mut player: Query<(&mut Sprite, &mut TextureAtlas, &mut Heading, Entity), With>, input: Res>, mouse_input: Res>, assets: Res, - win_q: Query<&Window, With>, - cam_q: Query<(&Camera, &GlobalTransform), With>, + win: Query<&Window, With>, + cam: Query<(&Camera, &GlobalTransform), With>, mut next_state: ResMut>, - mut crosshair_q: Query<&mut Transform, With>, + mut crosshair: Query<&mut Transform, With>, ) { - 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))