refactor: cleaning up code to newer C# standards

This commit is contained in:
D. Moonfire 2023-01-14 16:51:06 -06:00
parent 6b9c9cf5e3
commit e4c9e82b99
18 changed files with 1180 additions and 1153 deletions

View file

@ -3,8 +3,8 @@ using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
namespace MfGames.Gallium
{
namespace MfGames.Gallium;
/// <summary>
/// A low-overhead entity with identification.
/// </summary>
@ -354,7 +354,7 @@ namespace MfGames.Gallium
return this with
{
Components = this.Components.SetItem(typeof(T1), component),
Components = this.Components.SetItem(typeof(T1), component)
};
}
@ -393,7 +393,7 @@ namespace MfGames.Gallium
return this with
{
Components = this.Components.Add(typeof(T1), component),
Components = this.Components.Add(typeof(T1), component)
};
}
@ -428,7 +428,7 @@ namespace MfGames.Gallium
return this with
{
Components = this.Components.Remove(type),
Components = this.Components.Remove(type)
};
}
@ -456,7 +456,7 @@ namespace MfGames.Gallium
{
return this with
{
Id = Interlocked.Increment(ref nextId),
Id = Interlocked.Increment(ref nextId)
};
}
@ -470,4 +470,3 @@ namespace MfGames.Gallium
return this.Components.Keys;
}
}
}

View file

@ -2,8 +2,8 @@ using System;
using System.Collections.Generic;
using System.Linq;
namespace MfGames.Gallium
{
namespace MfGames.Gallium;
public static class JoinEntityExtensions
{
/// <summary>
@ -26,4 +26,3 @@ namespace MfGames.Gallium
return input.Join(other, a => a.Id, a => a.Id, merge);
}
}
}

View file

@ -16,7 +16,8 @@ public static class SelectComponentExtensions
/// <param name="entities">The entities to process.</param>
/// <typeparam name="T1">The component type being searched.</typeparam>
/// <returns>A sequence of T1.</returns>
public static IEnumerable<T1> SelectComponent<T1>(this IEnumerable<Entity> entities)
public static IEnumerable<T1> SelectComponent<T1>(
this IEnumerable<Entity> entities)
{
foreach (Entity entity in entities)
{

View file

@ -37,7 +37,8 @@ public static class SelectComponentOrDefaultExtensions
/// <param name="entities">The entities to process.</param>
/// <typeparam name="T1">The component type being searched.</typeparam>
/// <returns>A sequence of T1.</returns>
public static IEnumerable<T1?> SelectComponentOrDefault<T1>(this IEnumerable<Entity> entities)
public static IEnumerable<T1?> SelectComponentOrDefault<T1>(
this IEnumerable<Entity> entities)
{
foreach (Entity entity in entities)
{

View file

@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
namespace MfGames.Gallium
{
namespace MfGames.Gallium;
public static class SelectEntityExtensions
{
/// <summary>
@ -26,7 +26,9 @@ namespace MfGames.Gallium
Func<Entity, T1, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
@ -51,7 +53,9 @@ namespace MfGames.Gallium
Func<Entity, T1, T2, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
@ -77,7 +81,9 @@ namespace MfGames.Gallium
Func<Entity, T1, T2, T3, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
@ -104,7 +110,9 @@ namespace MfGames.Gallium
Func<Entity, T1, T2, T3, T4, Entity?> selectWithComponents,
bool includeEntitiesWithoutComponents = true)
{
return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null);
return entities.SelectEntity(
selectWithComponents,
includeEntitiesWithoutComponents ? a => a : a => null);
}
/// <summary>
@ -167,7 +175,8 @@ namespace MfGames.Gallium
{
foreach (Entity entity in entities)
{
Entity? result = entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2)
Entity? result = entity.TryGet(out T1 value1)
&& entity.TryGet(out T2 value2)
? selectWithComponents?.Invoke(entity, value1, value2)
: selectWithoutComponents?.Invoke(entity);
@ -205,8 +214,14 @@ namespace MfGames.Gallium
foreach (Entity entity in entities)
{
Entity? result =
entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) && entity.TryGet(out T3 value3)
? selectWithComponents?.Invoke(entity, value1, value2, value3)
entity.TryGet(out T1 value1)
&& entity.TryGet(out T2 value2)
&& entity.TryGet(out T3 value3)
? selectWithComponents?.Invoke(
entity,
value1,
value2,
value3)
: selectWithoutComponents?.Invoke(entity);
if (result != null)
@ -248,7 +263,12 @@ namespace MfGames.Gallium
&& entity.TryGet(out T2 value2)
&& entity.TryGet(out T3 value3)
&& entity.TryGet(out T4 value4)
? selectWithComponents?.Invoke(entity, value1, value2, value3, value4)
? selectWithComponents?.Invoke(
entity,
value1,
value2,
value3,
value4)
: selectWithoutComponents?.Invoke(entity);
if (result != null)
@ -258,4 +278,3 @@ namespace MfGames.Gallium
}
}
}
}

View file

@ -189,7 +189,9 @@ public static class SplitEntityExtensions
Type t1,
Func<Entity, object, bool> test)
{
return SplitEntity(entities, a => a.Has(t1) && test(a, a.Get<object>(t1)));
return SplitEntity(
entities,
a => a.Has(t1) && test(a, a.Get<object>(t1)));
}
/// <summary>

View file

@ -2,8 +2,8 @@ using System;
using System.Collections.Generic;
using System.Linq;
namespace MfGames.Gallium
{
namespace MfGames.Gallium;
public static class WhereEntityExtensions
{
public static IEnumerable<Entity> WhereEntity<T1>(
@ -45,4 +45,3 @@ namespace MfGames.Gallium
x.Get<T4>()));
}
}
}

View file

@ -1,28 +1,31 @@
using System.Collections.Generic;
using System.Linq;
namespace MfGames.Gallium
{
namespace MfGames.Gallium;
public static class WhereEntityHasExtensions
{
public static IEnumerable<Entity> WhereEntityHas<T1>(this IEnumerable<Entity> entities)
public static IEnumerable<Entity> WhereEntityHas<T1>(
this IEnumerable<Entity> entities)
{
return entities.Where(x => x.Has<T1>());
}
public static IEnumerable<Entity> WhereEntityHasAll<T1, T2>(this IEnumerable<Entity> entities)
public static IEnumerable<Entity> WhereEntityHasAll
<T1, T2>(this IEnumerable<Entity> entities)
{
return entities.Where(x => x.HasAll<T1, T2>());
}
public static IEnumerable<Entity> WhereEntityHasAll<T1, T2, T3>(this IEnumerable<Entity> entities)
public static IEnumerable<Entity> WhereEntityHasAll
<T1, T2, T3>(this IEnumerable<Entity> entities)
{
return entities.Where(x => x.HasAll<T1, T2, T3>());
}
public static IEnumerable<Entity> WhereEntityHasAll<T1, T2, T3, T4>(this IEnumerable<Entity> entities)
public static IEnumerable<Entity> WhereEntityHasAll
<T1, T2, T3, T4>(this IEnumerable<Entity> entities)
{
return entities.Where(x => x.HasAll<T1, T2, T3, T4>());
}
}
}

View file

@ -1,28 +1,31 @@
using System.Collections.Generic;
using System.Linq;
namespace MfGames.Gallium
{
namespace MfGames.Gallium;
public static class WhereEntityNotHasExtensions
{
public static IEnumerable<Entity> WhereEntityNotHas<T1>(this IEnumerable<Entity> entities)
public static IEnumerable<Entity> WhereEntityNotHas<T1>(
this IEnumerable<Entity> entities)
{
return entities.Where(x => !x.Has<T1>());
}
public static IEnumerable<Entity> WhereEntityNotHasAll<T1, T2>(this IEnumerable<Entity> entities)
public static IEnumerable<Entity> WhereEntityNotHasAll
<T1, T2>(this IEnumerable<Entity> entities)
{
return entities.Where(x => !x.HasAll<T1, T2>());
}
public static IEnumerable<Entity> WhereEntityNotHasAll<T1, T2, T3>(this IEnumerable<Entity> entities)
public static IEnumerable<Entity> WhereEntityNotHasAll
<T1, T2, T3>(this IEnumerable<Entity> entities)
{
return entities.Where(x => !x.HasAll<T1, T2, T3>());
}
public static IEnumerable<Entity> WhereEntityNotHasAll<T1, T2, T3, T4>(this IEnumerable<Entity> entities)
public static IEnumerable<Entity> WhereEntityNotHasAll
<T1, T2, T3, T4>(this IEnumerable<Entity> entities)
{
return entities.Where(x => !x.HasAll<T1, T2, T3, T4>());
}
}
}

View file

@ -1,10 +1,12 @@
using System;
using MfGames.Gallium;
using Xunit;
using Xunit.Abstractions;
namespace MfGames.Gallium.Tests
{
namespace Gallium.Tests;
public class EntityTests : GalliumTestsBase
{
public EntityTests(ITestOutputHelper output)
@ -213,4 +215,3 @@ namespace MfGames.Gallium.Tests
Assert.True(entity1 == entity2);
}
}
}

View file

@ -1,9 +1,11 @@
using System.Linq;
using MfGames.Gallium;
using Xunit;
namespace MfGames.Gallium.Tests
{
namespace Gallium.Tests;
public class EnumerableEntityTests
{
[Fact]
@ -16,7 +18,7 @@ namespace MfGames.Gallium.Tests
new Entity().Add("2")
.Add(new TestComponent2()),
new Entity().Add("3")
.Add(new TestComponent1()),
.Add(new TestComponent1())
};
Assert.Equal(
@ -40,7 +42,7 @@ namespace MfGames.Gallium.Tests
.Add(new TestComponent2()),
new Entity().Add("3")
.Add(new TestComponent1())
.Add(new TestComponent2()),
.Add(new TestComponent2())
};
Assert.Equal(
@ -67,12 +69,13 @@ namespace MfGames.Gallium.Tests
new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a())
.Add(new TestComponent1())
.Add(new TestComponent2()),
.Add(new TestComponent2())
};
Assert.Equal(
new[] { "1", "2", "3-TestComponent3a" },
entities.SelectEntity<TestComponent1, TestComponent2, ITestComponent3>(
entities
.SelectEntity<TestComponent1, TestComponent2, ITestComponent3>(
(
e,
_,
@ -96,7 +99,7 @@ namespace MfGames.Gallium.Tests
new Entity().Add("2")
.Add(new TestComponent2()),
new Entity().Add("3")
.Add(new TestComponent1()),
.Add(new TestComponent1())
};
Assert.Equal(
@ -117,7 +120,7 @@ namespace MfGames.Gallium.Tests
.Add(new TestComponent2())
.Add(new TestComponent1()),
new Entity().Add("3")
.Add(new TestComponent1()),
.Add(new TestComponent1())
};
Assert.Equal(
@ -140,12 +143,14 @@ namespace MfGames.Gallium.Tests
.Add(new TestComponent2())
.Add(new TestComponent1()),
new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a()),
.Add<ITestComponent3>(new TestComponent3a())
};
Assert.Equal(
new[] { "1" },
entities.WhereEntityHasAll<TestComponent1, TestComponent2, ITestComponent3>()
entities
.WhereEntityHasAll<TestComponent1, TestComponent2,
ITestComponent3>()
.Select(x => x.Get<string>())
.ToArray());
}
@ -160,7 +165,7 @@ namespace MfGames.Gallium.Tests
new Entity().Add("2")
.Add(new TestComponent2()),
new Entity().Add("3")
.Add(new TestComponent1()),
.Add(new TestComponent1())
};
Assert.Equal(
@ -180,7 +185,7 @@ namespace MfGames.Gallium.Tests
new Entity().Add("2")
.Add(new TestComponent2())
.Add(new TestComponent1()),
new Entity().Add("3"),
new Entity().Add("3")
};
Assert.Equal(
@ -202,14 +207,15 @@ namespace MfGames.Gallium.Tests
.Add(new TestComponent2())
.Add<ITestComponent3>(new TestComponent3b()),
new Entity().Add("3")
.Add<ITestComponent3>(new TestComponent3a()),
.Add<ITestComponent3>(new TestComponent3a())
};
Assert.Equal(
new string[] { "1", "3" },
entities.WhereEntityNotHasAll<TestComponent1, TestComponent2, ITestComponent3>()
new[] { "1", "3" },
entities
.WhereEntityNotHasAll<TestComponent1, TestComponent2,
ITestComponent3>()
.Select(x => x.Get<string>())
.ToArray());
}
}
}

View file

@ -3,8 +3,8 @@ using Serilog.Core;
using Xunit.Abstractions;
namespace MfGames.Gallium.Tests
{
namespace Gallium.Tests;
/// <summary>
/// Common initialization logic for Gallium-based tests including setting
/// up containers, logging, and Serilog.
@ -38,4 +38,3 @@ namespace MfGames.Gallium.Tests
/// </summary>
protected Logger Logger { get; }
}
}

View file

@ -1,6 +1,5 @@
namespace MfGames.Gallium.Tests
{
namespace Gallium.Tests;
public interface ITestComponent3
{
}
}

View file

@ -1,6 +1,5 @@
namespace MfGames.Gallium.Tests
{
namespace Gallium.Tests;
public class TestComponent1
{
}
}

View file

@ -1,6 +1,5 @@
namespace MfGames.Gallium.Tests
{
namespace Gallium.Tests;
public class TestComponent2
{
}
}

View file

@ -1,6 +1,5 @@
namespace MfGames.Gallium.Tests
{
namespace Gallium.Tests;
public class TestComponent3a : ITestComponent3
{
}
}

View file

@ -1,6 +1,5 @@
namespace MfGames.Gallium.Tests
{
namespace Gallium.Tests;
public class TestComponent3b : ITestComponent3
{
}
}