Skip to content

Commit

Permalink
feat: update to bones with increase network player limit (#1005)
Browse files Browse the repository at this point in the history
matching PR to fishfolk/bones#407

---------

Co-authored-by: Max Whitehead <max.blackllama.alt3@gmail.com>
  • Loading branch information
dignifiedquire and MaxCWhitehead authored Jul 4, 2024
1 parent 7ea4b43 commit 4c9c8a6
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 45 deletions.
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod win_indicator;
pub const FPS: f32 = 60.0;

/// The maximum number of players per match.
pub const MAX_PLAYERS: usize = 4;
pub const MAX_PLAYERS: u32 = 4;

use std::time::Duration;

Expand Down Expand Up @@ -52,7 +52,7 @@ pub fn game_plugin(game: &mut Game) {

pub struct MatchPlugin {
pub maps: MapPool,
pub player_info: [PlayerInput; MAX_PLAYERS],
pub player_info: [PlayerInput; MAX_PLAYERS as usize],
/// The lua plugins to enable for this match.
pub plugins: Arc<Vec<Handle<LuaPlugin>>>,

Expand Down Expand Up @@ -150,7 +150,7 @@ impl SessionRunner for JumpyDefaultMatchRunner {
let input = self.input_collector.get_current_controls();
{
let mut player_inputs = world.resource_mut::<MatchInputs>();
(0..MAX_PLAYERS).for_each(|i| {
(0..MAX_PLAYERS as usize).for_each(|i| {
let player_input = &mut player_inputs.players[i];
let Some(source) = &player_input.control_source else {
return;
Expand Down
3 changes: 2 additions & 1 deletion src/core/elements/flappy_jellyfish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ fn explode_flappy_jellyfish(
mut dehydrate_jellyfish: CompMut<DehydrateJellyfish>,
) {
// Collect the hitboxes of all players
let mut player_hitboxes = SmallVec::<[Rect; MAX_PLAYERS]>::with_capacity(MAX_PLAYERS);
let mut player_hitboxes =
SmallVec::<[Rect; MAX_PLAYERS as usize]>::with_capacity(MAX_PLAYERS as usize);
player_hitboxes.extend(
entities
.iter_with((&player_indexes, &transforms, &bodies))
Expand Down
2 changes: 1 addition & 1 deletion src/core/elements/player_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn update(
.collect::<Vec<_>>();

// For every player
for i in 0..MAX_PLAYERS as u32 {
for i in 0..MAX_PLAYERS {
let player = &player_inputs.players[i as usize];

// If the player is active, but not alive
Expand Down
4 changes: 2 additions & 2 deletions src/core/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn install(session: &mut Session) {
/// The inputs for each player in this simulation frame.
#[derive(Clone, Debug, HasSchema)]
pub struct MatchInputs {
pub players: [PlayerInput; MAX_PLAYERS],
pub players: [PlayerInput; MAX_PLAYERS as usize],
}

impl Default for MatchInputs {
Expand All @@ -30,7 +30,7 @@ impl PlayerControls<'_, PlayerControl> for MatchInputs {
type InputCollector = PlayerInputCollector;

fn update_controls(&mut self, collector: &mut PlayerInputCollector) {
(0..MAX_PLAYERS).for_each(|i| {
(0..MAX_PLAYERS as usize).for_each(|i| {
let player_input = &mut self.players[i];
if let Some(source) = &player_input.control_source {
player_input.control = *collector.get_control(i, *source);
Expand Down
6 changes: 3 additions & 3 deletions src/core/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct Inv {

/// System param that can be used to conveniently get the inventory of each player.
#[derive(Deref, DerefMut, Debug)]
pub struct PlayerInventories<'a>(&'a [Option<Inv>; MAX_PLAYERS]);
pub struct PlayerInventories<'a>(&'a [Option<Inv>; MAX_PLAYERS as usize]);

impl PlayerInventories<'_> {
pub fn find_item(&self, item: Entity) -> Option<Inv> {
Expand All @@ -55,15 +55,15 @@ impl PlayerInventories<'_> {
}

impl<'a> SystemParam for PlayerInventories<'a> {
type State = [Option<Inv>; MAX_PLAYERS];
type State = [Option<Inv>; MAX_PLAYERS as usize];
type Param<'s> = PlayerInventories<'s>;

fn get_state(world: &World) -> Self::State {
world.run_system(
|entities: Res<Entities>,
player_indexes: Comp<PlayerIdx>,
inventories: Comp<Inventory>| {
let mut player_inventories = [None; MAX_PLAYERS];
let mut player_inventories = [None; MAX_PLAYERS as usize];
for (player, (idx, inventory)) in
entities.iter_with((&player_indexes, &inventories))
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ fn player_ai_system(
#[derive(Debug, Clone, HasSchema, Default)]
struct PlayersHaveSpawned {
/// For each player, whether they have spawned before.
pub players: [bool; MAX_PLAYERS],
pub players: [bool; MAX_PLAYERS as usize],
}

/// Marker component for a player hat.
Expand Down
3 changes: 2 additions & 1 deletion src/core/player/state/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ impl SystemStage for PlayerStateStageImpl {
|entities: Res<Entities>,
player_indexes: Comp<PlayerIdx>,
player_states: Comp<PlayerState>| {
let mut states: [Option<Ustr>; MAX_PLAYERS] = std::array::from_fn(|_| None);
let mut states: [Option<Ustr>; MAX_PLAYERS as usize] =
std::array::from_fn(|_| None);
for (_ent, (idx, state)) in
entities.iter_with((&player_indexes, &player_states))
{
Expand Down
2 changes: 1 addition & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl Default for PlayerInputCollector {
// We always have the keyboard controls "plugged in"
m.insert(ControlSource::Keyboard1, default());
m.insert(ControlSource::Keyboard2, default());
for i in 0..MAX_PLAYERS as u32 {
for i in 0..MAX_PLAYERS {
m.insert(ControlSource::Gamepad(i), default());
}
m
Expand Down
2 changes: 1 addition & 1 deletion src/ui/main_menu/map_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn handle_match_setup_messages(
asset_server: &AssetServer,
) -> Option<MapSelectAction> {
if let Some(socket) = socket {
let datas: Vec<(usize, Vec<u8>)> = socket.recv_reliable();
let datas: Vec<(u32, Vec<u8>)> = socket.recv_reliable();

for (_player, data) in datas {
match postcard::from_bytes::<MapSelectMessage>(&data) {
Expand Down
29 changes: 16 additions & 13 deletions src/ui/main_menu/player_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::*;

#[derive(Default, Clone, Debug, HasSchema)]
pub struct PlayerSelectState {
pub slots: [PlayerSlot; MAX_PLAYERS],
pub slots: [PlayerSlot; MAX_PLAYERS as usize],
/// Cache of available players from the game and packs.
pub players: Vec<Handle<PlayerMeta>>,
/// Cache of available hats from the game and packs.
Expand Down Expand Up @@ -177,9 +177,12 @@ pub fn widget(
ui.vertical_centered(|ui| {
ui.set_width(ui.available_width() - normal_button_style.font.size * 2.0);

ui.columns(MAX_PLAYERS, |columns| {
ui.columns(MAX_PLAYERS as usize, |columns| {
for (i, ui) in columns.iter_mut().enumerate() {
world.run_system(player_select_panel, (ui, i, &mut state))
world.run_system(
player_select_panel,
(ui, u32::try_from(i).unwrap(), &mut state),
)
}
});
});
Expand All @@ -197,21 +200,21 @@ fn handle_match_setup_messages(
player_select_state: &mut PlayerSelectState,
asset_server: &AssetServer,
) {
let datas: Vec<(usize, Vec<u8>)> = network_socket.recv_reliable();
let datas: Vec<(u32, Vec<u8>)> = network_socket.recv_reliable();

for (player, data) in datas {
match postcard::from_bytes::<PlayerSelectMessage>(&data) {
Ok(message) => match message {
PlayerSelectMessage::SelectPlayer(player_handle) => {
let player_handle = player_handle.into_handle(asset_server);
player_select_state.slots[player].selected_player = player_handle;
player_select_state.slots[player as usize].selected_player = player_handle;
}
PlayerSelectMessage::ConfirmSelection(confirmed) => {
player_select_state.slots[player].confirmed = confirmed;
player_select_state.slots[player as usize].confirmed = confirmed;
}
PlayerSelectMessage::SelectHat(hat) => {
let hat = hat.map(|hat| hat.into_handle(asset_server));
player_select_state.slots[player].selected_hat = hat;
player_select_state.slots[player as usize].selected_hat = hat;
}
},
Err(e) => warn!("Ignoring network message that was not understood: {e}"),
Expand All @@ -220,7 +223,7 @@ fn handle_match_setup_messages(
}

fn player_select_panel(
mut params: In<(&mut egui::Ui, usize, &mut PlayerSelectState)>,
mut params: In<(&mut egui::Ui, u32, &mut PlayerSelectState)>,
meta: Root<GameMeta>,
controls: Res<GlobalPlayerControls>,
asset_server: Res<AssetServer>,
Expand Down Expand Up @@ -259,7 +262,7 @@ fn player_select_panel(
}

#[cfg(target_arch = "wasm32")]
let network_local_player_slot: Option<usize> = None;
let network_local_player_slot: Option<u32> = None;

#[cfg(target_arch = "wasm32")]
let is_network = false;
Expand All @@ -272,18 +275,18 @@ fn player_select_panel(
if *slot_id + 1 > socket.player_count() {
return;
} else {
state.slots[*slot_id].active = true;
state.slots[*slot_id as usize].active = true;
}
}

let is_next_open_slot = state
.slots
.iter()
.enumerate()
.any(|(i, slot)| (!slot.active && i == *slot_id));
.any(|(i, slot)| (!slot.active && u32::try_from(i).unwrap() == *slot_id));

#[cfg(not(target_arch = "wasm32"))]
let mut network_local_player_slot: Option<usize> = None;
let mut network_local_player_slot: Option<u32> = None;

#[cfg(not(target_arch = "wasm32"))]
let slot_allows_new_player = if is_network {
Expand Down Expand Up @@ -332,7 +335,7 @@ fn player_select_panel(
sources
};

let slot = &mut state.slots[*slot_id];
let slot = &mut state.slots[*slot_id as usize];
let player_handle = &mut slot.selected_player;

// If the handle is empty
Expand Down
4 changes: 2 additions & 2 deletions src/ui/network_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ pub enum LanMode {
Join,
Host {
service_name: String,
player_count: usize,
player_count: u32,
},
}

#[derive(Eq, PartialEq, Clone)]
pub struct OnlineState {
player_count: usize,
player_count: u32,
matchmaking_server: String,
search_state: SearchState,
}
Expand Down
6 changes: 3 additions & 3 deletions src/ui/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,13 @@ fn handle_scoring_messages(
state: &mut ScoringMenuState,
) {
// TODO handle disconnects
let datas: Vec<(usize, Vec<u8>)> = network_socket.recv_reliable();
let local_players = network_socket.player_is_local();
let datas: Vec<(u32, Vec<u8>)> = network_socket.recv_reliable();
let local_player_idx = network_socket.player_idx();
for (_, data) in datas {
match postcard::from_bytes::<ScoringMessage>(&data) {
Ok(message) => match message.data {
ScoringMessageEnum::PlayerReady(player) => {
if message.magic == SCORING_MESSAGE_MAGIC && !local_players[player as usize] {
if message.magic == SCORING_MESSAGE_MAGIC && player != local_player_idx {
state.ready_players.insert(PlayerIdx(player));
debug!("Received message player {} ready", player);
}
Expand Down

0 comments on commit 4c9c8a6

Please sign in to comment.