added second layer for start of pause menu stuff

This commit is contained in:
gyoder 2025-12-28 17:46:53 -07:00
parent a703f6ab85
commit 9de729fb7f
8 changed files with 247 additions and 11 deletions

View file

@ -1 +1,2 @@
pub mod navi;
pub mod pause;

View file

@ -1,4 +1,6 @@
use bevy::{post_process::bloom::Bloom, prelude::*, sprite::Anchor};
use bevy::{
camera::visibility::RenderLayers, post_process::bloom::Bloom, prelude::*, sprite::Anchor,
};
use rand::Rng;
use crate::{
@ -26,6 +28,10 @@ impl Plugin for NaviGameplayPlugin {
#[derive(Component)]
pub struct PlayerVWBus;
#[derive(Component)]
#[require(Camera2d)]
struct MainCamera;
pub fn setup_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
@ -36,7 +42,16 @@ pub fn setup_scene(
MeshMaterial2d(materials.add(Color::srgb(0.2, 0.2, 0.3))),
));
commands.spawn((Camera2d, Bloom::NATURAL));
commands.spawn((
MainCamera,
Camera {
order: 1,
// clear_color: ClearColorConfig::None,
..default()
},
Bloom::NATURAL,
RenderLayers::layer(0),
));
}
pub fn spawn_bus(mut commands: Commands, asset_server: Res<AssetServer>) {
@ -106,9 +121,9 @@ pub fn move_player(
}
}
pub fn follow_player(
mut camera: Single<&mut Transform, (With<Camera2d>, Without<PlayerVWBus>)>,
player: Single<&Transform, (With<PlayerVWBus>, Without<Camera2d>)>,
fn follow_player(
mut camera: Single<&mut Transform, (With<MainCamera>, Without<PlayerVWBus>)>,
player: Single<&Transform, (With<PlayerVWBus>, Without<MainCamera>)>,
time: Res<Time>,
) {
let Vec3 { x, y, .. } = player.translation;

115
src/gameplay/pause.rs Normal file
View file

@ -0,0 +1,115 @@
use bevy::{
camera::visibility::RenderLayers, post_process::bloom::Bloom, prelude::*, sprite::Anchor,
};
use crate::{
GameState, PauseState,
utils::input::{GameInputPlugin, InputButton, InputMessage, InputPressStatus},
};
const COLOR_BG: Color = Color::srgba_u8(249, 226, 156, 255);
const COLOR_HL1: Color = Color::srgba_u8(187, 241, 241, 255);
const COLOR_HL2: Color = Color::srgba_u8(192, 149, 145, 255);
pub struct PausePlugin;
impl Plugin for PausePlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup_pause).add_systems(
Update,
(
listen_for_pause.run_if(in_state(PauseState::Running)),
listen_for_unpause.run_if(in_state(PauseState::Paused)),
animate_in.run_if(in_state(PauseState::AnimatingIntoPause)),
animate_out.run_if(in_state(PauseState::AnimatingOutOfPause)),
),
);
}
}
#[derive(Component)]
pub struct PauseMenuRoot;
#[derive(Component)]
#[require(Camera2d)]
struct PauseCamera;
fn setup_pause(
mut commands: Commands,
assets: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn((
PauseCamera,
Camera {
order: 3,
clear_color: ClearColorConfig::None,
..default()
},
// Bloom::NATURAL,
RenderLayers::layer(3),
));
commands.spawn((
Mesh2d(meshes.add(Triangle2d::new(
Vec2::new(-100., -100.),
Vec2::new(-100., 100.),
Vec2::new(-100., 100.),
))),
MeshMaterial2d(materials.add(Color::srgb(0.2, 0.2, 0.3))),
RenderLayers::layer(3),
));
commands.spawn((
Node {
position_type: PositionType::Absolute,
width: percent(80),
height: percent(80),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
UiTransform::from_translation(Val2::px(0., 0.)),
BackgroundColor(COLOR_BG),
Visibility::Visible,
PauseMenuRoot,
));
}
fn listen_for_pause(
mut next: ResMut<NextState<PauseState>>,
mut btn_msgs: MessageReader<InputMessage>,
) {
for btn_msg in btn_msgs.read() {
if btn_msg.just_pressed(InputButton::Pause) {
next.set(PauseState::AnimatingIntoPause);
}
}
}
fn listen_for_unpause(
mut next: ResMut<NextState<PauseState>>,
mut btn_msgs: MessageReader<InputMessage>,
) {
for btn_msg in btn_msgs.read() {
if btn_msg.just_pressed(InputButton::Pause) {
next.set(PauseState::AnimatingOutOfPause);
}
}
}
fn animate_in(
mut menu_visibility: Single<&mut Visibility, With<PauseMenuRoot>>,
mut next_state: ResMut<NextState<PauseState>>,
) {
**menu_visibility = Visibility::Visible;
next_state.set(PauseState::Paused);
}
fn animate_out(
mut menu_visibility: Single<&mut Visibility, With<PauseMenuRoot>>,
mut next_state: ResMut<NextState<PauseState>>,
) {
**menu_visibility = Visibility::Hidden;
next_state.set(PauseState::Running);
}

View file

@ -1,8 +1,7 @@
use bevy::{post_process::bloom::Bloom, prelude::*, sprite::Anchor};
use rand::Rng;
use crate::{
gameplay::navi::NaviGameplayPlugin,
gameplay::{navi::NaviGameplayPlugin, pause::PausePlugin},
utils::input::{GameInputPlugin, InputButton, InputPressStatus},
};
@ -29,7 +28,12 @@ pub enum PauseState {
fn main() {
println!("Hello, world!");
App::new()
.add_plugins((DefaultPlugins, GameInputPlugin, NaviGameplayPlugin))
.add_plugins((
DefaultPlugins,
GameInputPlugin,
NaviGameplayPlugin,
PausePlugin,
))
.init_state::<GameState>()
.init_state::<PauseState>()
.run();

View file

@ -7,7 +7,7 @@ impl Plugin for GameInputPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(InputPressStatus::default())
.add_message::<InputMessage>()
.add_systems(Update, process_input);
.add_systems(Update, (process_input, input_debug_logger));
}
}
@ -28,7 +28,7 @@ fn process_input(
}
}
#[derive(EnumSetType)]
#[derive(EnumSetType, Debug)]
pub enum InputButton {
Up,
Down,
@ -39,12 +39,13 @@ pub enum InputButton {
Pause,
}
#[derive(PartialEq, Debug)]
pub enum InputAction {
Pressed,
Released,
}
#[derive(Message)]
#[derive(Message, Debug)]
pub struct InputMessage {
button: InputButton,
action: InputAction,
@ -73,6 +74,11 @@ impl InputMessage {
pub fn new(button: InputButton, action: InputAction) -> Self {
Self { button, action }
}
#[inline(always)]
pub fn just_pressed(&self, button: InputButton) -> bool {
self.button == button && self.action == InputAction::Pressed
}
}
impl KeyboardMapping {
@ -80,3 +86,9 @@ impl KeyboardMapping {
Self { key, button }
}
}
fn input_debug_logger(mut input_reader: MessageReader<InputMessage>) {
for msg in input_reader.read() {
info!("Input Event: {:?}", msg);
}
}