Skip to content

Commit

Permalink
Re-Spawn Items That Fall Out-of-bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Dec 1, 2022
1 parent b67b528 commit 90a6d63
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ impl Plugin for MapPlugin {
.register_rollback_type::<Handle<MapElementMeta>>()
})
.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);
}
Expand All @@ -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<Handle<JsScript>>);
Expand Down Expand Up @@ -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<PlayerIdx>>,
mut items: Query<(&mut Transform, &MapRespawnPoint), Without<PlayerIdx>>,
) {
const KILL_ZONE_BORDER: f32 = 500.0;
let Ok(map) = map.get_single() else {
Expand All @@ -275,11 +285,21 @@ 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;

if pos.x < left_kill_zone || pos.x > right_kill_zone || pos.y < bottom_kill_zone {
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;
}
}
}
2 changes: 1 addition & 1 deletion src/map/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
1 change: 1 addition & 0 deletions src/map/elements/grenade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/map/elements/sword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn pre_update_in_game(
..default()
})
.insert_bundle(VisibilityBundle::default())
.insert(MapRespawnPoint(transform.translation))
.insert_bundle(TransformBundle {
local: *transform,
..default()
Expand Down

0 comments on commit 90a6d63

Please sign in to comment.