sprite working

This commit is contained in:
Back777space
2025-04-06 00:52:18 +02:00
22 changed files with 1424 additions and 79 deletions

View File

@@ -1,10 +1,11 @@
use bevy::{
input::mouse::AccumulatedMouseMotion, prelude::*,
render::view::RenderLayers, window::PrimaryWindow,
input::mouse::AccumulatedMouseMotion, prelude::*, render::view::RenderLayers, transform, window::{self, PrimaryWindow}
};
use bevy_rapier3d::prelude::*;
use crate::{asset_loading::{FlashlightAssets, ImageAssets}, GameState};
pub mod toolbar;
use crate::{asset_loading::{FlashlightAssets}, GameState};
#[derive(Debug, Component)]
pub struct Player;
@@ -26,7 +27,8 @@ pub struct PlayerInput {
}
pub fn plugin(app: &mut App) {
app.add_systems(OnEnter(GameState::Playing), (init_player, hide_cursor))
app.add_plugins(toolbar::plugin)
.add_systems(OnEnter(GameState::Playing), (init_player, hide_cursor))
.add_systems(
Update,
(move_camera, handle_input).run_if(in_state(GameState::Playing)),
@@ -43,14 +45,17 @@ const STATIC_LAYER: usize = 1;
pub fn init_player(
mut commands: Commands,
flashlights: Res<FlashlightAssets>,
images: Res<ImageAssets>,
window: Query<&Window>,
) {
let window = window.single();
commands
.spawn((
Player,
PlayerAction::default(),
CameraSensitivity::default(),
PlayerInput::default(),
toolbar::Item::none(),
// rapier
RigidBody::Dynamic,
Collider::capsule(Vec3::new(0.0, -0.5, 0.0), Vec3::new(0.0, 0.5, 0.0), 0.5),
@@ -60,7 +65,7 @@ pub fn init_player(
linear_damping: 6.0,
angular_damping: 1.0,
},
GravityScale(3.0),
Transform::from_xyz(0.0, 1.0, 0.0),
))
.with_children(|parent| {
@@ -75,26 +80,30 @@ pub fn init_player(
// camera voor pitslampke
parent.spawn((
Camera3d::default(),
Camera2d::default(),
Camera {
order: 1,
..default()
},
RenderLayers::layer(STATIC_LAYER),
));
// pitslampke
let window_size = Vec2::new(window.resolution.width(), window.resolution.height());
let sprite_size = Vec2::new(101.0, 101.0);
let scale = 2.2;
let world_size = sprite_size * scale;
let offset = 70.0;
let mut transform = Transform::from_translation(
Vec3::new(window_size.x / 2.0 - world_size.x / 2.0 - offset, -window_size.y / 2.0 + world_size.y / 2.0, 0.0)
);
transform.scale = Vec3::new(scale, scale, 1.0);
parent.spawn((
Transform::from_xyz(-3.0, -3.0, 0.0),
Sprite {
image: images.king.clone(),
..default()
},
RenderLayers::layer(STATIC_LAYER),
Sprite::from_image(flashlights.flash_hold_4.clone()),
transform,
RenderLayers::layer(STATIC_LAYER),
));
});
commands.spawn(Sprite::from_image(images.king.clone()));
}
fn hide_cursor(mut windows: Query<&mut Window, With<PrimaryWindow>>) {
@@ -125,11 +134,20 @@ pub fn move_camera(
}
}
#[derive(Component, Debug, Clone, Copy, Eq, PartialEq, Hash, Reflect, Default)]
pub(crate) enum PlayerAction {
#[default]
Move,
Sprint,
Jump,
Interact,
}
pub fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut query: Query<(&Transform, &mut PlayerInput), With<Player>>,
mut query: Query<(&Transform, &mut PlayerInput, &mut PlayerAction), With<Player>>,
) {
for (transform, mut input) in query.iter_mut() {
for (transform, mut input, mut action) in query.iter_mut() {
let forward = transform.forward();
let right = transform.right();
let mut movement_direction = Vec3::ZERO;
@@ -152,31 +170,32 @@ pub fn handle_input(
if keyboard_input.pressed(KeyCode::ShiftLeft) {
movement_direction -= Vec3::Y;
}
if keyboard_input.pressed(KeyCode::KeyE) {
*action = PlayerAction::Interact
}
input.movement_direction = movement_direction.normalize_or_zero();
}
}
pub fn apply_player_movement(
mut player_query: Query<(&PlayerInput, &mut Velocity), With<Player>>,
) {
pub fn apply_player_movement(mut player_query: Query<(&PlayerInput, &mut Velocity), With<Player>>) {
const SPEED: f32 = 3.0;
const JUMP_FORCE: f32 = 4.0;
for (input, mut velocity) in player_query.iter_mut() {
let horizontal_movement = Vec3::new(
input.movement_direction.x * SPEED,
0.0,
input.movement_direction.z * SPEED,
);
velocity.linvel.x = horizontal_movement.x;
velocity.linvel.z = horizontal_movement.z;
if input.movement_direction.y > 0.0 {
velocity.linvel.y = JUMP_FORCE;
} else if input.movement_direction.y < 0.0 {
velocity.linvel.y = -SPEED;
velocity.linvel.y = -SPEED;
}
}
}
}