mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
feature: MatchInterestManagement (#2837)
* feature: MatchInterestManagement component * Update Assets/Mirror/Components/InterestManagement/Match/MatchInterestManagement.cs Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com> * Update Assets/Mirror/Components/InterestManagement/Match/MatchInterestManagement.cs Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com> * Update Assets/Mirror/Components/InterestManagement/Match/MatchInterestManagement.cs Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com> * Update Assets/Mirror/Components/InterestManagement/Match/MatchInterestManagement.cs Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com> * Update Assets/Mirror/Components/InterestManagement/Match/MatchInterestManagement.cs Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com> * Update Assets/Mirror/Components/InterestManagement/Match/MatchInterestManagement.cs Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com> * Update Assets/Mirror/Components/InterestManagement/Match/MatchInterestManagement.cs Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com> * Update Assets/Mirror/Components/InterestManagement/Match/MatchInterestManagement.cs Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com> * Update Assets/Mirror/Components/InterestManagement/Match/MatchInterestManagement.cs Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com>
This commit is contained in:
parent
6242c618d5
commit
f79eab5d20
3
Assets/Mirror/Components/InterestManagement/Match.meta
Normal file
3
Assets/Mirror/Components/InterestManagement/Match.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5eca5245ae6bb460e9a92f7e14d5493a
|
||||
timeCreated: 1622649517
|
@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Mirror
|
||||
{
|
||||
public class MatchInterestManagement : InterestManagement
|
||||
{
|
||||
readonly Dictionary<Guid, HashSet<NetworkIdentity>> matchObjects =
|
||||
new Dictionary<Guid, HashSet<NetworkIdentity>>();
|
||||
|
||||
readonly Dictionary<NetworkIdentity, Guid> lastObjectMatch =
|
||||
new Dictionary<NetworkIdentity, Guid>();
|
||||
|
||||
HashSet<Guid> dirtyMatches = new HashSet<Guid>();
|
||||
|
||||
public override void OnSpawned(NetworkIdentity identity)
|
||||
{
|
||||
Guid currentMatch = identity.GetComponent<NetworkMatch>().matchId;
|
||||
lastObjectMatch[identity] = currentMatch;
|
||||
|
||||
// Guid.Empty is never a valid matchId...do not add to matchObjects collection
|
||||
if (currentMatch == Guid.Empty) return;
|
||||
|
||||
// Debug.Log($"MatchInterestManagement.OnSpawned({identity.name}) currentMatch: {currentMatch}");
|
||||
if (!matchObjects.TryGetValue(currentMatch, out HashSet<NetworkIdentity> objects))
|
||||
{
|
||||
objects = new HashSet<NetworkIdentity>();
|
||||
matchObjects.Add(currentMatch, objects);
|
||||
}
|
||||
|
||||
objects.Add(identity);
|
||||
}
|
||||
|
||||
public override void OnDestroyed(NetworkIdentity identity)
|
||||
{
|
||||
Guid currentMatch = lastObjectMatch[identity];
|
||||
lastObjectMatch.Remove(identity);
|
||||
if (currentMatch != Guid.Empty && matchObjects.TryGetValue(currentMatch, out HashSet<NetworkIdentity> objects) && objects.Remove(identity))
|
||||
RebuildMatchObservers(currentMatch);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// only on server
|
||||
if (!NetworkServer.active) return;
|
||||
|
||||
// for each spawned:
|
||||
// if match changed:
|
||||
// add previous to dirty
|
||||
// add new to dirty
|
||||
foreach (NetworkIdentity netIdentity in NetworkIdentity.spawned.Values)
|
||||
{
|
||||
Guid currentMatch = lastObjectMatch[netIdentity];
|
||||
Guid newMatch = netIdentity.GetComponent<NetworkMatch>().matchId;
|
||||
if (newMatch == currentMatch) continue;
|
||||
|
||||
// Mark new/old scenes as dirty so they get rebuilt
|
||||
// Guid.Empty is never a valid matchId
|
||||
if (currentMatch != Guid.Empty)
|
||||
dirtyMatches.Add(currentMatch);
|
||||
dirtyMatches.Add(newMatch);
|
||||
|
||||
// This object is in a new match so observers in the prior match
|
||||
// and the new scene need to rebuild their respective observers lists.
|
||||
|
||||
// Remove this object from the hashset of the match it just left
|
||||
// Guid.Empty is never a valid matchId
|
||||
if (currentMatch != Guid.Empty)
|
||||
matchObjects[currentMatch].Remove(netIdentity);
|
||||
|
||||
// Set this to the new match this object just entered
|
||||
lastObjectMatch[netIdentity] = newMatch;
|
||||
|
||||
// Guid.Empty is never a valid matchId...do not add to matchObjects collection
|
||||
if (newMatch == Guid.Empty) continue;
|
||||
|
||||
|
||||
// Make sure this new match is in the dictionary
|
||||
if (!matchObjects.ContainsKey(newMatch))
|
||||
matchObjects.Add(newMatch, new HashSet<NetworkIdentity>());
|
||||
|
||||
// Add this object to the hashset of the new match
|
||||
matchObjects[newMatch].Add(netIdentity);
|
||||
}
|
||||
|
||||
// rebuild all dirty matchs
|
||||
foreach (Guid dirtyMatch in dirtyMatches)
|
||||
{
|
||||
RebuildMatchObservers(dirtyMatch);
|
||||
}
|
||||
|
||||
dirtyMatches.Clear();
|
||||
}
|
||||
|
||||
void RebuildMatchObservers(Guid matchId)
|
||||
{
|
||||
foreach (NetworkIdentity netIdentity in matchObjects[matchId])
|
||||
if (netIdentity != null)
|
||||
NetworkServer.RebuildObservers(netIdentity, false);
|
||||
}
|
||||
|
||||
public override bool OnCheckObserver(NetworkIdentity identity, NetworkConnection newObserver)
|
||||
{
|
||||
return identity.GetComponent<NetworkMatch>().matchId ==
|
||||
newObserver.identity.GetComponent<NetworkMatch>().matchId;
|
||||
}
|
||||
|
||||
public override void OnRebuildObservers(NetworkIdentity identity, HashSet<NetworkConnection> newObservers,
|
||||
bool initialize)
|
||||
{
|
||||
Guid matchId = identity.GetComponent<NetworkMatch>().matchId;
|
||||
|
||||
// Guid.Empty is never a valid matchId
|
||||
if (matchId == Guid.Empty) return;
|
||||
|
||||
if (!matchObjects.TryGetValue(matchId, out HashSet<NetworkIdentity> objects))
|
||||
return;
|
||||
|
||||
// Add everything in the hashset for this object's current match
|
||||
foreach (NetworkIdentity networkIdentity in objects)
|
||||
if (networkIdentity != null && networkIdentity.connectionToClient != null)
|
||||
newObservers.Add(networkIdentity.connectionToClient);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d09f5c8bf2f4747b7a9284ef5d9ce2a7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user