Clientside Backpack UI (#84)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use crate::{GameState, HEDZ_GREEN, heads::HeadsImages, loading_assets::UIAssets};
|
use crate::{GameState, HEDZ_GREEN, heads::HeadsImages, loading_assets::UIAssets};
|
||||||
use bevy::{ecs::spawn::SpawnIter, prelude::*};
|
use bevy::{ecs::spawn::SpawnIter, prelude::*};
|
||||||
use shared::backpack::backpack_ui::{
|
use shared::backpack::backpack_ui::{
|
||||||
BackpackCountText, BackpackMarker, BackpackUiState, HEAD_SLOTS, HeadDamage, HeadImage,
|
BACKPACK_HEAD_SLOTS, BackpackCountText, BackpackMarker, BackpackUiState, HeadDamage, HeadImage,
|
||||||
HeadSelector,
|
HeadSelector,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ fn setup(mut commands: Commands, assets: Res<UIAssets>) {
|
|||||||
height: Val::Px(74.0),
|
height: Val::Px(74.0),
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
Children::spawn(SpawnIter((0..HEAD_SLOTS).map({
|
Children::spawn(SpawnIter((0..BACKPACK_HEAD_SLOTS).map({
|
||||||
let bg = assets.head_bg.clone();
|
let bg = assets.head_bg.clone();
|
||||||
let regular = assets.head_regular.clone();
|
let regular = assets.head_regular.clone();
|
||||||
let selector = assets.head_selector.clone();
|
let selector = assets.head_selector.clone();
|
||||||
|
|||||||
@@ -8,9 +8,8 @@ use bevy::{
|
|||||||
};
|
};
|
||||||
use shared::{
|
use shared::{
|
||||||
control::{
|
control::{
|
||||||
BackpackLeftPressed, BackpackRightPressed, BackpackSwapPressed, BackpackTogglePressed,
|
BackpackButtonPress, CashHealPressed, ClientInputs, ControllerSet, Inputs, LocalInputs,
|
||||||
CashHealPressed, ClientInputs, ControllerSet, Inputs, LocalInputs, LookDirMovement,
|
LookDirMovement, SelectLeftPressed, SelectRightPressed,
|
||||||
SelectLeftPressed, SelectRightPressed,
|
|
||||||
},
|
},
|
||||||
player::{LocalPlayer, PlayerBodyMesh},
|
player::{LocalPlayer, PlayerBodyMesh},
|
||||||
};
|
};
|
||||||
@@ -98,10 +97,7 @@ fn gamepad_controls(
|
|||||||
gamepads: Query<&Gamepad>,
|
gamepads: Query<&Gamepad>,
|
||||||
mut inputs: Single<&mut LocalInputs>,
|
mut inputs: Single<&mut LocalInputs>,
|
||||||
mut look_dir: ResMut<LookDirMovement>,
|
mut look_dir: ResMut<LookDirMovement>,
|
||||||
mut backpack_toggle_pressed: MessageWriter<BackpackTogglePressed>,
|
mut backpack_inputs: MessageWriter<BackpackButtonPress>,
|
||||||
mut backpack_swap_pressed: MessageWriter<BackpackSwapPressed>,
|
|
||||||
mut backpack_left_pressed: MessageWriter<BackpackLeftPressed>,
|
|
||||||
mut backpack_right_pressed: MessageWriter<BackpackRightPressed>,
|
|
||||||
mut select_left_pressed: MessageWriter<SelectLeftPressed>,
|
mut select_left_pressed: MessageWriter<SelectLeftPressed>,
|
||||||
mut select_right_pressed: MessageWriter<SelectRightPressed>,
|
mut select_right_pressed: MessageWriter<SelectRightPressed>,
|
||||||
mut cash_heal_pressed: MessageWriter<CashHealPressed>,
|
mut cash_heal_pressed: MessageWriter<CashHealPressed>,
|
||||||
@@ -139,19 +135,19 @@ fn gamepad_controls(
|
|||||||
inputs.0.trigger |= gamepad.pressed(GamepadButton::RightTrigger2);
|
inputs.0.trigger |= gamepad.pressed(GamepadButton::RightTrigger2);
|
||||||
|
|
||||||
if gamepad.just_pressed(GamepadButton::DPadUp) {
|
if gamepad.just_pressed(GamepadButton::DPadUp) {
|
||||||
backpack_toggle_pressed.write(BackpackTogglePressed);
|
backpack_inputs.write(BackpackButtonPress::Toggle);
|
||||||
}
|
}
|
||||||
|
|
||||||
if gamepad.just_pressed(GamepadButton::DPadDown) {
|
if gamepad.just_pressed(GamepadButton::DPadDown) {
|
||||||
backpack_swap_pressed.write(BackpackSwapPressed);
|
backpack_inputs.write(BackpackButtonPress::Swap);
|
||||||
}
|
}
|
||||||
|
|
||||||
if gamepad.just_pressed(GamepadButton::DPadLeft) {
|
if gamepad.just_pressed(GamepadButton::DPadLeft) {
|
||||||
backpack_left_pressed.write(BackpackLeftPressed);
|
backpack_inputs.write(BackpackButtonPress::Left);
|
||||||
}
|
}
|
||||||
|
|
||||||
if gamepad.just_pressed(GamepadButton::DPadRight) {
|
if gamepad.just_pressed(GamepadButton::DPadRight) {
|
||||||
backpack_right_pressed.write(BackpackRightPressed);
|
backpack_inputs.write(BackpackButtonPress::Right);
|
||||||
}
|
}
|
||||||
|
|
||||||
if gamepad.just_pressed(GamepadButton::LeftTrigger) {
|
if gamepad.just_pressed(GamepadButton::LeftTrigger) {
|
||||||
@@ -181,10 +177,7 @@ fn keyboard_controls(
|
|||||||
keyboard: Res<ButtonInput<KeyCode>>,
|
keyboard: Res<ButtonInput<KeyCode>>,
|
||||||
mouse: Res<ButtonInput<MouseButton>>,
|
mouse: Res<ButtonInput<MouseButton>>,
|
||||||
mut inputs: Single<&mut LocalInputs>,
|
mut inputs: Single<&mut LocalInputs>,
|
||||||
mut backpack_toggle_pressed: MessageWriter<BackpackTogglePressed>,
|
mut backpack_inputs: MessageWriter<BackpackButtonPress>,
|
||||||
mut backpack_swap_pressed: MessageWriter<BackpackSwapPressed>,
|
|
||||||
mut backpack_left_pressed: MessageWriter<BackpackLeftPressed>,
|
|
||||||
mut backpack_right_pressed: MessageWriter<BackpackRightPressed>,
|
|
||||||
mut select_left_pressed: MessageWriter<SelectLeftPressed>,
|
mut select_left_pressed: MessageWriter<SelectLeftPressed>,
|
||||||
mut select_right_pressed: MessageWriter<SelectRightPressed>,
|
mut select_right_pressed: MessageWriter<SelectRightPressed>,
|
||||||
mut cash_heal_pressed: MessageWriter<CashHealPressed>,
|
mut cash_heal_pressed: MessageWriter<CashHealPressed>,
|
||||||
@@ -209,19 +202,19 @@ fn keyboard_controls(
|
|||||||
inputs.0.trigger = mouse.pressed(MouseButton::Left);
|
inputs.0.trigger = mouse.pressed(MouseButton::Left);
|
||||||
|
|
||||||
if keyboard.just_pressed(KeyCode::KeyB) {
|
if keyboard.just_pressed(KeyCode::KeyB) {
|
||||||
backpack_toggle_pressed.write(BackpackTogglePressed);
|
backpack_inputs.write(BackpackButtonPress::Toggle);
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyboard.just_pressed(KeyCode::Enter) {
|
if keyboard.just_pressed(KeyCode::Enter) {
|
||||||
backpack_swap_pressed.write(BackpackSwapPressed);
|
backpack_inputs.write(BackpackButtonPress::Swap);
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyboard.just_pressed(KeyCode::Comma) {
|
if keyboard.just_pressed(KeyCode::Comma) {
|
||||||
backpack_left_pressed.write(BackpackLeftPressed);
|
backpack_inputs.write(BackpackButtonPress::Left);
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyboard.just_pressed(KeyCode::Period) {
|
if keyboard.just_pressed(KeyCode::Period) {
|
||||||
backpack_right_pressed.write(BackpackRightPressed);
|
backpack_inputs.write(BackpackButtonPress::Right);
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyboard.just_pressed(KeyCode::KeyQ) {
|
if keyboard.just_pressed(KeyCode::KeyQ) {
|
||||||
|
|||||||
@@ -1,128 +0,0 @@
|
|||||||
use bevy::prelude::*;
|
|
||||||
use bevy_replicon::prelude::{ClientState, FromClient, SendMode, ServerTriggerExt, ToClients};
|
|
||||||
use shared::{
|
|
||||||
GameState,
|
|
||||||
backpack::{
|
|
||||||
BackbackSwapEvent, Backpack, UiHeadState,
|
|
||||||
backpack_ui::{BackpackUiState, HEAD_SLOTS},
|
|
||||||
},
|
|
||||||
control::{
|
|
||||||
BackpackLeftPressed, BackpackRightPressed, BackpackSwapPressed, BackpackTogglePressed,
|
|
||||||
},
|
|
||||||
protocol::{ClientToController, PlaySound},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
|
||||||
app.add_systems(
|
|
||||||
FixedUpdate,
|
|
||||||
sync_on_change.run_if(in_state(GameState::Playing)),
|
|
||||||
);
|
|
||||||
app.add_systems(
|
|
||||||
FixedUpdate,
|
|
||||||
swap_head_inputs.run_if(in_state(ClientState::Disconnected)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
fn swap_head_inputs(
|
|
||||||
backpacks: Query<Ref<Backpack>>,
|
|
||||||
clients: ClientToController,
|
|
||||||
mut backpack_toggles: MessageReader<FromClient<BackpackTogglePressed>>,
|
|
||||||
mut backpack_lefts: MessageReader<FromClient<BackpackLeftPressed>>,
|
|
||||||
mut backpack_rights: MessageReader<FromClient<BackpackRightPressed>>,
|
|
||||||
mut backpack_swaps: MessageReader<FromClient<BackpackSwapPressed>>,
|
|
||||||
mut commands: Commands,
|
|
||||||
mut state: Single<&mut BackpackUiState>,
|
|
||||||
time: Res<Time>,
|
|
||||||
) {
|
|
||||||
for _ in backpack_toggles.read() {
|
|
||||||
if state.count == 0 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
state.open = !state.open;
|
|
||||||
commands.server_trigger(ToClients {
|
|
||||||
mode: SendMode::Broadcast,
|
|
||||||
message: PlaySound::Backpack { open: state.open },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for press in backpack_lefts.read() {
|
|
||||||
let player = clients.get_controller(press.client_id);
|
|
||||||
let backpack = backpacks.get(player).unwrap();
|
|
||||||
|
|
||||||
if !state.open {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if state.current_slot > 0 {
|
|
||||||
state.current_slot -= 1;
|
|
||||||
|
|
||||||
commands.server_trigger(ToClients {
|
|
||||||
mode: SendMode::Broadcast,
|
|
||||||
message: PlaySound::Selection,
|
|
||||||
});
|
|
||||||
sync(&backpack, &mut state, time.elapsed_secs());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for press in backpack_rights.read() {
|
|
||||||
let player = clients.get_controller(press.client_id);
|
|
||||||
let backpack = backpacks.get(player).unwrap();
|
|
||||||
|
|
||||||
if !state.open {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if state.current_slot < state.count.saturating_sub(1) {
|
|
||||||
state.current_slot += 1;
|
|
||||||
|
|
||||||
commands.server_trigger(ToClients {
|
|
||||||
mode: SendMode::Broadcast,
|
|
||||||
message: PlaySound::Selection,
|
|
||||||
});
|
|
||||||
sync(&backpack, &mut state, time.elapsed_secs());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _ in backpack_swaps.read() {
|
|
||||||
if !state.open {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
commands.trigger(BackbackSwapEvent(state.current_slot));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sync_on_change(
|
|
||||||
backpack: Query<Ref<Backpack>>,
|
|
||||||
mut state: Single<&mut BackpackUiState>,
|
|
||||||
time: Res<Time>,
|
|
||||||
) {
|
|
||||||
for backpack in backpack.iter() {
|
|
||||||
if backpack.is_changed() || backpack.reloading() {
|
|
||||||
sync(&backpack, &mut state, time.elapsed_secs());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sync(backpack: &Backpack, state: &mut Single<&mut BackpackUiState>, time: f32) {
|
|
||||||
state.count = backpack.heads.len();
|
|
||||||
|
|
||||||
state.scroll = state.scroll.min(state.count.saturating_sub(HEAD_SLOTS));
|
|
||||||
|
|
||||||
if state.current_slot >= state.scroll + HEAD_SLOTS {
|
|
||||||
state.scroll = state.current_slot.saturating_sub(HEAD_SLOTS - 1);
|
|
||||||
}
|
|
||||||
if state.current_slot < state.scroll {
|
|
||||||
state.scroll = state.current_slot;
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in 0..HEAD_SLOTS {
|
|
||||||
if let Some(head) = backpack.heads.get(i + state.scroll) {
|
|
||||||
state.heads[i] = Some(UiHeadState::new(*head, time));
|
|
||||||
} else {
|
|
||||||
state.heads[i] = None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
pub mod backpack_ui;
|
|
||||||
|
|
||||||
use bevy::prelude::*;
|
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
|
||||||
app.add_plugins(backpack_ui::plugin);
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,6 @@ use bevy_trenchbroom::prelude::*;
|
|||||||
use bevy_trenchbroom_avian::AvianPhysicsBackend;
|
use bevy_trenchbroom_avian::AvianPhysicsBackend;
|
||||||
use shared::{DebugVisuals, GameState, heads_database::HeadDatabaseAsset};
|
use shared::{DebugVisuals, GameState, heads_database::HeadDatabaseAsset};
|
||||||
|
|
||||||
mod backpack;
|
|
||||||
mod config;
|
mod config;
|
||||||
mod head_drop;
|
mod head_drop;
|
||||||
mod platforms;
|
mod platforms;
|
||||||
@@ -119,7 +118,6 @@ fn main() {
|
|||||||
app.add_plugins(server::plugin);
|
app.add_plugins(server::plugin);
|
||||||
app.add_plugins(shared::protocol::plugin);
|
app.add_plugins(shared::protocol::plugin);
|
||||||
|
|
||||||
app.add_plugins(backpack::plugin);
|
|
||||||
app.add_plugins(config::plugin);
|
app.add_plugins(config::plugin);
|
||||||
app.add_plugins(head_drop::plugin);
|
app.add_plugins(head_drop::plugin);
|
||||||
app.add_plugins(platforms::plugin);
|
app.add_plugins(platforms::plugin);
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
use super::UiHeadState;
|
use super::UiHeadState;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
pub static HEAD_SLOTS: usize = 5;
|
pub static BACKPACK_HEAD_SLOTS: usize = 5;
|
||||||
|
|
||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
pub struct BackpackMarker;
|
pub struct BackpackMarker;
|
||||||
@@ -19,7 +18,7 @@ pub struct HeadImage(pub usize);
|
|||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
pub struct HeadDamage(pub usize);
|
pub struct HeadDamage(pub usize);
|
||||||
|
|
||||||
#[derive(Component, Default, Debug, Reflect, Serialize, Deserialize, PartialEq)]
|
#[derive(Component, Default, Debug, Reflect)]
|
||||||
#[reflect(Component, Default)]
|
#[reflect(Component, Default)]
|
||||||
pub struct BackpackUiState {
|
pub struct BackpackUiState {
|
||||||
pub heads: [Option<UiHeadState>; 5],
|
pub heads: [Option<UiHeadState>; 5],
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
|
#[cfg(feature = "client")]
|
||||||
|
use crate::{
|
||||||
|
backpack::backpack_ui::BackpackUiState, control::BackpackButtonPress, player::LocalPlayer,
|
||||||
|
protocol::PlaySound,
|
||||||
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
cash::CashCollectEvent, global_observer, head_drop::HeadCollected, heads::HeadState,
|
cash::CashCollectEvent, global_observer, head_drop::HeadCollected, heads::HeadState,
|
||||||
heads_database::HeadsDatabase,
|
heads_database::HeadsDatabase,
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
use bevy_replicon::prelude::ClientTriggerExt;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
pub use ui_head_state::UiHeadState;
|
pub use ui_head_state::UiHeadState;
|
||||||
|
|
||||||
@@ -35,17 +42,116 @@ impl Backpack {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Event)]
|
#[derive(Event, Serialize, Deserialize)]
|
||||||
pub struct BackbackSwapEvent(pub usize);
|
pub struct BackpackSwapEvent(pub usize);
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
pub fn plugin(app: &mut App) {
|
||||||
app.register_type::<Backpack>();
|
app.register_type::<Backpack>();
|
||||||
|
|
||||||
app.add_plugins(backpack_ui::plugin);
|
app.add_plugins(backpack_ui::plugin);
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
app.add_systems(FixedUpdate, (backpack_inputs, sync_on_change));
|
||||||
|
|
||||||
global_observer!(app, on_head_collect);
|
global_observer!(app, on_head_collect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
fn backpack_inputs(
|
||||||
|
backpacks: Single<(&Backpack, &mut BackpackUiState), With<LocalPlayer>>,
|
||||||
|
mut backpack_inputs: MessageReader<BackpackButtonPress>,
|
||||||
|
mut commands: Commands,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
let (backpack, mut state) = backpacks.into_inner();
|
||||||
|
|
||||||
|
for input in backpack_inputs.read() {
|
||||||
|
match input {
|
||||||
|
BackpackButtonPress::Toggle => {
|
||||||
|
if state.count == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
state.open = !state.open;
|
||||||
|
commands.trigger(PlaySound::Backpack { open: state.open });
|
||||||
|
}
|
||||||
|
BackpackButtonPress::Swap => {
|
||||||
|
if !state.open {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
commands.client_trigger(BackpackSwapEvent(state.current_slot));
|
||||||
|
}
|
||||||
|
BackpackButtonPress::Left => {
|
||||||
|
if !state.open {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if state.current_slot > 0 {
|
||||||
|
state.current_slot -= 1;
|
||||||
|
|
||||||
|
commands.trigger(PlaySound::Selection);
|
||||||
|
sync_backpack_ui(backpack, &mut state, time.elapsed_secs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BackpackButtonPress::Right => {
|
||||||
|
if !state.open {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if state.current_slot < state.count.saturating_sub(1) {
|
||||||
|
state.current_slot += 1;
|
||||||
|
|
||||||
|
commands.trigger(PlaySound::Selection);
|
||||||
|
sync_backpack_ui(backpack, &mut state, time.elapsed_secs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
fn sync_on_change(
|
||||||
|
backpack: Query<Ref<Backpack>>,
|
||||||
|
mut state: Single<&mut BackpackUiState>,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
for backpack in backpack.iter() {
|
||||||
|
if backpack.is_changed() || backpack.reloading() {
|
||||||
|
sync_backpack_ui(&backpack, &mut state, time.elapsed_secs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
fn sync_backpack_ui(backpack: &Backpack, state: &mut BackpackUiState, time: f32) {
|
||||||
|
use crate::backpack::backpack_ui::BACKPACK_HEAD_SLOTS;
|
||||||
|
|
||||||
|
state.count = backpack.heads.len();
|
||||||
|
|
||||||
|
state.scroll = state
|
||||||
|
.scroll
|
||||||
|
.min(state.count.saturating_sub(BACKPACK_HEAD_SLOTS));
|
||||||
|
|
||||||
|
if state.current_slot >= state.scroll + BACKPACK_HEAD_SLOTS {
|
||||||
|
state.scroll = state.current_slot.saturating_sub(BACKPACK_HEAD_SLOTS - 1);
|
||||||
|
}
|
||||||
|
if state.current_slot < state.scroll {
|
||||||
|
state.scroll = state.current_slot;
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in 0..BACKPACK_HEAD_SLOTS {
|
||||||
|
if let Some(head) = backpack.heads.get(i + state.scroll) {
|
||||||
|
use crate::backpack::ui_head_state::UiHeadState;
|
||||||
|
|
||||||
|
state.heads[i] = Some(UiHeadState::new(*head, time));
|
||||||
|
} else {
|
||||||
|
state.heads[i] = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn on_head_collect(
|
fn on_head_collect(
|
||||||
trigger: On<HeadCollected>,
|
trigger: On<HeadCollected>,
|
||||||
mut cmds: Commands,
|
mut cmds: Commands,
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ use crate::{
|
|||||||
abilities::TriggerStateRes,
|
abilities::TriggerStateRes,
|
||||||
animation::AnimationFlags,
|
animation::AnimationFlags,
|
||||||
control::{ControllerSettings, Inputs, SelectedController},
|
control::{ControllerSettings, Inputs, SelectedController},
|
||||||
head::ActiveHead,
|
|
||||||
heads_database::{HeadControls, HeadsDatabase},
|
|
||||||
physics_layers::GameLayer,
|
physics_layers::GameLayer,
|
||||||
player::{Player, PlayerBodyMesh},
|
player::{Player, PlayerBodyMesh},
|
||||||
};
|
};
|
||||||
@@ -40,8 +38,7 @@ pub fn plugin(app: &mut App) {
|
|||||||
.after(ControllerSet::ApplyControlsRun)
|
.after(ControllerSet::ApplyControlsRun)
|
||||||
.after(ControllerSet::ApplyControlsFly)
|
.after(ControllerSet::ApplyControlsFly)
|
||||||
.run_if(in_state(GameState::Playing)),
|
.run_if(in_state(GameState::Playing)),
|
||||||
)
|
);
|
||||||
.add_systems(FixedPostUpdate, add_controller_bundle);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_animation_flags(
|
fn set_animation_flags(
|
||||||
@@ -79,7 +76,7 @@ pub fn reset_upon_switch(
|
|||||||
mut event_controller_switch: MessageReader<ControllerSwitchEvent>,
|
mut event_controller_switch: MessageReader<ControllerSwitchEvent>,
|
||||||
selected_controller: Res<SelectedController>,
|
selected_controller: Res<SelectedController>,
|
||||||
mut rig_transforms: Query<&mut Transform, With<PlayerBodyMesh>>,
|
mut rig_transforms: Query<&mut Transform, With<PlayerBodyMesh>>,
|
||||||
mut controllers: Query<(&mut KinematicVelocity, &Children, &Inputs), With<Character>>,
|
mut controllers: Query<(&mut KinematicVelocity, &Children, &Inputs), With<Player>>,
|
||||||
) {
|
) {
|
||||||
for &ControllerSwitchEvent { controller } in event_controller_switch.read() {
|
for &ControllerSwitchEvent { controller } in event_controller_switch.read() {
|
||||||
let (mut velocity, children, inputs) = controllers.get_mut(controller).unwrap();
|
let (mut velocity, children, inputs) = controllers.get_mut(controller).unwrap();
|
||||||
@@ -151,69 +148,35 @@ fn decelerate(
|
|||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct MovementSpeedFactor(pub f32);
|
pub struct MovementSpeedFactor(pub f32);
|
||||||
|
|
||||||
#[derive(Component, Reflect, Default, Debug, Serialize, Deserialize, PartialEq)]
|
impl Default for MovementSpeedFactor {
|
||||||
#[reflect(Component)]
|
fn default() -> Self {
|
||||||
pub struct PlayerCharacterController;
|
Self(1.0)
|
||||||
|
|
||||||
fn add_controller_bundle(
|
|
||||||
mut commands: Commands,
|
|
||||||
controllers: Query<
|
|
||||||
(Entity, &ActiveHead),
|
|
||||||
(With<PlayerCharacterController>, Without<Character>),
|
|
||||||
>,
|
|
||||||
db: If<Res<HeadsDatabase>>,
|
|
||||||
) {
|
|
||||||
for (controller, head) in controllers {
|
|
||||||
let controls = db.head_stats(head.0).controls;
|
|
||||||
commands
|
|
||||||
.entity(controller)
|
|
||||||
.insert(CharacterControllerBundle::new(controls));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A bundle that contains the components needed for a basic
|
#[derive(Component, Reflect, Default, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
/// kinematic character controller.
|
#[reflect(Component)]
|
||||||
#[derive(Bundle)]
|
#[require(
|
||||||
pub struct CharacterControllerBundle {
|
Character,
|
||||||
character_controller: Character,
|
RigidBody::Kinematic,
|
||||||
rigidbody: RigidBody,
|
Collider::capsule(0.9, 1.2),
|
||||||
collider: Collider,
|
CollisionLayers::new(
|
||||||
move_input: MoveInput,
|
|
||||||
movement_factor: MovementSpeedFactor,
|
|
||||||
collision_messages: CollisionEventsEnabled,
|
|
||||||
movement_config: MovementConfig,
|
|
||||||
layers: CollisionLayers,
|
|
||||||
interpolation: TransformInterpolation,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CharacterControllerBundle {
|
|
||||||
pub fn new(controls: HeadControls) -> Self {
|
|
||||||
// Create shape caster as a slightly smaller version of collider
|
|
||||||
let collider = Collider::capsule(0.9, 1.2);
|
|
||||||
let mut caster_shape = collider.clone();
|
|
||||||
caster_shape.set_scale(Vector::ONE * 0.98, 10);
|
|
||||||
|
|
||||||
let config = match controls {
|
|
||||||
HeadControls::Plane => FLYING_MOVEMENT_CONFIG,
|
|
||||||
HeadControls::Walk => RUNNING_MOVEMENT_CONFIG,
|
|
||||||
};
|
|
||||||
|
|
||||||
Self {
|
|
||||||
character_controller: Character,
|
|
||||||
rigidbody: RigidBody::Kinematic,
|
|
||||||
collider,
|
|
||||||
move_input: MoveInput::default(),
|
|
||||||
movement_factor: MovementSpeedFactor(1.0),
|
|
||||||
collision_messages: CollisionEventsEnabled,
|
|
||||||
movement_config: config,
|
|
||||||
layers: CollisionLayers::new(
|
|
||||||
LayerMask(GameLayer::Player.to_bits()),
|
LayerMask(GameLayer::Player.to_bits()),
|
||||||
LayerMask::ALL & !GameLayer::CollectiblePhysics.to_bits(),
|
LayerMask::ALL & !GameLayer::CollectiblePhysics.to_bits(),
|
||||||
),
|
),
|
||||||
interpolation: TransformInterpolation,
|
CollisionEventsEnabled,
|
||||||
}
|
MoveInput,
|
||||||
}
|
MovementSpeedFactor,
|
||||||
}
|
TransformInterpolation,
|
||||||
|
CharacterMovement = RUNNING_MOVEMENT_CONFIG.movement,
|
||||||
|
ControllerSettings = RUNNING_MOVEMENT_CONFIG.settings,
|
||||||
|
CharacterGravity = RUNNING_MOVEMENT_CONFIG.gravity,
|
||||||
|
CharacterDrag = RUNNING_MOVEMENT_CONFIG.drag,
|
||||||
|
SteppingConfig = RUNNING_MOVEMENT_CONFIG.step,
|
||||||
|
GroundFriction = RUNNING_MOVEMENT_CONFIG.friction,
|
||||||
|
GroundingConfig = RUNNING_MOVEMENT_CONFIG.ground,
|
||||||
|
)]
|
||||||
|
pub struct PlayerCharacterController;
|
||||||
|
|
||||||
#[derive(Bundle)]
|
#[derive(Bundle)]
|
||||||
struct MovementConfig {
|
struct MovementConfig {
|
||||||
|
|||||||
@@ -38,7 +38,8 @@ pub fn plugin(app: &mut App) {
|
|||||||
app.init_resource::<LookDirMovement>();
|
app.init_resource::<LookDirMovement>();
|
||||||
app.init_resource::<SelectedController>();
|
app.init_resource::<SelectedController>();
|
||||||
|
|
||||||
app.add_message::<ControllerSwitchEvent>();
|
app.add_message::<ControllerSwitchEvent>()
|
||||||
|
.add_message::<BackpackButtonPress>();
|
||||||
|
|
||||||
app.add_plugins(controller_common::plugin);
|
app.add_plugins(controller_common::plugin);
|
||||||
app.add_plugins(controller_flying::CharacterControllerPlugin);
|
app.add_plugins(controller_flying::CharacterControllerPlugin);
|
||||||
@@ -114,17 +115,13 @@ pub struct SelectLeftPressed;
|
|||||||
#[derive(Message, Serialize, Deserialize)]
|
#[derive(Message, Serialize, Deserialize)]
|
||||||
pub struct SelectRightPressed;
|
pub struct SelectRightPressed;
|
||||||
|
|
||||||
#[derive(Message, Serialize, Deserialize)]
|
#[derive(Message)]
|
||||||
pub struct BackpackTogglePressed;
|
pub enum BackpackButtonPress {
|
||||||
|
Toggle,
|
||||||
#[derive(Message, Serialize, Deserialize)]
|
Swap,
|
||||||
pub struct BackpackSwapPressed;
|
Left,
|
||||||
|
Right,
|
||||||
#[derive(Message, Serialize, Deserialize)]
|
}
|
||||||
pub struct BackpackLeftPressed;
|
|
||||||
|
|
||||||
#[derive(Message, Serialize, Deserialize)]
|
|
||||||
pub struct BackpackRightPressed;
|
|
||||||
|
|
||||||
#[derive(Message, Serialize, Deserialize)]
|
#[derive(Message, Serialize, Deserialize)]
|
||||||
pub struct CashHealPressed;
|
pub struct CashHealPressed;
|
||||||
|
|||||||
@@ -1,22 +1,16 @@
|
|||||||
#[cfg(feature = "server")]
|
|
||||||
use crate::protocol::PlaySound;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
GameState,
|
GameState,
|
||||||
backpack::{BackbackSwapEvent, Backpack},
|
animation::AnimationFlags,
|
||||||
|
backpack::{Backpack, BackpackSwapEvent},
|
||||||
|
control::{SelectLeftPressed, SelectRightPressed},
|
||||||
global_observer,
|
global_observer,
|
||||||
heads_database::HeadsDatabase,
|
heads_database::HeadsDatabase,
|
||||||
hitpoints::Hitpoints,
|
hitpoints::Hitpoints,
|
||||||
player::Player,
|
player::Player,
|
||||||
};
|
protocol::{ClientToController, PlaySound},
|
||||||
#[cfg(feature = "server")]
|
|
||||||
use crate::{
|
|
||||||
animation::AnimationFlags,
|
|
||||||
control::{SelectLeftPressed, SelectRightPressed},
|
|
||||||
protocol::ClientToController,
|
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
#[cfg(feature = "server")]
|
use bevy_replicon::prelude::{ClientState, FromClient};
|
||||||
use bevy_replicon::prelude::FromClient;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub mod heads_ui;
|
pub mod heads_ui;
|
||||||
@@ -188,13 +182,14 @@ pub fn plugin(app: &mut App) {
|
|||||||
app.register_type::<ActiveHeads>();
|
app.register_type::<ActiveHeads>();
|
||||||
|
|
||||||
app.add_systems(OnEnter(GameState::Playing), setup);
|
app.add_systems(OnEnter(GameState::Playing), setup);
|
||||||
#[cfg(feature = "server")]
|
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
FixedUpdate,
|
FixedUpdate,
|
||||||
|
(
|
||||||
(reload, sync_hp).run_if(in_state(GameState::Playing)),
|
(reload, sync_hp).run_if(in_state(GameState::Playing)),
|
||||||
|
on_select_active_head,
|
||||||
|
)
|
||||||
|
.run_if(in_state(ClientState::Disconnected)),
|
||||||
);
|
);
|
||||||
#[cfg(feature = "server")]
|
|
||||||
app.add_systems(FixedUpdate, on_select_active_head);
|
|
||||||
|
|
||||||
global_observer!(app, on_swap_backpack);
|
global_observer!(app, on_swap_backpack);
|
||||||
}
|
}
|
||||||
@@ -208,7 +203,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, heads: Res<Head
|
|||||||
commands.insert_resource(HeadsImages { heads });
|
commands.insert_resource(HeadsImages { heads });
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "server")]
|
|
||||||
fn sync_hp(mut query: Query<(&mut ActiveHeads, &Hitpoints)>) {
|
fn sync_hp(mut query: Query<(&mut ActiveHeads, &Hitpoints)>) {
|
||||||
for (mut active_heads, hp) in query.iter_mut() {
|
for (mut active_heads, hp) in query.iter_mut() {
|
||||||
if active_heads.hp().get() != hp.get() {
|
if active_heads.hp().get() != hp.get() {
|
||||||
@@ -217,7 +211,6 @@ fn sync_hp(mut query: Query<(&mut ActiveHeads, &Hitpoints)>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "server")]
|
|
||||||
fn reload(
|
fn reload(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut active: Query<&mut ActiveHeads>,
|
mut active: Query<&mut ActiveHeads>,
|
||||||
@@ -249,7 +242,6 @@ fn reload(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "server")]
|
|
||||||
fn on_select_active_head(
|
fn on_select_active_head(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut query: Query<(&mut ActiveHeads, &mut Hitpoints), With<Player>>,
|
mut query: Query<(&mut ActiveHeads, &mut Hitpoints), With<Player>>,
|
||||||
@@ -305,7 +297,7 @@ fn on_select_active_head(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn on_swap_backpack(
|
fn on_swap_backpack(
|
||||||
trigger: On<BackbackSwapEvent>,
|
trigger: On<FromClient<BackpackSwapEvent>>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut query: Query<(&mut ActiveHeads, &mut Hitpoints, &mut Backpack), With<Player>>,
|
mut query: Query<(&mut ActiveHeads, &mut Hitpoints, &mut Backpack), With<Player>>,
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
#[cfg(feature = "client")]
|
|
||||||
use crate::control::LocalInputs;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
GameState,
|
GameState,
|
||||||
cash::{Cash, CashCollectEvent},
|
cash::{Cash, CashCollectEvent},
|
||||||
character::HedzCharacter,
|
character::HedzCharacter,
|
||||||
protocol::PlayerId,
|
protocol::PlayerId,
|
||||||
};
|
};
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
use crate::{backpack::backpack_ui::BackpackUiState, control::LocalInputs};
|
||||||
use avian3d::prelude::*;
|
use avian3d::prelude::*;
|
||||||
use bevy::{
|
use bevy::{
|
||||||
input::common_conditions::input_just_pressed,
|
input::common_conditions::input_just_pressed,
|
||||||
@@ -22,7 +22,7 @@ pub struct Player;
|
|||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
#[derive(Component, Debug, Reflect)]
|
#[derive(Component, Debug, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
#[require(LocalInputs)]
|
#[require(LocalInputs, BackpackUiState)]
|
||||||
pub struct LocalPlayer;
|
pub struct LocalPlayer;
|
||||||
|
|
||||||
#[derive(Component, Default, Serialize, Deserialize, PartialEq)]
|
#[derive(Component, Default, Serialize, Deserialize, PartialEq)]
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ use crate::{
|
|||||||
GameState,
|
GameState,
|
||||||
abilities::{BuildExplosionSprite, curver::CurverProjectile, healing::Healing},
|
abilities::{BuildExplosionSprite, curver::CurverProjectile, healing::Healing},
|
||||||
animation::AnimationFlags,
|
animation::AnimationFlags,
|
||||||
backpack::{Backpack, backpack_ui::BackpackUiState},
|
backpack::{Backpack, BackpackSwapEvent},
|
||||||
camera::{CameraArmRotation, CameraTarget},
|
camera::{CameraArmRotation, CameraTarget},
|
||||||
cash::CashInventory,
|
cash::CashInventory,
|
||||||
character::{AnimatedCharacter, HedzCharacter},
|
character::{AnimatedCharacter, HedzCharacter},
|
||||||
control::{
|
control::{
|
||||||
BackpackLeftPressed, BackpackRightPressed, BackpackSwapPressed, BackpackTogglePressed,
|
|
||||||
CashHealPressed, ClientInputs, ControllerSettings, Inputs, SelectLeftPressed,
|
CashHealPressed, ClientInputs, ControllerSettings, Inputs, SelectLeftPressed,
|
||||||
SelectRightPressed,
|
SelectRightPressed,
|
||||||
controller_common::{MovementSpeedFactor, PlayerCharacterController},
|
controller_common::{MovementSpeedFactor, PlayerCharacterController},
|
||||||
@@ -54,13 +53,10 @@ pub fn plugin(app: &mut App) {
|
|||||||
app.add_client_message::<ClientInputs>(Channel::Unreliable)
|
app.add_client_message::<ClientInputs>(Channel::Unreliable)
|
||||||
.add_client_message::<SelectLeftPressed>(Channel::Ordered)
|
.add_client_message::<SelectLeftPressed>(Channel::Ordered)
|
||||||
.add_client_message::<SelectRightPressed>(Channel::Ordered)
|
.add_client_message::<SelectRightPressed>(Channel::Ordered)
|
||||||
.add_client_message::<BackpackTogglePressed>(Channel::Ordered)
|
|
||||||
.add_client_message::<BackpackSwapPressed>(Channel::Ordered)
|
|
||||||
.add_client_message::<BackpackLeftPressed>(Channel::Ordered)
|
|
||||||
.add_client_message::<BackpackRightPressed>(Channel::Ordered)
|
|
||||||
.add_client_message::<CashHealPressed>(Channel::Ordered);
|
.add_client_message::<CashHealPressed>(Channel::Ordered);
|
||||||
|
|
||||||
app.add_client_event::<ClientEnteredPlaying>(Channel::Ordered);
|
app.add_client_event::<ClientEnteredPlaying>(Channel::Ordered)
|
||||||
|
.add_client_event::<BackpackSwapEvent>(Channel::Ordered);
|
||||||
|
|
||||||
app.add_server_message::<messages::DespawnTbMapEntity>(Channel::Unordered)
|
app.add_server_message::<messages::DespawnTbMapEntity>(Channel::Unordered)
|
||||||
.add_server_message::<messages::AssignClientPlayer>(Channel::Unordered);
|
.add_server_message::<messages::AssignClientPlayer>(Channel::Unordered);
|
||||||
@@ -94,7 +90,6 @@ pub fn plugin(app: &mut App) {
|
|||||||
.replicate_once::<AutoRotation>()
|
.replicate_once::<AutoRotation>()
|
||||||
.replicate_once::<CurverProjectile>()
|
.replicate_once::<CurverProjectile>()
|
||||||
.replicate::<Backpack>()
|
.replicate::<Backpack>()
|
||||||
.replicate::<BackpackUiState>()
|
|
||||||
.replicate::<Billboard>()
|
.replicate::<Billboard>()
|
||||||
.replicate_once::<CameraArmRotation>()
|
.replicate_once::<CameraArmRotation>()
|
||||||
.replicate_once::<CameraTarget>()
|
.replicate_once::<CameraTarget>()
|
||||||
|
|||||||
Reference in New Issue
Block a user