start flashlight working
This commit is contained in:
16
src/main.rs
16
src/main.rs
@@ -22,7 +22,7 @@ fn main() {
|
|||||||
RapierPhysicsPlugin::<NoUserData>::default(),
|
RapierPhysicsPlugin::<NoUserData>::default(),
|
||||||
RapierDebugRenderPlugin::default(),
|
RapierDebugRenderPlugin::default(),
|
||||||
player::plugin,
|
player::plugin,
|
||||||
debugging::plugin
|
// debugging::plugin
|
||||||
))
|
))
|
||||||
.init_state::<GameState>()
|
.init_state::<GameState>()
|
||||||
.add_systems(OnEnter(GameState::Playing), setup)
|
.add_systems(OnEnter(GameState::Playing), setup)
|
||||||
@@ -62,11 +62,11 @@ fn setup(
|
|||||||
Collider::cuboid(0.5, 0.5, 0.5),
|
Collider::cuboid(0.5, 0.5, 0.5),
|
||||||
));
|
));
|
||||||
// light
|
// light
|
||||||
commands.spawn((
|
// commands.spawn((
|
||||||
PointLight {
|
// PointLight {
|
||||||
shadows_enabled: true,
|
// shadows_enabled: true,
|
||||||
..default()
|
// ..default()
|
||||||
},
|
// },
|
||||||
Transform::from_xyz(4.0, 8.0, 4.0),
|
// Transform::from_xyz(4.0, 8.0, 4.0),
|
||||||
));
|
// ));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,12 +51,21 @@ impl Default for HeadBob {
|
|||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct BaseTransform(pub Transform);
|
pub struct BaseTransform(pub Transform);
|
||||||
|
|
||||||
|
#[derive(Debug, Component)]
|
||||||
|
pub struct Flashlight;
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
pub fn plugin(app: &mut App) {
|
||||||
app.add_plugins(toolbar::plugin)
|
app.add_plugins(toolbar::plugin)
|
||||||
.add_systems(OnEnter(GameState::Playing), (init_player, hide_cursor))
|
.add_systems(OnEnter(GameState::Playing), (init_player, hide_cursor))
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
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(
|
.add_systems(
|
||||||
FixedUpdate,
|
FixedUpdate,
|
||||||
@@ -112,6 +121,7 @@ pub fn init_player(
|
|||||||
RenderLayers::layer(STATIC_LAYER),
|
RenderLayers::layer(STATIC_LAYER),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
// pitslamp sprite
|
||||||
let window = window.single();
|
let window = window.single();
|
||||||
let transform = flashlight_base_transform(window.width(), window.height());
|
let transform = flashlight_base_transform(window.width(), window.height());
|
||||||
parent.spawn((
|
parent.spawn((
|
||||||
@@ -120,6 +130,19 @@ pub fn init_player(
|
|||||||
transform,
|
transform,
|
||||||
RenderLayers::layer(STATIC_LAYER),
|
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 {
|
} else {
|
||||||
player.speed_factor = 1.0;
|
player.speed_factor = 1.0;
|
||||||
}
|
}
|
||||||
if keyboard_input.pressed(KeyCode::KeyE) {
|
if keyboard_input.just_pressed(KeyCode::KeyE) {
|
||||||
*action = PlayerAction::Interact
|
*action = PlayerAction::Interact
|
||||||
}
|
}
|
||||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
if keyboard_input.just_pressed(KeyCode::KeyF) {
|
||||||
*action = PlayerAction::ToggleFlashlight;
|
*action = PlayerAction::ToggleFlashlight;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,7 +318,7 @@ pub fn apply_head_bob(
|
|||||||
if is_moving {
|
if is_moving {
|
||||||
transform.translation.x = horizontal_offset;
|
transform.translation.x = horizontal_offset;
|
||||||
} else {
|
} else {
|
||||||
// decrease bobbing magnitued
|
// decrease bobbing magnitude
|
||||||
transform.translation.x *= 0.8;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user