start flashlight working

This commit is contained in:
Back777space
2025-04-06 13:38:16 +02:00
parent 975fc97402
commit bb6827b0f0
2 changed files with 57 additions and 13 deletions

View File

@@ -22,7 +22,7 @@ fn main() {
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
player::plugin,
debugging::plugin
// debugging::plugin
))
.init_state::<GameState>()
.add_systems(OnEnter(GameState::Playing), setup)
@@ -62,11 +62,11 @@ fn setup(
Collider::cuboid(0.5, 0.5, 0.5),
));
// light
commands.spawn((
PointLight {
shadows_enabled: true,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
));
// commands.spawn((
// PointLight {
// shadows_enabled: true,
// ..default()
// },
// Transform::from_xyz(4.0, 8.0, 4.0),
// ));
}

View File

@@ -51,12 +51,21 @@ impl Default for HeadBob {
#[derive(Component, Debug)]
pub struct BaseTransform(pub Transform);
#[derive(Debug, Component)]
pub struct Flashlight;
pub fn plugin(app: &mut App) {
app.add_plugins(toolbar::plugin)
.add_systems(OnEnter(GameState::Playing), (init_player, hide_cursor))
.add_systems(
Update,
(move_camera, handle_input, apply_head_bob, on_resize_system).run_if(in_state(GameState::Playing)),
(
move_camera,
handle_input,
apply_head_bob,
on_resize_system,
handle_flashlight,
).run_if(in_state(GameState::Playing)),
)
.add_systems(
FixedUpdate,
@@ -112,6 +121,7 @@ pub fn init_player(
RenderLayers::layer(STATIC_LAYER),
));
// pitslamp sprite
let window = window.single();
let transform = flashlight_base_transform(window.width(), window.height());
parent.spawn((
@@ -120,6 +130,19 @@ pub fn init_player(
transform,
RenderLayers::layer(STATIC_LAYER),
));
// feitelijke pitslamp
parent.spawn((
Flashlight,
PointLight {
intensity: 0.0, // start with the light off
color: Color::WHITE,
shadows_enabled: true,
..default()
},
Transform::from_xyz(1.0, 2.0, 0.0),
GlobalTransform::default(),
));
});
}
@@ -221,10 +244,10 @@ pub fn handle_input(
} else {
player.speed_factor = 1.0;
}
if keyboard_input.pressed(KeyCode::KeyE) {
if keyboard_input.just_pressed(KeyCode::KeyE) {
*action = PlayerAction::Interact
}
if keyboard_input.pressed(KeyCode::KeyA) {
if keyboard_input.just_pressed(KeyCode::KeyF) {
*action = PlayerAction::ToggleFlashlight;
}
@@ -295,7 +318,7 @@ pub fn apply_head_bob(
if is_moving {
transform.translation.x = horizontal_offset;
} else {
// decrease bobbing magnitued
// decrease bobbing magnitude
transform.translation.x *= 0.8;
}
}
@@ -321,3 +344,24 @@ pub fn apply_head_bob(
}
}
}
pub fn handle_flashlight(
mut player_query: Query<(&mut PlayerAction, Entity), With<Player>>,
mut flashlight_query: Query<&mut PointLight, With<Flashlight>>,
) {
let Ok((mut action, player_entity)) = player_query.get_single_mut() else {
return;
};
if *action != PlayerAction::ToggleFlashlight {
return;
}
if let Ok(mut point_light) = flashlight_query.get_mut(player_entity) {
point_light.intensity = if point_light.intensity > 0.0 {
0.0
} else {
100_000.0
};
println!("Flashlight {}", if point_light.intensity > 0.0 { "on" } else { "off" });
}
*action = PlayerAction::Move;
}