| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using Lattice.ECS.Core; |
| | | 8 | | using static Lattice.ECS.Core.DeterministicHash; |
| | | 9 | | |
| | | 10 | | namespace Lattice.Core |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// 实体引用 - FrameSync 风格(对齐 EntityRef) |
| | | 14 | | /// |
| | | 15 | | /// 用于标识 ECS 中的唯一实体。 |
| | | 16 | | /// 实现了稀疏集合的 ECS 模型(类似 enTT)。 |
| | | 17 | | /// |
| | | 18 | | /// 内存布局:8 字节(Index + Version) |
| | | 19 | | /// - Index: int (4 bytes) - 实体在存储中的索引 |
| | | 20 | | /// - Version: int (4 bytes) - 版本号,用于检测过期引用 |
| | | 21 | | /// - Raw: ulong (8 bytes) - 与 Index/Version 重叠,用于快速比较 |
| | | 22 | | /// |
| | | 23 | | /// 设计参考:Bevy ECS, FrameSync (Quantum), Entitas |
| | | 24 | | /// </summary> |
| | | 25 | | [StructLayout(LayoutKind.Explicit, Size = 8)] |
| | | 26 | | public readonly struct EntityRef : IEquatable<EntityRef> |
| | | 27 | | { |
| | | 28 | | /// <summary> |
| | | 29 | | /// 实体结构体的字节大小(8字节) |
| | | 30 | | /// </summary> |
| | | 31 | | public const int Size = 8; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// 实体在内部数组中的索引位置 |
| | | 35 | | /// </summary> |
| | | 36 | | [FieldOffset(0)] |
| | | 37 | | public readonly int Index; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// 版本号:槽位复用时递增,用于检测失效引用 |
| | | 41 | | /// 最高位同时用作活跃标志位(参见 EntityRegistry.ActiveBit) |
| | | 42 | | /// </summary> |
| | | 43 | | [FieldOffset(4)] |
| | | 44 | | public readonly int Version; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// 原始64位值,用于快速比较和哈希计算 |
| | | 48 | | /// </summary> |
| | | 49 | | [FieldOffset(0)] |
| | | 50 | | public readonly ulong Raw; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// 表示无效实体的静态实例(Index=0, Version=0) |
| | | 54 | | /// </summary> |
| | | 55 | | public static readonly EntityRef None = default; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// 无效实体引用(None 的别名,与 FrameSync 命名一致) |
| | | 59 | | /// </summary> |
| | | 60 | | public static readonly EntityRef Invalid = default; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// 判断此实体标识符是否有效(非零) |
| | | 64 | | /// </summary> |
| | 0 | 65 | | public bool IsValid => Raw != 0; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// 创建一个新的实体引用 |
| | | 69 | | /// </summary> |
| | | 70 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 71 | | public EntityRef(int index, int version) |
| | | 72 | | { |
| | 33624 | 73 | | Index = index; |
| | 33624 | 74 | | Version = version; |
| | 33624 | 75 | | Raw = (ulong)(uint)index | ((ulong)(uint)version << 32); |
| | 33624 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// 从原始值创建实体引用(用于序列化) |
| | | 80 | | /// </summary> |
| | | 81 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 82 | | public EntityRef(ulong raw) |
| | | 83 | | { |
| | 3 | 84 | | Raw = raw; |
| | 3 | 85 | | Index = (int)(raw & 0xFFFFFFFF); |
| | 3 | 86 | | Version = (int)(raw >> 32); |
| | 3 | 87 | | } |
| | | 88 | | |
| | | 89 | | #region 相等性比较 |
| | | 90 | | |
| | | 91 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 92 | | public static bool operator ==(EntityRef a, EntityRef b) |
| | | 93 | | { |
| | 127 | 94 | | return a.Raw == b.Raw; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 98 | | public static bool operator !=(EntityRef a, EntityRef b) |
| | | 99 | | { |
| | 226 | 100 | | return a.Raw != b.Raw; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 104 | | public bool Equals(EntityRef other) |
| | | 105 | | { |
| | 35 | 106 | | return Raw == other.Raw; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | public override bool Equals(object? obj) |
| | | 110 | | { |
| | 0 | 111 | | return obj is EntityRef other && Equals(other); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 115 | | public override int GetHashCode() |
| | | 116 | | { |
| | | 117 | | // 使用确定性哈希(跨平台一致) |
| | 0 | 118 | | return DeterministicHash.GetHashCode(this); |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | #endregion |
| | | 122 | | |
| | | 123 | | #region 转换 |
| | | 124 | | |
| | | 125 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 126 | | public ulong ToRaw() |
| | | 127 | | { |
| | 0 | 128 | | return Raw; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 132 | | public static EntityRef FromRaw(ulong raw) |
| | | 133 | | { |
| | 3 | 134 | | return new EntityRef(raw); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | #endregion |
| | | 138 | | |
| | | 139 | | #region 字符串表示 |
| | | 140 | | |
| | | 141 | | public override string ToString() |
| | | 142 | | { |
| | 1 | 143 | | return $"E.{Index:D5}.{Version:D3}"; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public static bool TryParse(string str, out EntityRef result) |
| | | 147 | | { |
| | 0 | 148 | | result = None; |
| | 0 | 149 | | if (string.IsNullOrEmpty(str) || str.Length < 3) |
| | 0 | 150 | | return false; |
| | | 151 | | |
| | 0 | 152 | | if (!str.StartsWith("E.", StringComparison.Ordinal)) |
| | 0 | 153 | | return false; |
| | | 154 | | |
| | 0 | 155 | | var parts = str[2..].Split('.'); |
| | 0 | 156 | | if (parts.Length != 2) |
| | 0 | 157 | | return false; |
| | | 158 | | |
| | 0 | 159 | | if (int.TryParse(parts[0], out int index) && |
| | 0 | 160 | | int.TryParse(parts[1], out int version)) |
| | | 161 | | { |
| | 0 | 162 | | result = new EntityRef(index, version); |
| | 0 | 163 | | return true; |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | return false; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | #endregion |
| | | 170 | | |
| | | 171 | | #region 序列化支持 |
| | | 172 | | |
| | | 173 | | // 序列化方法在 FrameSerializer 中实现 |
| | | 174 | | |
| | | 175 | | #endregion |
| | | 176 | | } |
| | | 177 | | } |