DragonECS/src/Entity.cs

130 lines
4.1 KiB
C#
Raw Normal View History

2023-02-05 19:59:45 +08:00
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DCFApixels.DragonECS
{
2023-03-26 11:19:03 +08:00
// id - 32 bits
// gen - 16 bits
// world - 16 bits
[StructLayout(LayoutKind.Explicit, Pack = 2, Size = 8)]
2023-02-05 19:59:45 +08:00
public readonly struct ent : IEquatable<long>, IEquatable<ent>
{
2023-03-02 14:42:44 +08:00
public static readonly ent NULL = default;
2023-02-05 19:59:45 +08:00
2023-03-26 11:19:03 +08:00
[FieldOffset(0)]
private readonly long _full;
[FieldOffset(3)]
public readonly int id;
[FieldOffset(1)]
public readonly short gen;
[FieldOffset(0)]
public readonly short world;
2023-02-05 19:59:45 +08:00
#region Constructors
2023-02-05 19:59:45 +08:00
[EditorBrowsable(EditorBrowsableState.Never)]
2023-03-26 11:19:03 +08:00
public ent(int id, short gen, short world): this()
2023-02-05 19:59:45 +08:00
{
2023-03-26 11:19:03 +08:00
this.id = id;
this.gen = gen;
this.world = world;
2023-02-05 19:59:45 +08:00
}
2023-03-26 11:19:03 +08:00
internal ent(long full) : this()
2023-02-05 19:59:45 +08:00
{
2023-03-26 11:19:03 +08:00
_full = full;
2023-02-05 19:59:45 +08:00
}
#endregion
2023-02-05 19:59:45 +08:00
#region GetHashCode
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return unchecked((int)(_full)) ^ (int)(_full >> 32);
}
#endregion
#region Equals
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)
{
2023-03-26 11:19:03 +08:00
return obj is ent other && _full == other._full;
2023-02-05 19:59:45 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(ent other)
{
return _full == other._full;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(long other)
{
return _full == other;
}
#endregion
2023-03-26 11:19:03 +08:00
#region ToString
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString() => $"ent(id:{id} gen:{gen} world:{world})";
#endregion
2023-02-05 19:59:45 +08:00
#region operators
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-26 11:19:03 +08:00
public static bool operator ==(in ent a, in ent b) => a._full == b._full;
2023-02-05 19:59:45 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-26 11:19:03 +08:00
public static bool operator !=(in ent a, in ent b) => a._full != b._full;
2023-02-05 19:59:45 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-26 11:19:03 +08:00
public static explicit operator long(in ent a) => a._full;
2023-02-05 19:59:45 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-26 11:19:03 +08:00
public static explicit operator int(in ent a) => a.id;
2023-02-05 19:59:45 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-03-26 11:19:03 +08:00
public static explicit operator ent(in long a) => new ent(a);
2023-02-05 19:59:45 +08:00
#endregion
}
2023-02-06 01:27:32 +08:00
public static partial class entExtensions
2023-02-06 01:27:32 +08:00
{
2023-03-26 11:19:03 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAlive(this ref ent self)
{
bool result = EcsWorld.Worlds[self.world].EntityIsAlive(self.id, self.gen);
if (!result) self = ent.NULL;
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-02-06 01:27:32 +08:00
public static bool IsNull(this in ent self)
{
return self == ent.NULL;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref readonly T Read<T>(this in ent self)
where T : struct
{
return ref EcsWorld.Worlds[self.world].GetPool<T>().Read(self.id);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T Write<T>(this in ent self)
where T : struct
{
return ref EcsWorld.Worlds[self.world].GetPool<T>().Write(self.id);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Has<T>(this in ent self)
where T : struct
{
return EcsWorld.Worlds[self.world].GetPool<T>().Has(self.id);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Del<T>(this in ent self)
where T : struct
{
EcsWorld.Worlds[self.world].GetPool<T>().Del(self.id);
}
}
2023-02-05 19:59:45 +08:00
}