From 90a6d639aed49c99397e2fe25d58ca660194596b Mon Sep 17 00:00:00 2001 From: Zicklag Date: Thu, 1 Dec 2022 15:32:34 -0600 Subject: [PATCH] Re-Spawn Items That Fall Out-of-bounds --- src/map.rs | 24 ++++++++++++++++++++++-- src/map/elements.rs | 2 +- src/map/elements/grenade.rs | 1 + src/map/elements/sword.rs | 1 + 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/map.rs b/src/map.rs index 4ad2f20ecf..eec84e11be 100644 --- a/src/map.rs +++ b/src/map.rs @@ -31,7 +31,10 @@ impl Plugin for MapPlugin { .register_rollback_type::>() }) .extend_rollback_schedule(|schedule| { - schedule.add_system_to_stage(RollbackStage::Last, despawn_out_of_bounds); + schedule.add_system_to_stage( + RollbackStage::Last, + handle_out_of_bounds_players_and_items, + ); }) .add_plugin(elements::MapElementsPlugin); } @@ -43,6 +46,12 @@ impl Plugin for MapPlugin { #[reflect(Component, Default)] pub struct MapElementHydrated; +/// If this component and a [`Transform`] component is added to any entity, it will be moved back to +/// given position if the entity ever ends up outside the map bounds. +#[derive(Deref, DerefMut, Component, Reflect, Default, Debug)] +#[reflect(Default, Component)] +pub struct MapRespawnPoint(pub Vec3); + // /// Contains the scripts that have been added for the currently loaded map // #[derive(Deref, DerefMut, Default)] // pub struct MapScripts(pub HashSet>); @@ -260,10 +269,11 @@ pub fn hydrate_map( session_manager.start_session(); } -fn despawn_out_of_bounds( +fn handle_out_of_bounds_players_and_items( mut commands: Commands, map: Query<&MapMeta>, players: Query<(Entity, &Transform), With>, + mut items: Query<(&mut Transform, &MapRespawnPoint), Without>, ) { const KILL_ZONE_BORDER: f32 = 500.0; let Ok(map) = map.get_single() else { @@ -275,6 +285,7 @@ fn despawn_out_of_bounds( let right_kill_zone = map_width + KILL_ZONE_BORDER; let bottom_kill_zone = -KILL_ZONE_BORDER; + // Kill out of bounds players for (player_ent, transform) in &players { let pos = transform.translation; @@ -282,4 +293,13 @@ fn despawn_out_of_bounds( commands.add(PlayerKillCommand::new(player_ent)); } } + + // Reset out of bound item positions + for (mut transform, respawn_point) in &mut items { + let pos = transform.translation; + + if pos.x < left_kill_zone || pos.x > right_kill_zone || pos.y < bottom_kill_zone { + transform.translation = respawn_point.0; + } + } } diff --git a/src/map/elements.rs b/src/map/elements.rs index 25bc063b37..eac51f17b2 100644 --- a/src/map/elements.rs +++ b/src/map/elements.rs @@ -3,7 +3,7 @@ use crate::{ damage::{DamageRegion, DamageRegionOwner}, item::{Item, ItemDropped, ItemUsed}, lifetime::Lifetime, - map::MapElementHydrated, + map::{MapElementHydrated, MapRespawnPoint}, metadata::{BuiltinElementKind, MapElementMeta}, name::EntityName, physics::{collisions::CollisionWorld, KinematicBody}, diff --git a/src/map/elements/grenade.rs b/src/map/elements/grenade.rs index bc16a9cd8d..437f68efe1 100644 --- a/src/map/elements/grenade.rs +++ b/src/map/elements/grenade.rs @@ -98,6 +98,7 @@ fn pre_update_in_game( }) .insert(map_element_handle.clone_weak()) .insert_bundle(VisibilityBundle::default()) + .insert(MapRespawnPoint(transform.translation)) .insert_bundle(TransformBundle { local: *transform, ..default() diff --git a/src/map/elements/sword.rs b/src/map/elements/sword.rs index 5be3551386..035004609c 100644 --- a/src/map/elements/sword.rs +++ b/src/map/elements/sword.rs @@ -63,6 +63,7 @@ fn pre_update_in_game( ..default() }) .insert_bundle(VisibilityBundle::default()) + .insert(MapRespawnPoint(transform.translation)) .insert_bundle(TransformBundle { local: *transform, ..default()