Clientside Backpack UI (#84)
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
use super::UiHeadState;
|
||||
use bevy::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub static HEAD_SLOTS: usize = 5;
|
||||
pub static BACKPACK_HEAD_SLOTS: usize = 5;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct BackpackMarker;
|
||||
@@ -19,7 +18,7 @@ pub struct HeadImage(pub usize);
|
||||
#[derive(Component, Default)]
|
||||
pub struct HeadDamage(pub usize);
|
||||
|
||||
#[derive(Component, Default, Debug, Reflect, Serialize, Deserialize, PartialEq)]
|
||||
#[derive(Component, Default, Debug, Reflect)]
|
||||
#[reflect(Component, Default)]
|
||||
pub struct BackpackUiState {
|
||||
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::{
|
||||
cash::CashCollectEvent, global_observer, head_drop::HeadCollected, heads::HeadState,
|
||||
heads_database::HeadsDatabase,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
#[cfg(feature = "client")]
|
||||
use bevy_replicon::prelude::ClientTriggerExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
pub use ui_head_state::UiHeadState;
|
||||
|
||||
@@ -35,17 +42,116 @@ impl Backpack {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Event)]
|
||||
pub struct BackbackSwapEvent(pub usize);
|
||||
#[derive(Event, Serialize, Deserialize)]
|
||||
pub struct BackpackSwapEvent(pub usize);
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.register_type::<Backpack>();
|
||||
|
||||
app.add_plugins(backpack_ui::plugin);
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
app.add_systems(FixedUpdate, (backpack_inputs, sync_on_change));
|
||||
|
||||
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(
|
||||
trigger: On<HeadCollected>,
|
||||
mut cmds: Commands,
|
||||
|
||||
@@ -4,8 +4,6 @@ use crate::{
|
||||
abilities::TriggerStateRes,
|
||||
animation::AnimationFlags,
|
||||
control::{ControllerSettings, Inputs, SelectedController},
|
||||
head::ActiveHead,
|
||||
heads_database::{HeadControls, HeadsDatabase},
|
||||
physics_layers::GameLayer,
|
||||
player::{Player, PlayerBodyMesh},
|
||||
};
|
||||
@@ -40,8 +38,7 @@ pub fn plugin(app: &mut App) {
|
||||
.after(ControllerSet::ApplyControlsRun)
|
||||
.after(ControllerSet::ApplyControlsFly)
|
||||
.run_if(in_state(GameState::Playing)),
|
||||
)
|
||||
.add_systems(FixedPostUpdate, add_controller_bundle);
|
||||
);
|
||||
}
|
||||
|
||||
fn set_animation_flags(
|
||||
@@ -79,7 +76,7 @@ pub fn reset_upon_switch(
|
||||
mut event_controller_switch: MessageReader<ControllerSwitchEvent>,
|
||||
selected_controller: Res<SelectedController>,
|
||||
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() {
|
||||
let (mut velocity, children, inputs) = controllers.get_mut(controller).unwrap();
|
||||
@@ -151,70 +148,36 @@ fn decelerate(
|
||||
#[reflect(Component)]
|
||||
pub struct MovementSpeedFactor(pub f32);
|
||||
|
||||
impl Default for MovementSpeedFactor {
|
||||
fn default() -> Self {
|
||||
Self(1.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Reflect, Default, Debug, Serialize, Deserialize, PartialEq)]
|
||||
#[reflect(Component)]
|
||||
#[require(
|
||||
Character,
|
||||
RigidBody::Kinematic,
|
||||
Collider::capsule(0.9, 1.2),
|
||||
CollisionLayers::new(
|
||||
LayerMask(GameLayer::Player.to_bits()),
|
||||
LayerMask::ALL & !GameLayer::CollectiblePhysics.to_bits(),
|
||||
),
|
||||
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;
|
||||
|
||||
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
|
||||
/// kinematic character controller.
|
||||
#[derive(Bundle)]
|
||||
pub struct CharacterControllerBundle {
|
||||
character_controller: Character,
|
||||
rigidbody: RigidBody,
|
||||
collider: Collider,
|
||||
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::ALL & !GameLayer::CollectiblePhysics.to_bits(),
|
||||
),
|
||||
interpolation: TransformInterpolation,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Bundle)]
|
||||
struct MovementConfig {
|
||||
movement: CharacterMovement,
|
||||
|
||||
@@ -38,7 +38,8 @@ pub fn plugin(app: &mut App) {
|
||||
app.init_resource::<LookDirMovement>();
|
||||
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_flying::CharacterControllerPlugin);
|
||||
@@ -114,17 +115,13 @@ pub struct SelectLeftPressed;
|
||||
#[derive(Message, Serialize, Deserialize)]
|
||||
pub struct SelectRightPressed;
|
||||
|
||||
#[derive(Message, Serialize, Deserialize)]
|
||||
pub struct BackpackTogglePressed;
|
||||
|
||||
#[derive(Message, Serialize, Deserialize)]
|
||||
pub struct BackpackSwapPressed;
|
||||
|
||||
#[derive(Message, Serialize, Deserialize)]
|
||||
pub struct BackpackLeftPressed;
|
||||
|
||||
#[derive(Message, Serialize, Deserialize)]
|
||||
pub struct BackpackRightPressed;
|
||||
#[derive(Message)]
|
||||
pub enum BackpackButtonPress {
|
||||
Toggle,
|
||||
Swap,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Message, Serialize, Deserialize)]
|
||||
pub struct CashHealPressed;
|
||||
|
||||
@@ -1,22 +1,16 @@
|
||||
#[cfg(feature = "server")]
|
||||
use crate::protocol::PlaySound;
|
||||
use crate::{
|
||||
GameState,
|
||||
backpack::{BackbackSwapEvent, Backpack},
|
||||
animation::AnimationFlags,
|
||||
backpack::{Backpack, BackpackSwapEvent},
|
||||
control::{SelectLeftPressed, SelectRightPressed},
|
||||
global_observer,
|
||||
heads_database::HeadsDatabase,
|
||||
hitpoints::Hitpoints,
|
||||
player::Player,
|
||||
};
|
||||
#[cfg(feature = "server")]
|
||||
use crate::{
|
||||
animation::AnimationFlags,
|
||||
control::{SelectLeftPressed, SelectRightPressed},
|
||||
protocol::ClientToController,
|
||||
protocol::{ClientToController, PlaySound},
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
#[cfg(feature = "server")]
|
||||
use bevy_replicon::prelude::FromClient;
|
||||
use bevy_replicon::prelude::{ClientState, FromClient};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod heads_ui;
|
||||
@@ -188,13 +182,14 @@ pub fn plugin(app: &mut App) {
|
||||
app.register_type::<ActiveHeads>();
|
||||
|
||||
app.add_systems(OnEnter(GameState::Playing), setup);
|
||||
#[cfg(feature = "server")]
|
||||
app.add_systems(
|
||||
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);
|
||||
}
|
||||
@@ -208,7 +203,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, heads: Res<Head
|
||||
commands.insert_resource(HeadsImages { heads });
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
fn sync_hp(mut query: Query<(&mut ActiveHeads, &Hitpoints)>) {
|
||||
for (mut active_heads, hp) in query.iter_mut() {
|
||||
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(
|
||||
mut commands: Commands,
|
||||
mut active: Query<&mut ActiveHeads>,
|
||||
@@ -249,7 +242,6 @@ fn reload(
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
fn on_select_active_head(
|
||||
mut commands: Commands,
|
||||
mut query: Query<(&mut ActiveHeads, &mut Hitpoints), With<Player>>,
|
||||
@@ -305,7 +297,7 @@ fn on_select_active_head(
|
||||
}
|
||||
|
||||
fn on_swap_backpack(
|
||||
trigger: On<BackbackSwapEvent>,
|
||||
trigger: On<FromClient<BackpackSwapEvent>>,
|
||||
mut commands: Commands,
|
||||
mut query: Query<(&mut ActiveHeads, &mut Hitpoints, &mut Backpack), With<Player>>,
|
||||
) {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#[cfg(feature = "client")]
|
||||
use crate::control::LocalInputs;
|
||||
use crate::{
|
||||
GameState,
|
||||
cash::{Cash, CashCollectEvent},
|
||||
character::HedzCharacter,
|
||||
protocol::PlayerId,
|
||||
};
|
||||
#[cfg(feature = "client")]
|
||||
use crate::{backpack::backpack_ui::BackpackUiState, control::LocalInputs};
|
||||
use avian3d::prelude::*;
|
||||
use bevy::{
|
||||
input::common_conditions::input_just_pressed,
|
||||
@@ -22,7 +22,7 @@ pub struct Player;
|
||||
#[cfg(feature = "client")]
|
||||
#[derive(Component, Debug, Reflect)]
|
||||
#[reflect(Component)]
|
||||
#[require(LocalInputs)]
|
||||
#[require(LocalInputs, BackpackUiState)]
|
||||
pub struct LocalPlayer;
|
||||
|
||||
#[derive(Component, Default, Serialize, Deserialize, PartialEq)]
|
||||
|
||||
@@ -2,12 +2,11 @@ use crate::{
|
||||
GameState,
|
||||
abilities::{BuildExplosionSprite, curver::CurverProjectile, healing::Healing},
|
||||
animation::AnimationFlags,
|
||||
backpack::{Backpack, backpack_ui::BackpackUiState},
|
||||
backpack::{Backpack, BackpackSwapEvent},
|
||||
camera::{CameraArmRotation, CameraTarget},
|
||||
cash::CashInventory,
|
||||
character::{AnimatedCharacter, HedzCharacter},
|
||||
control::{
|
||||
BackpackLeftPressed, BackpackRightPressed, BackpackSwapPressed, BackpackTogglePressed,
|
||||
CashHealPressed, ClientInputs, ControllerSettings, Inputs, SelectLeftPressed,
|
||||
SelectRightPressed,
|
||||
controller_common::{MovementSpeedFactor, PlayerCharacterController},
|
||||
@@ -54,13 +53,10 @@ pub fn plugin(app: &mut App) {
|
||||
app.add_client_message::<ClientInputs>(Channel::Unreliable)
|
||||
.add_client_message::<SelectLeftPressed>(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);
|
||||
|
||||
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)
|
||||
.add_server_message::<messages::AssignClientPlayer>(Channel::Unordered);
|
||||
@@ -94,7 +90,6 @@ pub fn plugin(app: &mut App) {
|
||||
.replicate_once::<AutoRotation>()
|
||||
.replicate_once::<CurverProjectile>()
|
||||
.replicate::<Backpack>()
|
||||
.replicate::<BackpackUiState>()
|
||||
.replicate::<Billboard>()
|
||||
.replicate_once::<CameraArmRotation>()
|
||||
.replicate_once::<CameraTarget>()
|
||||
|
||||
Reference in New Issue
Block a user