bit field swapped for normal list at API boundaries
This commit is contained in:
parent
157b77ebfa
commit
bf3c008777
1 changed files with 17 additions and 20 deletions
|
|
@ -13,10 +13,12 @@ namespace TD.Gameplay
|
|||
/// <see cref="PlayerBuffManager"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para><b>Bitmask, not a list.</b> A builder effect carries no per-grant state (no level,
|
||||
/// no enable/disable toggle) — it's either granted or it isn't — so the granted set is one
|
||||
/// <see cref="NetworkVariable{T}"/> bitmask keyed by <see cref="BuilderEffectKind"/> rather
|
||||
/// than a <c>NetworkList</c>. "Has effect" and "grant effect" are both O(1) bit ops.</para>
|
||||
/// <para><b>List of granted kinds.</b> A builder effect carries no per-grant state (no
|
||||
/// level, no enable/disable toggle) — it's either granted or it isn't — so the granted set
|
||||
/// is a <see cref="NetworkList{T}"/> of <see cref="BuilderEffectKind"/> values. Stored as
|
||||
/// <c>byte</c> because <c>NetworkList<T></c> requires <c>T : IEquatable<T></c>
|
||||
/// and plain enums don't implement that (see <see cref="TD.Gameplay.BuildJob"/> for the
|
||||
/// same constraint on a struct); <c>byte</c> already does, for free.</para>
|
||||
///
|
||||
/// <para><b>Granted via draft.</b> <see cref="TD.Gameplay.Draft.BuilderEffectDraftOption"/>
|
||||
/// calls <see cref="ServerGrantEffect"/> when a player picks it. Effects are permanent for
|
||||
|
|
@ -60,13 +62,10 @@ namespace TD.Gameplay
|
|||
|
||||
// ----- Networked state --------------------------------------------
|
||||
|
||||
// One bit per BuilderEffectKind. readPerm Everyone (consistent with the other
|
||||
// per-player managers); writePerm Server.
|
||||
private readonly NetworkVariable<uint> grantedMask = new NetworkVariable<uint>(
|
||||
value: 0,
|
||||
readPerm: NetworkVariableReadPermission.Everyone,
|
||||
writePerm: NetworkVariableWritePermission.Server
|
||||
);
|
||||
// Granted effect kinds, stored as byte (see class remarks for why not the enum
|
||||
// directly). readPerm Everyone (consistent with the other per-player managers);
|
||||
// writePerm Server — both are NetworkList's defaults.
|
||||
private readonly NetworkList<byte> grantedKinds = new NetworkList<byte>();
|
||||
|
||||
/// <summary>Fired on every peer when a new effect is granted.</summary>
|
||||
public event System.Action OnUpgradesChanged;
|
||||
|
|
@ -76,23 +75,23 @@ namespace TD.Gameplay
|
|||
public override void OnNetworkSpawn()
|
||||
{
|
||||
s_byClientId[OwnerClientId] = this;
|
||||
grantedMask.OnValueChanged += HandleMaskChanged;
|
||||
grantedKinds.OnListChanged += HandleGrantedKindsChanged;
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
grantedMask.OnValueChanged -= HandleMaskChanged;
|
||||
grantedKinds.OnListChanged -= HandleGrantedKindsChanged;
|
||||
|
||||
if (s_byClientId.TryGetValue(OwnerClientId, out var registered) && registered == this)
|
||||
s_byClientId.Remove(OwnerClientId);
|
||||
}
|
||||
|
||||
private void HandleMaskChanged(uint previous, uint current) => OnUpgradesChanged?.Invoke();
|
||||
private void HandleGrantedKindsChanged(NetworkListEvent<byte> change) => OnUpgradesChanged?.Invoke();
|
||||
|
||||
// ----- Read API -----------------------------------------------------
|
||||
|
||||
/// <summary>True if this player has been granted the given effect.</summary>
|
||||
public bool PlayerHasEffect(BuilderEffectKind kind) => (grantedMask.Value & BitFor(kind)) != 0;
|
||||
public bool PlayerHasEffect(BuilderEffectKind kind) => grantedKinds.Contains((byte)kind);
|
||||
|
||||
/// <summary>
|
||||
/// Returns this player's <see cref="BuilderEffectDefinition"/> for <paramref name="kind"/>
|
||||
|
|
@ -113,13 +112,11 @@ namespace TD.Gameplay
|
|||
{
|
||||
if (!IsServer) return false;
|
||||
|
||||
uint bit = BitFor(kind);
|
||||
if ((grantedMask.Value & bit) != 0) return false;
|
||||
byte value = (byte)kind;
|
||||
if (grantedKinds.Contains(value)) return false;
|
||||
|
||||
grantedMask.Value |= bit;
|
||||
grantedKinds.Add(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static uint BitFor(BuilderEffectKind kind) => 1u << (int)kind;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue