update comit

This commit is contained in:
2024-09-19 06:00:48 +02:00
parent b0fbfe1157
commit 1b0d2f5acd
5 changed files with 326 additions and 460 deletions

40
Cargo.lock generated
View File

@@ -207,9 +207,9 @@ dependencies = [
[[package]]
name = "arrayref"
version = "0.3.8"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d151e35f61089500b617991b791fc8bfd237ae50cd5950803758a179b41e67a"
checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb"
[[package]]
name = "arrayvec"
@@ -1482,9 +1482,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.1.18"
version = "1.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476"
checksum = "2d74707dde2ba56f86ae90effb3b43ddd369504387e718014de010cec7959800"
dependencies = [
"jobserver",
"libc",
@@ -1679,6 +1679,16 @@ dependencies = [
"libc",
]
[[package]]
name = "core-foundation"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b55271e5c8c478ad3f38ad24ef34923091e0548492a266d19b3c0b4d82574c63"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
@@ -1692,7 +1702,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081"
dependencies = [
"bitflags 1.3.2",
"core-foundation",
"core-foundation 0.9.4",
"core-graphics-types",
"foreign-types",
"libc",
@@ -1705,7 +1715,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf"
dependencies = [
"bitflags 1.3.2",
"core-foundation",
"core-foundation 0.9.4",
"libc",
]
@@ -2308,9 +2318,9 @@ dependencies = [
[[package]]
name = "gilrs"
version = "0.10.9"
version = "0.10.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfb8c78963a8856a5b10015c9349176ff5edbc8095384d52aada467a848bc03a"
checksum = "a556964c6d62458084356ce9770676f5104bd667e12e9a795691076e8a17c5cf"
dependencies = [
"fnv",
"gilrs-core",
@@ -2325,7 +2335,7 @@ version = "0.5.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "732dadc05170599ddec9a89653f10d7a2af54da9181b3fa6e2bd49907ec8f7e4"
dependencies = [
"core-foundation",
"core-foundation 0.9.4",
"inotify 0.10.2",
"io-kit-sys",
"js-sys",
@@ -3591,9 +3601,9 @@ dependencies = [
[[package]]
name = "once_cell"
version = "1.19.0"
version = "1.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
checksum = "33ea5043e58958ee56f3e15a90aee535795cd7dfd319846288d93c5b57d85cbe"
[[package]]
name = "opener"
@@ -4912,12 +4922,12 @@ dependencies = [
[[package]]
name = "webbrowser"
version = "1.0.1"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "425ba64c1e13b1c6e8c5d2541c8fac10022ca584f33da781db01b5756aef1f4e"
checksum = "2e5f07fb9bc8de2ddfe6b24a71a75430673fd679e568c48b52716cef1cfae923"
dependencies = [
"block2",
"core-foundation",
"core-foundation 0.10.0",
"home",
"jni",
"log",
@@ -5455,7 +5465,7 @@ dependencies = [
"calloop",
"cfg_aliases 0.2.1",
"concurrent-queue",
"core-foundation",
"core-foundation 0.9.4",
"core-graphics",
"cursor-icon",
"dpi",

View File

@@ -1,282 +0,0 @@
use bevy::{
math::vec3,
math::vec2,
math::bounding::{Aabb2d, BoundingCircle, BoundingVolume, IntersectsVolume},
prelude::*
};
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
//paddle
const PADDLE_START_Y: f32 = 0.0;
const PADDLE_SIZE: Vec2 = Vec2::new(120.0, 20.0);
const PADDLE_COLOR: Color = Color::srgb(0.3, 0.3, 0.7);
const PADDLE_SPEED: f32 = 500.0;
//ball
const BALL_COLOR: Color = Color::srgb(1.0, 0.54, 0.5);
const BALL_STARTING_POSITION: Vec3 = Vec3::new(0.0, -50.0, 1.0);
const BALL_SIZE: Vec2 = Vec2::new(30.0, 30.0);
const BALL_SPEED: f32 = 400.0;
const BALL_INITIAL_DIRECTION: Vec2 = Vec2::new(0.5, -0.5);
//wall
const LEFT_WALL: f32 = -450.0;
const RIGHT_WALL: f32 = 450.0;
const BOTTOM_WALL: f32 = -300.0;
const TOP_WALL: f32 = 300.0;
const WALL_THICKNESS: f32 = 10.0;
const WALL_BLOCK_WIDTH: f32 = RIGHT_WALL - LEFT_WALL;
const WALL_BLOCK_HEIGHT: f32 = TOP_WALL - BOTTOM_WALL;
const WALL_COLOR: Color = Color::srgb(0.8, 0.8, 0.8);
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Update, close_on_esc)
.add_systems(Startup, setup)
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.add_systems(
FixedUpdate,
(
move_paddle,
apply_velocity,
check_ball_collision.after(apply_velocity),
),
)
.run();
}
#[derive(Component)]
struct Paddle;
#[derive(Component)]
struct Ball {
size: Vec2,
}
#[derive(Component, Deref, DerefMut)]
struct Velocity(Vec2);
#[derive(Component)]
struct Collider {
size: Vec2,
}
#[derive(Bundle)]
struct WallBundle{
sprite_bundle: SpriteBundle,
collider: Collider,
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
//camera
commands.spawn(Camera2dBundle::default());
//paddle
commands.spawn((
SpriteBundle {
transform: Transform {
translation: vec3(0., PADDLE_START_Y, 0.),
..default()
},
sprite: Sprite {
color: PADDLE_COLOR,
custom_size: Some(PADDLE_SIZE),
..default()
},
..default()
},
Paddle,
));
//ball
let ball_tex = asset_server.load("textures/circle.png");
commands.spawn((
SpriteBundle {
transform: Transform {
translation: BALL_STARTING_POSITION,
..default()
},
sprite: Sprite {
color: BALL_COLOR,
custom_size: Some(BALL_SIZE),
..default()
},
texture:ball_tex,
..default()
},
Ball { size: BALL_SIZE },
Velocity(BALL_SPEED * BALL_INITIAL_DIRECTION),
));
//walls
{
let vertical_wall_size = vec2(WALL_THICKNESS, WALL_BLOCK_HEIGHT + WALL_THICKNESS);
let horizontal_wall_size = vec2(WALL_BLOCK_WIDTH + WALL_THICKNESS, WALL_THICKNESS);
//left wall
commands.spawn(WallBundle {
sprite_bundle: SpriteBundle {
transform: Transform {
translation: vec3(LEFT_WALL, 0.0, 0.0),
..default()
},
sprite: Sprite {
color: WALL_COLOR,
custom_size: Some(vertical_wall_size),
..default()
},
..default()
},
collider: Collider {
size: vertical_wall_size,
},
});
//right wall
commands.spawn(WallBundle {
sprite_bundle: SpriteBundle {
transform: Transform {
translation: vec3(RIGHT_WALL, 0.0, 0.0),
..default()
},
sprite: Sprite {
color: WALL_COLOR,
custom_size: Some(vertical_wall_size),
..default()
},
..default()
},
collider: Collider {
size: vertical_wall_size,
},
});
//bottom wall
commands.spawn(WallBundle {
sprite_bundle: SpriteBundle {
transform: Transform {
translation: vec3(0.0, BOTTOM_WALL, 0.0),
..default()
},
sprite: Sprite {
color: WALL_COLOR,
custom_size: Some(horizontal_wall_size),
..default()
},
..default()
},
collider: Collider {
size: horizontal_wall_size,
},
});
//top wall
commands.spawn(WallBundle {
sprite_bundle: SpriteBundle {
transform: Transform {
translation: vec3(0.0, TOP_WALL, 0.0),
..default()
},
sprite: Sprite {
color: WALL_COLOR,
custom_size: Some(horizontal_wall_size),
..default()
},
..default()
},
collider: Collider {
size: horizontal_wall_size,
},
});
}
}
fn move_paddle(
input: Res<ButtonInput<KeyCode>>,
time_step: Res<Time<Fixed>>,
mut query: Query<&mut Transform, With<Paddle>>,
) {
let mut paddle_transform = query.single_mut();
let mut direction: f32 = 0.0;
if input.pressed(KeyCode::KeyA) {
direction -= 1.0
}
if input.pressed(KeyCode::KeyD) {
direction += 1.0;
}
let mut new_x =
paddle_transform.translation.x + direction * PADDLE_SPEED * time_step.delta_seconds();
new_x = new_x.min(RIGHT_WALL - (WALL_THICKNESS + PADDLE_SIZE.x) * 0.5);
new_x = new_x.max(LEFT_WALL + (WALL_THICKNESS + PADDLE_SIZE.x) * 0.5);
paddle_transform.translation.x = new_x;
}
fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time_step: Res<Time<Fixed>>) {
let dt = time_step.delta_seconds();
for (mut transform, velocity) in &mut query {
transform.translation.x += velocity.x * dt;
transform.translation.y += velocity.y * dt;
}
}
fn check_ball_collision(
mut ball_query: Query<(&mut Velocity, &Transform, &Ball)>,
collider_query: Query<(&Transform, &Collider)>,
) {
for (mut ball_velocity, ball_transform, ball) in &mut ball_query {
for (transform, other) in &collider_query {
let collision = ball_collision(
ball_transform.translation,
ball.size,
transform.translation,
other.size,
);
let mut reflect_x = false;
let mut reflect_y = false;
if let Some(collision) = collision {
match collision {
Collision::Left => reflect_x = ball_velocity.x > 0.0,
Collision::Right => reflect_x = ball_velocity.x < 0.0,
Collision::Top => reflect_y = ball_velocity.y < 0.0,
Collision::Bottom => reflect_y = ball_velocity.y > 0.0,
Collision::Inside => { /* do nothing */ }
}
if reflect_x {
ball_velocity.x *= -1.;
}
if reflect_y {
ball_velocity.y *= -1.;
}
}
}
}
}
fn ball_collision() {
}
pub fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<ButtonInput<KeyCode>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}
if input.just_pressed(KeyCode::Escape) {
commands.entity(window).despawn();
}
}
}

View File

@@ -1,50 +0,0 @@
use bevy::prelude::*;
#[derive(Component)]
struct Person;
#[derive(Component)]
struct Name(String);
#[derive(Resource)]
struct GreetTimer(Timer);
pub struct HelloPlugin;
impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)));
app.add_systems(Startup, add_people);
app.add_systems(Update, (update_people, greet_people).chain());
}
}
fn main() {
App::new()
.add_plugins(HelloPlugin)
.add_plugins(DefaultPlugins)
.run();
}
fn add_people(mut commands: Commands) {
commands.spawn((Person, Name("Elaina Proctor".to_string())));
commands.spawn((Person, Name("Renzo Hume".to_string())));
commands.spawn((Person, Name("Zayna Nieves".to_string())));
}
fn greet_people(time: Res<Time>, mut timer: ResMut<GreetTimer>, query: Query<&Name, With<Person>>) {
if timer.0.tick(time.delta()).just_finished() {
for name in &query {
println!("hello {}!", name.0);
}
}
}
fn update_people(mut query: Query<&mut Name, With<Person>>) {
for mut name in &mut query {
if name.0 == "Elaina Proctor" {
name.0 = "Elaina Hume".to_string();
break;
}
}
}

View File

@@ -16,6 +16,8 @@ use avian3d::prelude::*;
use bevy_editor_pls::prelude::*;
use leafwing_input_manager::prelude::*;
mod player;
fn main() {
App::new()
.insert_resource(GreetTimer(Timer::from_seconds(2.5, TimerMode::Repeating)))
@@ -24,8 +26,7 @@ fn main() {
(
//close_on_esc,
spawn_block,
move_camera,
toggle_mouse.run_if(input_just_pressed(KeyCode::Escape)),
//move_camera,
//move_player,
//change_fov
),
@@ -34,12 +35,12 @@ fn main() {
DefaultPlugins,
PhysicsPlugins::default(),
EditorPlugin::default(),
player::plugin,
))
.add_systems(
Startup,
(
setup,
cursor_grab,
//spawn_view_model,
//spawn_world_model,
),
@@ -56,17 +57,6 @@ struct WorldModelCamera;
#[derive(Resource)]
struct GreetTimer(Timer);
#[derive(Component)]
pub struct FPSCamera {
pub speed: f32,
pub sensitivity: f32,
pub rotate_lock: f32,
pub rotation: Vec3,
pub recoil_shake: Vec3,
pub camera_shake_readjustment_factor: f32,
}
/// Used implicitly by all entities without a `RenderLayers` component.
/// Our world model camera and all objects other than the player are on this layer.
/// The light source belongs to both layers.
@@ -76,32 +66,6 @@ const DEFAULT_RENDER_LAYER: usize = 0;
/// The light source belongs to both layers.
const VIEW_MODEL_RENDER_LAYER: usize = 1;
fn cursor_grab (
mut q_windows: Query<&mut Window, With<PrimaryWindow>>,
) {
let mut primary_window = q_windows.single_mut();
// if you want to use the cursor, but not let it leave the window,
// use `Confined` mode:
//primary_window.cursor.grab_mode = CursorGrabMode::Confined;
// for a game that doesn't use the cursor (like a shooter):
// use `Locked` mode to keep the cursor in one place
primary_window.cursor.grab_mode = CursorGrabMode::Locked;
// also hide the cursor
primary_window.cursor.visible = false;
}
/* fn cursor_ungrab(
mut q_windows: Query<&mut Window, With<PrimaryWindow>>,
) {
let mut primary_window = q_windows.single_mut();
primary_window.cursor.grab_mode = CursorGrabMode::None;
primary_window.cursor.visible = true;
} */
#[derive(Component)]
struct Customcomponent;
@@ -116,7 +80,7 @@ fn setup(
PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::srgb_u8(255, 144, 124)),
transform: Transform::from_xyz(0.0, 2.0, 0.0),
transform: Transform::from_xyz(-20.0, 2.0, -20.0),
..default()
},
Customcomponent,
@@ -140,7 +104,7 @@ fn setup(
PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::srgb_u8(124, 144, 255)),
transform: Transform::from_xyz(0.0, 5.0, 0.0),
transform: Transform::from_xyz(-20.0, 5.0, -20.0),
..default()
},
RigidBody::Dynamic,
@@ -161,14 +125,14 @@ fn setup(
});*/
// view_ model() - The *view model* is the model that represents the player's body
let arm = meshes.add(Cuboid::new(0.1, 0.1, 0.5));
let arm_materlial = materials.add(Color::from(tailwind::TEAL_200));
//let arm = meshes.add(Cuboid::new(0.1, 0.1, 0.5));
//let arm_materlial = materials.add(Color::from(tailwind::TEAL_200));
// Player
commands
/*commands
.spawn(SpatialBundle {
visibility: Visibility::Visible,
transform: Transform::from_xyz(10., 0., 5.)
transform: Transform::from_xyz(10., 1., 4.)
.looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
@@ -181,7 +145,7 @@ fn setup(
fov: 90.,
..Default::default()
})),*/
transform: Transform::from_xyz(0.0, 1.0, 4.0),
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..default()
},
RigidBody::Dynamic,
@@ -205,7 +169,7 @@ fn setup(
GravityScale(1.),
//Player,
));
});
});*/
}
/*pub fn close_on_esc(
@@ -224,21 +188,6 @@ fn setup(
}
}*/
fn toggle_mouse(mut window: Query<&mut Window, With<PrimaryWindow>>) {
for mut window in &mut window {
match window.cursor.grab_mode {
CursorGrabMode::None => {
window.cursor.grab_mode = CursorGrabMode::Locked;
window.cursor.visible = false;
}
CursorGrabMode::Confined | CursorGrabMode::Locked => {
window.cursor.grab_mode = CursorGrabMode::None;
window.cursor.visible = true;
}
}
}
}
fn spawn_block(
time: Res<Time>,
mut timer: ResMut<GreetTimer>,
@@ -252,7 +201,7 @@ fn spawn_block(
PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::srgb_u8(124, 144, 255)),
transform: Transform::from_xyz(0.0, 5.0, 0.0),
transform: Transform::from_xyz(20.0, 5.0, 10.0),
..default()
},
RigidBody::Dynamic,
@@ -262,55 +211,6 @@ fn spawn_block(
}
}
pub fn move_camera(
//cursor_lock_state: Res<>,
q_windows: Query<&Window, With<PrimaryWindow>>,
//mut q_windows: Query<&mut Window, With<PrimaryWindow>>,
mut motion_evr: EventReader<MouseMotion>,
time: Res<Time>,
mut camera_query: Query<(&mut Transform, &mut FPSCamera)>,
) {
let primary_window = q_windows.single();
//let mut primary_window = q_windows.single_mut();
//primary_window.cursor.grab_mode = CursorGrabMode::None;
//info!("MODE: {}", primary_window.cursor.grab_mode);
//if cursor_lock_state.state {
//info!("IM HERE");
if primary_window.cursor.grab_mode == CursorGrabMode::Locked{
//info!("MODE ACTIVE");
for (mut transform, mut camera) in camera_query.iter_mut() {
for ev in motion_evr.read() {
camera.rotation.y -= ev.delta.x * camera.sensitivity;
camera.rotation.x -= ev.delta.y * camera.sensitivity;
camera.rotation.x =
f32::clamp(camera.rotation.x, -camera.rotate_lock, camera.rotate_lock);
//transform.rotation += Quat::from_axis_angle(Vec3::new(0., 1., 0.), angle);
//println!("{}",x_quat);
}
/*camera.recoil_shake = move_towards(
camera.recoil_shake,
Vec3::ZERO,
time.delta_seconds() * camera.camera_shake_readjustment_factor,
);*/
let x_quat = Quat::from_axis_angle(
Vec3::new(0., 1., 0.),
//camera.rotation.y - camera.recoil_shake.x,
camera.rotation.y
);
let y_quat = Quat::from_axis_angle(
Vec3::new(1., 0., 0.),
//camera.rotation.x + camera.recoil_shake.y,
camera.rotation.x
);
transform.rotation = x_quat * y_quat;
}
}
}
/*fn change_fov (
input: Res<ButtonInput<KeyCode>>,
mut world_model_projection: Query<&mut Projection, With<WorldModelCamera>>,

288
src/player.rs Normal file
View File

@@ -0,0 +1,288 @@
use bevy::{
input::common_conditions::input_just_pressed,
prelude::*,
window::{CursorGrabMode, PrimaryWindow},
//input::mouse::MouseMotion,
};
use bevy_editor_pls::{egui::widgets, EditorPlugin};
use avian3d::prelude::*;
use leafwing_input_manager::prelude::*;
pub fn plugin(app: &mut App) {
app.add_plugins(InputManagerPlugin::<PlayerAction>::default())
.add_systems(Startup,
(
spawn_player,
lock_mouse,
)
)
.add_systems(
Update,
(
player_move,
player_look,
//move_camera,
toggle_mouse.run_if(input_just_pressed(KeyCode::Escape)),
//noclip.run_if(input_just_pressed(KeyCode::F11)),
),
);
}
#[derive(Component)]
pub struct FPSCamera {
pub speed: f32,
pub sensitivity: f32,
pub rotate_lock: f32,
pub rotation: Vec3,
pub recoil_shake: Vec3,
pub camera_shake_readjustment_factor: f32,
}
#[derive(Component)]
struct PlayerCam;
#[derive(Component)]
pub struct Player;
#[derive(Reflect, Clone, Copy, Hash, PartialEq, Eq, Debug)]
pub enum PlayerAction {
MoveUp,
MoveDown,
MoveLeft,
MoveRight,
Look,
FlyUp,
FlyDown,
Shoot,
}
impl Actionlike for PlayerAction {
fn input_control_kind(&self) -> InputControlKind {
match self {
PlayerAction::Look => InputControlKind::DualAxis,
_ => InputControlKind::Button,
}
}
}
fn player_bindings() -> InputMap<PlayerAction> {
let mut map = InputMap::new([
(PlayerAction::MoveUp, KeyCode::KeyW),
(PlayerAction::MoveDown, KeyCode::KeyS),
(PlayerAction::MoveLeft, KeyCode::KeyA),
(PlayerAction::MoveRight, KeyCode::KeyD),
])
.with_dual_axis(PlayerAction::Look, MouseMove::default().sensitivity(0.1));
map.insert(PlayerAction::FlyUp, KeyCode::Space)
.insert(PlayerAction::FlyDown, KeyCode::ShiftLeft)
.insert(PlayerAction::Shoot, MouseButton::Left);
map
}
fn spawn_player(
mut commands: Commands,
mut mesh_assets: ResMut<Assets<Mesh>>,
mut material_assets: ResMut<Assets<StandardMaterial>>,
) {
commands
.spawn((
Player,
FPSCamera {
camera_shake_readjustment_factor: 0.3,
recoil_shake: Vec3::ZERO,
rotation: Vec3::new(0., 0., 0.),
speed: 300.,
rotate_lock: 88. * 0.0174533,
//sensitivity: (0.173) / 900.,
sensitivity: 0.0008,
},
SpatialBundle{
transform: Transform::from_xyz(10., 1., 4.),
..default()
},
Collider::capsule(0.5, 1.0),
mesh_assets.add(Capsule3d::new(0.5, 1.)),
material_assets.add(StandardMaterial::default()),
RigidBody::Dynamic,
InputManagerBundle {
input_map: player_bindings(),
action_state: ActionState::default(),
},
LockedAxes::ROTATION_LOCKED,
CollidingEntities::default(),
//ActiveEvents::all(),
))
.with_children(|p| {
p.spawn((
Camera3dBundle {
transform: Transform::from_translation(Vec3::Y * 0.5),
..Default::default()
},
PlayerCam,
));
});
}
fn lock_mouse (
mut q_windows: Query<&mut Window, With<PrimaryWindow>>,
) {
let mut primary_window = q_windows.single_mut();
// use `Locked` mode to keep the cursor in one place
primary_window.cursor.grab_mode = CursorGrabMode::Locked;
// also hide the cursor
primary_window.cursor.visible = false;
}
fn toggle_mouse(mut window: Query<&mut Window, With<PrimaryWindow>>) {
for mut window in &mut window {
match window.cursor.grab_mode {
CursorGrabMode::None => {
window.cursor.grab_mode = CursorGrabMode::Locked;
window.cursor.visible = false;
}
CursorGrabMode::Confined | CursorGrabMode::Locked => {
window.cursor.grab_mode = CursorGrabMode::None;
window.cursor.visible = true;
}
}
}
}
/*pub fn move_camera(
//cursor_lock_state: Res<>,
q_windows: Query<&Window, With<PrimaryWindow>>,
//mut q_windows: Query<&mut Window, With<PrimaryWindow>>,
mut motion_evr: EventReader<MouseMotion>,
time: Res<Time>,
mut camera_query: Query<(&mut Transform, &mut FPSCamera)>,
) {
let primary_window = q_windows.single();
//let mut primary_window = q_windows.single_mut();
//primary_window.cursor.grab_mode = CursorGrabMode::None;
//info!("MODE: {}", primary_window.cursor.grab_mode);
//if cursor_lock_state.state {
//info!("IM HERE");
if primary_window.cursor.grab_mode == CursorGrabMode::Locked{
//info!("MODE ACTIVE");
for (mut transform, mut camera) in camera_query.iter_mut() {
for ev in motion_evr.read() {
camera.rotation.y -= ev.delta.x * camera.sensitivity;
camera.rotation.x -= ev.delta.y * camera.sensitivity;
camera.rotation.x =
f32::clamp(camera.rotation.x, -camera.rotate_lock, camera.rotate_lock);
//transform.rotation += Quat::from_axis_angle(Vec3::new(0., 1., 0.), angle);
//println!("{}",x_quat);
}
/*camera.recoil_shake = move_towards(
camera.recoil_shake,
Vec3::ZERO,
time.delta_seconds() * camera.camera_shake_readjustment_factor,
);*/
let x_quat = Quat::from_axis_angle(
Vec3::new(0., 1., 0.),
//camera.rotation.y - camera.recoil_shake.x,
camera.rotation.y
);
let y_quat = Quat::from_axis_angle(
Vec3::new(1., 0., 0.),
//camera.rotation.x + camera.recoil_shake.y,
camera.rotation.x
);
transform.rotation = x_quat * y_quat;
}
}
}*/
fn player_move(
mut player: Query<
(
//&mut KinematicCharacterController,
&mut Transform,
&Children,
&ActionState<PlayerAction>,
),
With<Player>,
>,
camera: Query<&GlobalTransform, With<PlayerCam>>,
) {
for (/*mut controller,*/ mut varposition, children, actions) in &mut player {
let mut delta = Vec3::default();
if actions.pressed(&PlayerAction::MoveUp) {
delta.z += 1.;
}
if actions.pressed(&PlayerAction::MoveDown) {
delta.z -= 1.;
}
if actions.pressed(&PlayerAction::MoveLeft) {
delta.x += 1.;
}
if actions.pressed(&PlayerAction::MoveRight) {
delta.x -= 1.;
}
let Some(child) = children.first().cloned() else {
error!("Player has not child entity");
continue;
};
let Ok(camera) = camera.get(child) else {
error!("first child is not camera");
continue;
};
let forward = camera.forward().as_vec3() * delta.z;
let left = camera.left().as_vec3() * delta.x;
delta = forward + left;
delta.y = 0.;
#[cfg(debug_assertions)]
if actions.pressed(&PlayerAction::FlyDown) {
delta.y -= 1.;
}
if actions.pressed(&PlayerAction::FlyUp) {
delta.y += 1.;
}
//if let Some(current) = &mut controller.translation {
if let Some(current) = &mut varposition {
*current += delta.normalize();
} else {
varposition = Some(delta.normalize());
}
}
}
fn player_look(
window: Query<&Window, With<PrimaryWindow>>,
mut player: Query<(&mut Transform, &Children, &ActionState<PlayerAction>), With<Player>>,
mut camera: Query<&mut Transform, (With<PlayerCam>, Without<Player>)>,
) {
let window = window.single();
if !window.focused {
return;
}
let scale = window.width().min(window.height()) / window.width();
for (mut body, children, actions) in &mut player {
let Some(child) = children.first().cloned() else {
error!("Player has not child entity");
continue;
};
let Ok(mut camera) = camera.get_mut(child) else {
error!("first child is not camera");
continue;
};
let look = actions.axis_pair(&PlayerAction::Look);
let (_, pitch, _) = camera.rotation.to_euler(EulerRot::YXZ);
camera.rotation = Quat::from_axis_angle(
Vec3::X,
(pitch - (look.y * scale).to_radians())
.clamp(-std::f32::consts::FRAC_PI_2, std::f32::consts::FRAC_PI_2),
);
let (yaw, _, _) = body.rotation.to_euler(EulerRot::YXZ);
body.rotation = Quat::from_axis_angle(Vec3::Y, yaw - (look.x * scale).to_radians());
}
}