DragonECS/src/entlong.cs

324 lines
11 KiB
C#
Raw Normal View History

2023-05-26 04:25:09 +08:00
#pragma warning disable IDE1006
2024-01-25 20:11:28 +08:00
#pragma warning disable CS8981
2023-06-26 02:53:55 +08:00
using DCFApixels.DragonECS.Internal;
2023-05-26 04:25:09 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DCFApixels.DragonECS
{
2023-05-26 04:25:09 +08:00
// [ id 32 | gen 16 | world 16 ]
/// <summary>Strong identifier/Permanent entity identifier</summary>
[StructLayout(LayoutKind.Explicit, Pack = 2, Size = 8)]
[DebuggerTypeProxy(typeof(DebuggerProxy))]
2023-07-17 15:54:28 +08:00
[Serializable]
public readonly struct entlong : IEquatable<long>, IEquatable<entlong>
{
public static readonly entlong NULL = default;
[FieldOffset(0)]
internal readonly long full; //Union
2023-07-17 15:54:28 +08:00
[FieldOffset(0), NonSerialized]
internal readonly int id;
2023-07-17 15:54:28 +08:00
[FieldOffset(4), NonSerialized]
internal readonly short gen;
2023-07-17 15:54:28 +08:00
[FieldOffset(6), NonSerialized]
internal readonly short world;
#region Properties
public bool IsAlive
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-28 01:20:35 +08:00
get { return EcsWorld.GetWorld(world).IsAlive(id, gen); }
}
public bool IsNull
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-28 01:20:35 +08:00
get { return full == 0L; }
}
public int ID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2024-02-28 01:20:35 +08:00
if (!IsAlive) { Throw.Ent_ThrowIsNotAlive(this); }
#endif
return id;
}
}
public short Gen
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2024-02-28 01:20:35 +08:00
if (!IsAlive) { Throw.Ent_ThrowIsNotAlive(this); }
#endif
return gen;
}
}
public EcsWorld World
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2024-02-28 01:20:35 +08:00
if (!IsAlive) { Throw.Ent_ThrowIsNotAlive(this); }
#endif
2023-06-22 02:02:43 +08:00
return EcsWorld.GetWorld(world);
}
}
public short WorldID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
2023-06-02 01:20:46 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2024-02-28 01:20:35 +08:00
if (!IsAlive) { Throw.Ent_ThrowIsNotAlive(this); }
#endif
return world;
}
}
#endregion
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public entlong(int id, short gen, short world) : this()
{
this.id = id;
this.gen = gen;
this.world = world;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal entlong(long full) : this()
{
this.full = full;
}
2023-11-21 10:43:02 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe entlong NewUnsafe(long id, long gen, long world)
{
long x = id << 48 | gen << 32 | id;
return *(entlong*)&x;
}
#endregion
#region TryGetters
public bool TryGetID(out int id)
{
id = this.id;
return IsAlive;
}
public bool TryGetWorld(out EcsWorld world)
{
2023-06-22 02:02:43 +08:00
world = EcsWorld.GetWorld(this.world);
return IsAlive;
}
2023-07-03 02:40:13 +08:00
public bool TryGetWorldID(out int worldID)
{
worldID = world;
return IsAlive;
}
2024-03-03 00:28:02 +08:00
public void Unpack(out int id, out EcsWorld world)
2023-07-03 02:40:13 +08:00
{
world = EcsWorld.GetWorld(this.world);
id = this.id;
}
2024-03-03 00:28:02 +08:00
public void Unpack(out int id, out int worldID)
2023-07-03 02:40:13 +08:00
{
worldID = world;
id = this.id;
}
2024-03-03 00:28:02 +08:00
public void Unpack(out int id, out short gen, out short worldID)
{
worldID = world;
gen = this.gen;
id = this.id;
}
public bool TryUnpack(out int id, out EcsWorld world)
{
2023-06-22 02:02:43 +08:00
world = EcsWorld.GetWorld(this.world);
id = this.id;
return IsAlive;
}
2024-03-03 00:28:02 +08:00
public bool TryUnpack(out int id, out int worldID)
{
worldID = world;
id = this.id;
return IsAlive;
}
public bool TryUnpack(out int id, out short gen, out short worldID)
2023-07-03 02:40:13 +08:00
{
worldID = world;
2024-03-03 00:28:02 +08:00
gen = this.gen;
2023-07-03 02:40:13 +08:00
id = this.id;
return IsAlive;
}
#endregion
2024-02-28 01:20:35 +08:00
#region Operators
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-28 01:20:35 +08:00
public static bool operator ==(entlong a, entlong b) { return a.full == b.full; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-28 01:20:35 +08:00
public static bool operator !=(entlong a, entlong b) { return a.full != b.full; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-28 01:20:35 +08:00
public static explicit operator long(entlong a) { return a.full; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-28 01:20:35 +08:00
public static explicit operator entlong(long a) { return new entlong(a); }
2023-07-17 15:54:28 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-28 01:20:35 +08:00
public static explicit operator int(entlong a) { return a.ID; }
#endregion
#region Other
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() { return unchecked((int)full) ^ (int)(full >> 32); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString() { return $"entity(id:{id} g:{gen} w:{world} {(IsNull ? "null" : IsAlive ? "alive" : "not alive")})"; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj) { return obj is entlong other && full == other.full; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(entlong other) { return full == other.full; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(long other) { return full == other; }
2024-01-26 18:58:54 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out int id, out int gen, out int world)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2024-02-28 01:20:35 +08:00
if (!IsAlive) { Throw.Ent_ThrowIsNotAlive(this); }
2024-01-26 18:58:54 +08:00
#endif
id = this.id;
gen = this.gen;
world = this.world;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out int id, out int world)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2024-02-28 01:20:35 +08:00
if (!IsAlive) { Throw.Ent_ThrowIsNotAlive(this); }
2024-01-26 18:58:54 +08:00
#endif
id = this.id;
world = this.world;
}
2023-11-15 17:47:11 +08:00
2024-02-29 03:28:13 +08:00
internal class DebuggerProxy
{
2024-02-29 03:28:13 +08:00
private List<object> _componentsList = new List<object>();
private entlong _value;
2024-02-28 01:20:35 +08:00
public long full { get { return _value.full; } }
public int id { get { return _value.id; } }
public int gen { get { return _value.gen; } }
public int world { get { return _value.world; } }
public EntState State { get { return _value.IsNull ? EntState.Null : _value.IsAlive ? EntState.Alive : EntState.Dead; } }
public EcsWorld EcsWorld { get { return EcsWorld.GetWorld(world); } }
public IEnumerable<object> components
{
get
{
_value.World.GetComponents(_value.ID, _componentsList);
return _componentsList;
}
}
public DebuggerProxy(entlong value)
{
_value = value;
2024-02-29 03:28:13 +08:00
}
public DebuggerProxy(EntitySlotInfo value)
{
_value = new entlong(value.id, value.gen, value.world);
}
public enum EntState { Null, Dead, Alive, }
}
#endregion
}
2024-02-29 03:28:13 +08:00
2024-03-01 22:02:36 +08:00
[DebuggerTypeProxy(typeof(DebuggerProxy))]
2024-02-29 03:28:13 +08:00
public readonly struct EntitySlotInfo : IEquatable<EntitySlotInfo>
{
public readonly int id;
public readonly short gen;
public readonly short world;
#region Constructors
public EntitySlotInfo(int id, short gen, short world)
{
this.id = id;
this.gen = gen;
this.world = world;
}
#endregion
#region Operators
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(EntitySlotInfo a, EntitySlotInfo b)
{
return a.id == b.id &&
a.gen == b.gen &&
a.world == b.world;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(EntitySlotInfo a, EntitySlotInfo b)
{
return a.id != b.id ||
a.gen != b.gen ||
a.world != b.world;
}
#endregion
#region Other
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() { return unchecked(id ^ gen ^ (world * EcsConsts.MAGIC_PRIME)); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString() { return $"slot(id:{id} g:{gen} w:{world})"; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj) { return obj is EntitySlotInfo other && this == other; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(EntitySlotInfo other) { return this == other; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out int id, out int gen, out int world)
{
id = this.id;
gen = this.gen;
world = this.world;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out int id, out int world)
{
id = this.id;
world = this.world;
}
#endregion
2024-03-01 22:02:36 +08:00
internal class DebuggerProxy
{
private List<object> _componentsList = new List<object>();
private entlong _value;
public long full { get { return _value.full; } }
public int id { get { return _value.id; } }
public int gen { get { return _value.gen; } }
public int world { get { return _value.world; } }
public EntState State { get { return _value.IsNull ? EntState.Null : _value.IsAlive ? EntState.Alive : EntState.Dead; } }
public EcsWorld EcsWorld { get { return EcsWorld.GetWorld(world); } }
public IEnumerable<object> components
{
get
{
_value.World.GetComponents(_value.ID, _componentsList);
return _componentsList;
}
}
public DebuggerProxy(EntitySlotInfo value)
{
_value = new entlong(value.id, value.gen, value.world);
}
public enum EntState { Null, Dead, Alive, }
}
2024-02-29 03:28:13 +08:00
}
}