Skip to content

Commit 90a6d63

Browse files
committed
Re-Spawn Items That Fall Out-of-bounds
1 parent b67b528 commit 90a6d63

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/map.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ impl Plugin for MapPlugin {
3131
.register_rollback_type::<Handle<MapElementMeta>>()
3232
})
3333
.extend_rollback_schedule(|schedule| {
34-
schedule.add_system_to_stage(RollbackStage::Last, despawn_out_of_bounds);
34+
schedule.add_system_to_stage(
35+
RollbackStage::Last,
36+
handle_out_of_bounds_players_and_items,
37+
);
3538
})
3639
.add_plugin(elements::MapElementsPlugin);
3740
}
@@ -43,6 +46,12 @@ impl Plugin for MapPlugin {
4346
#[reflect(Component, Default)]
4447
pub struct MapElementHydrated;
4548

49+
/// If this component and a [`Transform`] component is added to any entity, it will be moved back to
50+
/// given position if the entity ever ends up outside the map bounds.
51+
#[derive(Deref, DerefMut, Component, Reflect, Default, Debug)]
52+
#[reflect(Default, Component)]
53+
pub struct MapRespawnPoint(pub Vec3);
54+
4655
// /// Contains the scripts that have been added for the currently loaded map
4756
// #[derive(Deref, DerefMut, Default)]
4857
// pub struct MapScripts(pub HashSet<Handle<JsScript>>);
@@ -260,10 +269,11 @@ pub fn hydrate_map(
260269
session_manager.start_session();
261270
}
262271

263-
fn despawn_out_of_bounds(
272+
fn handle_out_of_bounds_players_and_items(
264273
mut commands: Commands,
265274
map: Query<&MapMeta>,
266275
players: Query<(Entity, &Transform), With<PlayerIdx>>,
276+
mut items: Query<(&mut Transform, &MapRespawnPoint), Without<PlayerIdx>>,
267277
) {
268278
const KILL_ZONE_BORDER: f32 = 500.0;
269279
let Ok(map) = map.get_single() else {
@@ -275,11 +285,21 @@ fn despawn_out_of_bounds(
275285
let right_kill_zone = map_width + KILL_ZONE_BORDER;
276286
let bottom_kill_zone = -KILL_ZONE_BORDER;
277287

288+
// Kill out of bounds players
278289
for (player_ent, transform) in &players {
279290
let pos = transform.translation;
280291

281292
if pos.x < left_kill_zone || pos.x > right_kill_zone || pos.y < bottom_kill_zone {
282293
commands.add(PlayerKillCommand::new(player_ent));
283294
}
284295
}
296+
297+
// Reset out of bound item positions
298+
for (mut transform, respawn_point) in &mut items {
299+
let pos = transform.translation;
300+
301+
if pos.x < left_kill_zone || pos.x > right_kill_zone || pos.y < bottom_kill_zone {
302+
transform.translation = respawn_point.0;
303+
}
304+
}
285305
}

src/map/elements.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
damage::{DamageRegion, DamageRegionOwner},
44
item::{Item, ItemDropped, ItemUsed},
55
lifetime::Lifetime,
6-
map::MapElementHydrated,
6+
map::{MapElementHydrated, MapRespawnPoint},
77
metadata::{BuiltinElementKind, MapElementMeta},
88
name::EntityName,
99
physics::{collisions::CollisionWorld, KinematicBody},

src/map/elements/grenade.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ fn pre_update_in_game(
9898
})
9999
.insert(map_element_handle.clone_weak())
100100
.insert_bundle(VisibilityBundle::default())
101+
.insert(MapRespawnPoint(transform.translation))
101102
.insert_bundle(TransformBundle {
102103
local: *transform,
103104
..default()

src/map/elements/sword.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ fn pre_update_in_game(
6363
..default()
6464
})
6565
.insert_bundle(VisibilityBundle::default())
66+
.insert(MapRespawnPoint(transform.translation))
6667
.insert_bundle(TransformBundle {
6768
local: *transform,
6869
..default()

0 commit comments

Comments
 (0)