Skip to content

Commit

Permalink
Merge #501
Browse files Browse the repository at this point in the history
501: Handle out of bounds r=zicklag a=zicklag

bors merge

Co-authored-by: Zicklag <zicklag@katharostech.com>
  • Loading branch information
bors[bot] and zicklag committed Dec 1, 2022
2 parents 54d4e18 + 90a6d63 commit dcde401
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion assets/map/levels/level1.map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ background_layers:
position:
- 0.0
- 360.0
background_color: 7EA8A6FF
background_color: 5B5772FF
grid_size:
- 27
- 21
Expand Down
2 changes: 1 addition & 1 deletion assets/map/levels/level2.map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ background_layers:
position:
- 0.0
- 360.0
background_color: 7EA8A6FF
background_color: 5B5772FF
grid_size:
- 34
- 14
Expand Down
2 changes: 1 addition & 1 deletion assets/map/levels/level3.map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ background_layers:
position:
- 0.0
- 360.0
background_color: 7EA8A6FF
background_color: 5B5772FF
grid_size:
- 27
- 22
Expand Down
2 changes: 1 addition & 1 deletion assets/map/levels/level4.map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ background_layers:
position:
- 0.0
- 360.0
background_color: 7EA8A6FF
background_color: 5B5772FF
grid_size:
- 35
- 15
Expand Down
17 changes: 16 additions & 1 deletion src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use bevy::render::view::RenderLayers;
use bevy_parallax::ParallaxCameraComponent;

use crate::{metadata::GameMeta, player::PlayerIdx, prelude::*};
use crate::{
metadata::{GameMeta, MapMeta},
player::PlayerIdx,
prelude::*,
};

pub struct CameraPlugin;

Expand Down Expand Up @@ -89,6 +93,7 @@ pub fn spawn_editor_camera(commands: &mut Commands) -> Entity {

fn camera_controller(
players: Query<&Transform, With<PlayerIdx>>,
map: Query<&MapMeta>,
mut camera: Query<
(&mut Transform, &mut OrthographicProjection),
(With<GameCamera>, Without<PlayerIdx>),
Expand All @@ -102,6 +107,10 @@ fn camera_controller(
const ZOOM_OUT_LERP_FACTOR: f32 = 0.1;
const MIN_BOUND: f32 = 350.0;

let Ok(map) = map.get_single() else {
return;
};

let Ok((mut camera_transform, mut projection)) = camera.get_single_mut() else {
return;
};
Expand All @@ -111,6 +120,11 @@ fn camera_controller(
let default_height = game.camera_height as f32;
let default_width = window_aspect * default_height;

let map_width = (map.tile_size.x * map.grid_size.x) as f32;
let map_height = (map.tile_size.y * map.grid_size.y) as f32;
let min_player_pos = Vec2::new(-CAMERA_PADDING, -CAMERA_PADDING);
let max_player_pos = Vec2::new(map_width + CAMERA_PADDING, map_height + CAMERA_PADDING);

let mut middle_point = Vec2::ZERO;
let mut min = Vec2::new(100000.0, 100000.0);
let mut max = Vec2::new(-100000.0, -100000.0);
Expand All @@ -119,6 +133,7 @@ fn camera_controller(

for player_transform in &players {
let pos = player_transform.translation.truncate();
let pos = pos.max(min_player_pos).min(max_player_pos);
middle_point += pos;

min.x = pos.x.min(min.x);
Expand Down
48 changes: 48 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
metadata::{MapElementMeta, MapLayerKind, MapLayerMeta, MapMeta},
name::EntityName,
physics::collisions::{CollisionLayerTag, TileCollision},
player::{PlayerIdx, PlayerKillCommand},
prelude::*,
session::SessionManager,
utils::Sort,
Expand All @@ -29,6 +30,12 @@ impl Plugin for MapPlugin {
.register_rollback_type::<MapElementHydrated>()
.register_rollback_type::<Handle<MapElementMeta>>()
})
.extend_rollback_schedule(|schedule| {
schedule.add_system_to_stage(
RollbackStage::Last,
handle_out_of_bounds_players_and_items,
);
})
.add_plugin(elements::MapElementsPlugin);
}
}
Expand All @@ -39,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 @@ -255,3 +268,38 @@ pub fn hydrate_map(
// Start the game session
session_manager.start_session();
}

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 {
return;
};

let map_width = (map.grid_size.x * map.tile_size.x) as f32;
let left_kill_zone = -KILL_ZONE_BORDER;
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
2 changes: 2 additions & 0 deletions src/ui/main_menu/map_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
client::NetClient,
proto::{match_setup::MatchSetupMessage, ReliableGameMessageKind},
},
player::input::WantsGamePause,
ui::pause_menu::PauseMenuPage,
};

Expand Down Expand Up @@ -135,6 +136,7 @@ impl<'w, 's> WidgetSystem for MapSelectMenu<'w, 's> {
*params.menu_page = MenuPage::Home;
params.reset_manager.reset_world();
params.commands.spawn().insert(map_handle.clone_weak());
params.commands.insert_resource(WantsGamePause(false));
params
.commands
.insert_resource(NextState(GameState::InGame));
Expand Down

0 comments on commit dcde401

Please sign in to comment.