Skip to content

Commit 53ba118

Browse files
committed
Use new C# syntactic sugar, fix improper caching of flag behavior
1 parent 4a6c391 commit 53ba118

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1390
-270
lines changed

Assets/Scripts/Voxelfield/Interface/BuyMenuButton.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class BuyMenuButton : MonoBehaviour, IPointerEnterHandler
1717
private Button m_Button;
1818

1919
public byte ItemId => m_ItemId;
20-
public UnityEvent OnEnter { get; } = new UnityEvent();
20+
public UnityEvent OnEnter { get; } = new();
2121
public Button.ButtonClickedEvent OnClick => m_Button.onClick;
2222

2323
private void Awake() => m_Button = GetComponent<Button>();

Assets/Scripts/Voxelfield/Interface/LoadOutInterface.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class LoadOutInterface : SessionInterfaceBehavior
1313
{
1414
private LoadOutButton[][] m_LoadOutButtons;
1515

16-
public WantedItemIdArray WantedItems { get; set; } = new WantedItemIdArray();
16+
public WantedItemIdArray WantedItems { get; set; } = new();
1717

1818
public override void Initialize()
1919
{

Assets/Scripts/Voxelfield/Interface/SteamFriendsInterface.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ private class FriendComparer : IEqualityComparer<Friend>
3636
[SerializeField] private SteamFriendEntryInterface m_EntryPrefab = default;
3737
[SerializeField] private Transform m_EntryParent = default;
3838

39-
private readonly Dictionary<Friend, Entry> m_Entries = new Dictionary<Friend, Entry>(new FriendComparer());
40-
private readonly List<SteamFriendEntryInterface> m_InterfaceEntries = new List<SteamFriendEntryInterface>();
41-
private readonly Mutex m_FriendMutex = new Mutex();
39+
private readonly Dictionary<Friend, Entry> m_Entries = new(new FriendComparer());
40+
private readonly List<SteamFriendEntryInterface> m_InterfaceEntries = new();
41+
private readonly Mutex m_FriendMutex = new();
4242

4343
protected override void OnSetInterfaceActive(bool isActive)
4444
{

Assets/Scripts/Voxelfield/Item/SculptingThrowable.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Voxelfield.Item
99
{
1010
public class SculptingThrowable : ThrowableModifierBehavior
1111
{
12-
private static readonly Color32 Sand = new Color32(253, 255, 224, 255);
12+
private static readonly Color32 Sand = new(253, 255, 224, 255);
1313

1414
protected override void JustPopped(in SessionContext context, ThrowableComponent throwable)
1515
{

Assets/Scripts/Voxelfield/Session/Components.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ShowdownSessionComponent : ComponentBase
2828
{
2929
public ByteProperty number;
3030
public TimeUsProperty remainingUs;
31-
public CurePackagesArray curePackages = new CurePackagesArray();
31+
public CurePackagesArray curePackages = new();
3232
}
3333

3434
[Serializable, ModeElement]
@@ -80,7 +80,7 @@ public class CtfComponent : ComponentBase
8080
{
8181
public TeamFlagArray teamFlags;
8282
public UIntProperty pickupFlags;
83-
[NoSerialization] public ArrayElement<TimeUsProperty> pickupCoolDowns = new ArrayElement<TimeUsProperty>(sizeof(uint) * 8);
83+
[NoSerialization] public ArrayElement<TimeUsProperty> pickupCoolDowns = new(sizeof(uint) * 8);
8484
}
8585

8686
/* Secure area */
@@ -101,7 +101,7 @@ public SiteArray() : base(2) { }
101101
[Serializable, ModeElement]
102102
public class SecureAreaComponent : ComponentBase
103103
{
104-
public SiteArray sites = new SiteArray();
104+
public SiteArray sites = new();
105105
public TimeUsProperty roundTime;
106106
[NoSerialization] public ByteProperty lastWinningTeam;
107107

Assets/Scripts/Voxelfield/Session/Injector.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public class Injector : SessionInjectorBase
3333
{
3434
private const int DiscordUpdateRate = 10;
3535

36-
protected readonly RequestConnectionComponent m_RequestConnection = new RequestConnectionComponent();
37-
protected readonly NetDataWriter m_RejectionWriter = new NetDataWriter();
36+
protected readonly RequestConnectionComponent m_RequestConnection = new();
37+
protected readonly NetDataWriter m_RejectionWriter = new();
3838
protected AuthTicket m_SteamAuthenticationTicket;
3939

4040
protected MapManager m_MapManager;

Assets/Scripts/Voxelfield/Session/Mode/CtfMode.cs

+16-23
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,27 @@ public class CtfMode : DeathmatchModeBase
2727

2828
[SerializeField] private LayerMask m_PlayerMask = default;
2929
[SerializeField] private float m_CaptureRadius = 3.0f;
30-
[SerializeField] private Color m_BlueColor = new Color(0.1764705882f, 0.5098039216f, 0.8509803922f),
31-
m_RedColor = new Color(0.8196078431f, 0.2156862745f, 0.1960784314f);
30+
[SerializeField] private Color m_BlueColor = new(0.1764705882f, 0.5098039216f, 0.8509803922f),
31+
m_RedColor = new(0.8196078431f, 0.2156862745f, 0.1960784314f);
3232
[SerializeField] private ItemModifierBase[] m_PickupItemIds = default;
3333
[SerializeField] private byte m_HealthPackBonus = 75;
3434

3535
// private readonly RaycastHit[] m_CachedHits = new RaycastHit[1];
3636
private readonly Collider[] m_CachedColliders = new Collider[SessionBase.MaxPlayers];
37-
private (PickUpBehavior[], FlagBehavior[][]) m_Behaviors;
38-
private VoxelMapNameProperty m_LastMapName;
39-
40-
public override void Initialize() => m_LastMapName = new VoxelMapNameProperty();
4137

4238
private (PickUpBehavior[], FlagBehavior[][]) GetBehaviors(SessionContext context)
4339
{
4440
MapManager mapManager = context.GetMapManager();
45-
StringProperty mapName = mapManager.Map.name;
46-
if (m_LastMapName == mapName) return m_Behaviors;
47-
m_LastMapName.SetTo(mapName);
48-
return m_Behaviors = (mapManager.Models.Values
49-
.Where(model => model is PickUpBehavior)
50-
.Cast<PickUpBehavior>()
51-
.OrderBy(model => model.Position.GetHashCode())
52-
.ToArray(),
53-
mapManager.Models.Values
54-
.Where(model => model is FlagBehavior)
55-
.GroupBy(model => model.Container.Require<TeamProperty>().Value)
56-
.OrderBy(group => group.Key)
57-
.Select(group => group.Cast<FlagBehavior>().ToArray()).ToArray());
41+
return (mapManager.Models.Values
42+
.Where(model => model is PickUpBehavior)
43+
.Cast<PickUpBehavior>()
44+
.OrderBy(model => model.Position.GetHashCode())
45+
.ToArray(),
46+
mapManager.Models.Values
47+
.Where(model => model is FlagBehavior)
48+
.GroupBy(model => model.Container.Require<TeamProperty>().Value)
49+
.OrderBy(group => group.Key)
50+
.Select(group => group.Cast<FlagBehavior>().ToArray()).ToArray());
5851
}
5952

6053
public override void BeginModify(in SessionContext context)
@@ -215,7 +208,7 @@ private void HandlePlayersNearFlag(in SessionContext context, FlagComponent flag
215208
{
216209
if (!m_CachedColliders[i].TryGetComponent(out PlayerTrigger playerTrigger)) continue;
217210

218-
var playerIdInFlag = (byte) playerTrigger.PlayerId;
211+
var playerIdInFlag = (byte)playerTrigger.PlayerId;
219212
Container player = context.GetModifyingPlayer(playerIdInFlag);
220213
if (player.Require<TeamProperty>() == flagTeam)
221214
{
@@ -264,7 +257,7 @@ private static void TryReturnCapturedFlag(byte playerTeam, DualScoresArray score
264257
for (var i = 0; i < enemyFlags.Length; i++)
265258
{
266259
FlagComponent enemyFlag = enemyFlags[i];
267-
if (enemyFlag.capturingPlayerId.WithValueEqualTo((byte) player.PlayerId)
260+
if (enemyFlag.capturingPlayerId.WithValueEqualTo((byte)player.PlayerId)
268261
&& enemyFlag.captureElapsedTimeUs > TakeFlagDurationUs) // Test if friendly returning enemy flag
269262
{
270263
enemyFlag.Clear();
@@ -279,12 +272,12 @@ protected override void SpawnPlayer(in SessionContext context, bool begin = fals
279272
Container player = context.player;
280273
if (begin)
281274
{
282-
player.Require<TeamProperty>().Value = (byte) (context.playerId % 2);
275+
player.Require<TeamProperty>().Value = (byte)(context.playerId % 2);
283276
player.ZeroIfWith<StatsComponent>();
284277
}
285278
player.Require<ByteIdProperty>().Value = 1;
286279
player.ZeroIfWith<CameraComponent>();
287-
if (player.With(out HealthProperty health)) health.Value = begin ? (byte) 0 : DefaultConfig.Active.respawnHealth;
280+
if (player.With(out HealthProperty health)) health.Value = begin ? (byte)0 : DefaultConfig.Active.respawnHealth;
288281
player.ZeroIfWith<RespawnTimerProperty>();
289282
if (player.With(out InventoryComponent inventory))
290283
{

Assets/Scripts/Voxelfield/Session/Mode/SecureAreaMode.cs

+12-16
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ public class SecureAreaMode : DeathmatchMode, IModeWithBuying
3838
private const byte BlueTeam = 0, RedTeam = 1;
3939
private const int MaxMoney = 7000;
4040

41-
private SiteBehavior[] m_SiteBehaviors;
42-
private VoxelMapNameProperty m_LastMapName;
43-
private readonly Collider[] m_CachedColliders = new Collider[SessionBase.MaxPlayers];
44-
45-
[SerializeField] private Color m_BlueColor = new Color(0.1764705882f, 0.5098039216f, 0.8509803922f),
46-
m_RedColor = new Color(0.8196078431f, 0.2156862745f, 0.1960784314f);
41+
[SerializeField] private Color m_BlueColor = new(0.1764705882f, 0.5098039216f, 0.8509803922f),
42+
m_RedColor = new(0.8196078431f, 0.2156862745f, 0.1960784314f);
4743
[SerializeField] private LayerMask m_PlayerTriggerMask = default;
4844
[SerializeField] private ushort[] m_ItemPrices = default;
45+
46+
private MapManager m_LastMapManager;
47+
private readonly Collider[] m_CachedColliders = new Collider[SessionBase.MaxPlayers];
4948
private SecureAreaConfig m_Config;
5049

5150
public uint SecureDurationUs => m_Config.secureDurationUs;
@@ -55,7 +54,6 @@ public class SecureAreaMode : DeathmatchMode, IModeWithBuying
5554

5655
public override void Initialize()
5756
{
58-
m_LastMapName = new VoxelMapNameProperty();
5957
m_Config = Config.Active.secureAreaConfig;
6058
SessionBase.RegisterSessionCommand("give_money");
6159
_randomIndices = Enumerable.Range(0, 2).Select(_ => new List<int>()).ToArray();
@@ -64,12 +62,9 @@ public override void Initialize()
6462
private SiteBehavior[] GetSiteBehaviors(in SessionContext context)
6563
{
6664
MapManager mapManager = context.GetMapManager();
67-
StringProperty mapName = mapManager.Map.name;
68-
if (m_LastMapName == mapName) return m_SiteBehaviors;
69-
m_LastMapName.SetTo(mapName);
70-
return m_SiteBehaviors = mapManager.Models.Values
71-
.Where(model => model.Container.Require<ModelIdProperty>() == ModelsProperty.Site)
72-
.Cast<SiteBehavior>().ToArray();
65+
return mapManager.Models.Values
66+
.Where(model => model.Container.Require<ModelIdProperty>() == ModelsProperty.Site)
67+
.Cast<SiteBehavior>().ToArray();
7368
}
7469

7570
public override uint GetItemEntityLifespanUs(in SessionContext context)
@@ -166,7 +161,8 @@ public override void Modify(in SessionContext context)
166161
Transform siteTransform = siteBehavior.transform;
167162
SiteComponent site = secureArea.sites[siteIndex];
168163
Vector3 bounds = siteBehavior.Container.Require<ExtentsProperty>();
169-
int playersInsideCount = context.PhysicsScene.OverlapBox(siteTransform.position, bounds / 2, m_CachedColliders, siteTransform.rotation, m_PlayerTriggerMask);
164+
int playersInsideCount =
165+
context.PhysicsScene.OverlapBox(siteTransform.position, bounds / 2, m_CachedColliders, siteTransform.rotation, m_PlayerTriggerMask);
170166
bool isRedInside = false, isBlueInside = false;
171167
for (var i = 0; i < playersInsideCount; i++)
172168
{
@@ -270,10 +266,10 @@ protected override void SpawnPlayer(in SessionContext context, bool begin = fals
270266
var teamProperty = player.Require<TeamProperty>();
271267
if (begin)
272268
{
273-
teamProperty.Value = (byte) ((context.playerId + 1) % 2);
269+
teamProperty.Value = (byte)((context.playerId + 1) % 2);
274270
player.ZeroIfWith<StatsComponent>();
275271
}
276-
else if (AtRoundSum(secureArea, scores, m_Config.maxRounds) && teamProperty.TryWithValue(out byte team)) teamProperty.Value = (byte) ((team + 1) % 2);
272+
else if (AtRoundSum(secureArea, scores, m_Config.maxRounds) && teamProperty.TryWithValue(out byte team)) teamProperty.Value = (byte)((team + 1) % 2);
277273
if (secureArea.roundTime.WithValue)
278274
{
279275
HealthProperty health = player.Health();

Assets/Scripts/Voxelfield/Session/SessionManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class SessionManager : SingletonBehavior<SessionManager>
3737
{
3838
private static IPAddress DefaultAddress => IPAddress.Loopback;
3939
public static int DefaultPort => 27015;
40-
private static readonly IPEndPoint DefaultEndPoint = new IPEndPoint(DefaultAddress, DefaultPort);
40+
private static readonly IPEndPoint DefaultEndPoint = new(DefaultAddress, DefaultPort);
4141
private static readonly string[] IpSeparator = {":"};
4242

4343
public static bool WantsApplicationQuit { get; set; }

Assets/Scripts/Voxels/Chunk.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Chunk : MonoBehaviour
1515
[SerializeField] private Material m_FoliageMaterial = default;
1616
[SerializeField, Layer] private int m_Layer = default;
1717

18-
private readonly MeshData m_SolidMeshData = new MeshData(), m_FoliageMeshData = new MeshData();
18+
private readonly MeshData m_SolidMeshData = new(), m_FoliageMeshData = new();
1919

2020
private ChunkManager m_ChunkManager;
2121
private Mesh m_SolidMesh, m_FoliageMesh;

Assets/Scripts/Voxels/ChunkManager.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public class ChunkManager : MonoBehaviour
4141
{
4242
[SerializeField] private GameObject m_ChunkPrefab = default;
4343
[SerializeField] private int m_ChunkSize = default;
44-
private readonly Stack<Chunk> m_ChunkPool = new Stack<Chunk>();
44+
private readonly Stack<Chunk> m_ChunkPool = new();
4545
private int m_PoolSize;
4646
private MapContainer m_LoadedMap;
4747

4848
public int ChunkSize => m_ChunkSize;
4949
public MapContainer Map { get; private set; }
50-
public Dictionary<Position3Int, Chunk> Chunks { get; } = new Dictionary<Position3Int, Chunk>();
51-
public MapProgressInfo ProgressInfo { get; set; } = new MapProgressInfo {stage = MapLoadingStage.Completed};
50+
public Dictionary<Position3Int, Chunk> Chunks { get; } = new();
51+
public MapProgressInfo ProgressInfo { get; set; } = new() {stage = MapLoadingStage.Completed};
5252

5353
public IEnumerator LoadMap(MapContainer map)
5454
{
@@ -155,9 +155,9 @@ private IEnumerator ManagePoolSize()
155155
}
156156
}
157157

158-
private static readonly TouchedChunks TouchedChunks = new TouchedChunks();
158+
private static readonly TouchedChunks TouchedChunks = new();
159159

160-
private static readonly ProfilerMarker ApplyVoxelChangeMarker = new ProfilerMarker("Apply Voxel Change");
160+
private static readonly ProfilerMarker ApplyVoxelChangeMarker = new("Apply Voxel Change");
161161

162162
/// <summary>
163163
/// Change the data of a chunk in the array.
@@ -384,7 +384,7 @@ void AddIfNeeded(int voxelChunkPositionSingle, in Position3Int axis)
384384
AddIfNeeded(voxelChunkPosition.z, new Position3Int {z = 1});
385385
}
386386

387-
private static readonly HashSet<Chunk> UpdateAdjacentChunks = new HashSet<Chunk>();
387+
private static readonly HashSet<Chunk> UpdateAdjacentChunks = new();
388388

389389
private void UpdateChunkMesh(Chunk chunk, in Position3Int voxelChunkPosition)
390390
{

Assets/Scripts/Voxels/Map/MapManager.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class MapManager : MonoBehaviour
3434

3535
public static ModelBehaviorBase[] ModelPrefabs { get; private set; }
3636

37-
public Dictionary<Position3Int, ModelBehaviorBase> Models { get; } = new Dictionary<Position3Int, ModelBehaviorBase>();
38-
public MapContainer Map { get; private set; } = new MapContainer();
37+
public Dictionary<Position3Int, ModelBehaviorBase> Models { get; } = new();
38+
public MapContainer Map { get; private set; } = new();
3939

4040
public ChunkManager ChunkManager { get; private set; }
4141

@@ -171,8 +171,8 @@ private void LoadModels(MapContainer map)
171171
{
172172
Models.Clear();
173173
foreach (Pool<ModelBehaviorBase> pool in m_ModelsPool) pool.ReturnAll();
174-
foreach (KeyValuePair<Position3Int, Container> pair in map.models.Map)
175-
InstantiateModel(pair.Key, pair.Value);
174+
foreach ((Position3Int position, Container model) in map.models.Map)
175+
InstantiateModel(position, model);
176176
}
177177

178178
private void InstantiateModel(in Position3Int position, Container model)

Assets/Scripts/Voxels/Map/ModelsProperty.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public class ModelsProperty : DictPropertyBase<Position3Int, Container>
2121
public override void Serialize(NetDataWriter writer)
2222
{
2323
writer.Put(m_Map.Count);
24-
foreach (KeyValuePair<Position3Int, Container> pair in m_Map)
24+
foreach ((Position3Int position, Container container) in m_Map)
2525
{
26-
Position3Int.Serialize(pair.Key, writer);
27-
var typeCount = (byte) pair.Value.Elements.Count;
26+
Position3Int.Serialize(position, writer);
27+
var typeCount = (byte) container.Elements.Count;
2828
writer.Put(typeCount);
29-
SerializeContainer(pair.Value, writer);
29+
SerializeContainer(container, writer);
3030
}
3131
}
3232

Assets/Scripts/Voxels/Noise.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private static void Shuffle(int seed)
2121
}
2222
}
2323

24-
private static readonly List<byte> StandardPermutations = new List<byte>
24+
private static readonly List<byte> StandardPermutations = new()
2525
{
2626
151, 160, 137, 91, 90, 15,
2727
131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
@@ -38,7 +38,7 @@ private static void Shuffle(int seed)
3838
138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180
3939
};
4040

41-
private static List<byte> _permutations = new List<byte>();
41+
private static List<byte> _permutations = new();
4242

4343
// F2 = (sqrt(3) - 1) / 2
4444
// G2 = (3 - sqrt(3)) / 6 = F2 / (1 + 2 * K)

Assets/Scripts/Voxels/Voxel.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public enum VoxelFlags : ushort
3434

3535
public struct Voxel
3636
{
37-
public static readonly Color32 Dirt = new Color32(39, 25, 10, 255),
38-
Stone = new Color32(39, 39, 39, 255),
37+
public static readonly Color32 Dirt = new(39, 25, 10, 255),
38+
Stone = new(39, 39, 39, 255),
3939
// Grass = new Color32(68, 144, 71, 255),
40-
Grass = new Color32(68, 144, 71, 255),
41-
Wood = new Color32(132, 83, 40, 255);
40+
Grass = new(68, 144, 71, 255),
41+
Wood = new(132, 83, 40, 255);
4242

4343
public const float
4444
TileSize = 256.0f,

0 commit comments

Comments
 (0)