[Fix] Code visual

This commit is contained in:
2026-02-28 19:25:26 +07:00
parent bee20fd1f8
commit e24b30743b
39 changed files with 2611 additions and 2687 deletions
+115 -116
View File
@@ -4,138 +4,137 @@ using UnityEngine;
namespace YachtDice.Dice
{
public sealed class Dice : MonoBehaviour
{
[Serializable]
public struct Entry : IEquatable<Entry>
public class Dice : MonoBehaviour
{
[SerializeField] private int value;
[SerializeField] private Transform point;
public int Value => value;
public Transform Point => point;
public bool Equals(Entry other) => point == other.point;
public override bool Equals(object obj) => obj is Entry other && Equals(other);
public override int GetHashCode() => point != null ? point.GetHashCode() : 0;
}
[SerializeField] private List<Entry> entries = new();
private HashSet<Entry> entrySet;
private void Awake() => RebuildSet();
private void OnValidate() => RebuildSet();
public void Test()
{
if (!TryGetTopValue(out var top) || !TryGetBottomValue(out var bottom))
return;
Debug.Log($"Top Value: {top}, Bottom Value: {bottom}");
AlignToTopByLocalAngles();
}
private void RebuildSet()
{
entrySet = new HashSet<Entry>();
if (entries == null) return;
for (int i = 0; i < entries.Count; i++) entrySet.Add(entries[i]);
}
public bool TryGetTopValue(out int value) => TryGetExtremeValue(isTop: true, out value);
public bool TryGetBottomValue(out int value) => TryGetExtremeValue(isTop: false, out value);
public int AlignToTopByLocalAngles()
{
var e = GetExtremeEntryByWorldY(isTop: true);
AlignByFaceLocalAngles(e.Point);
return e.Value;
}
public int AlignToBottomByLocalAngles()
{
var e = GetExtremeEntryByWorldY(isTop: false);
AlignByFaceLocalAngles(e.Point);
return e.Value;
}
private bool TryGetExtremeValue(bool isTop, out int value)
{
value = default;
if (entrySet == null || entrySet.Count == 0)
return false;
bool found = false;
float bestY = isTop ? float.NegativeInfinity : float.PositiveInfinity;
int best = default;
foreach (var e in entrySet)
[Serializable]
public struct Entry : IEquatable<Entry>
{
var p = e.Point;
if (!p) continue;
[SerializeField] private int value;
[SerializeField] private Transform point;
float y = p.position.y;
if (!found || (isTop ? y > bestY : y < bestY))
{
found = true;
bestY = y;
best = e.Value;
}
public int Value => value;
public Transform Point => point;
public bool Equals(Entry other) => point == other.point;
public override bool Equals(object obj) => obj is Entry other && Equals(other);
public override int GetHashCode() => point != null ? point.GetHashCode() : 0;
}
if (!found) return false;
[SerializeField] private List<Entry> entries = new();
value = best;
return true;
}
private HashSet<Entry> entrySet;
private Entry GetExtremeEntryByWorldY(bool isTop)
{
if (entrySet == null || entrySet.Count == 0)
throw new InvalidOperationException("Dice: коллекция пуста.");
private void Awake() => RebuildSet();
private void OnValidate() => RebuildSet();
bool found = false;
float bestY = isTop ? float.NegativeInfinity : float.PositiveInfinity;
Entry best = default;
foreach (var e in entrySet)
public void Test()
{
var p = e.Point;
if (!p) continue;
if (!TryGetTopValue(out var top) || !TryGetBottomValue(out var bottom))
return;
float y = p.position.y;
if (!found || (isTop ? y > bestY : y < bestY))
{
found = true;
bestY = y;
best = e;
}
Debug.Log($"Top Value: {top}, Bottom Value: {bottom}");
AlignToTopByLocalAngles();
}
if (!found)
throw new InvalidOperationException("Dice: все Transform == null.");
private void RebuildSet()
{
entrySet = new HashSet<Entry>();
if (entries == null) return;
for (int i = 0; i < entries.Count; i++) entrySet.Add(entries[i]);
}
return best;
}
public bool TryGetTopValue(out int value) => TryGetExtremeValue(isTop: true, out value);
public bool TryGetBottomValue(out int value) => TryGetExtremeValue(isTop: false, out value);
private static float Norm180(float a)
{
a %= 360f;
return a > 180f ? a - 360f : (a < -180f ? a + 360f : a);
}
public int AlignToTopByLocalAngles()
{
var e = GetExtremeEntryByWorldY(isTop: true);
AlignByFaceLocalAngles(e.Point);
return e.Value;
}
private void AlignByFaceLocalAngles(Transform facePoint)
{
if (!facePoint) throw new InvalidOperationException("Dice: facePoint == null.");
public int AlignToBottomByLocalAngles()
{
var e = GetExtremeEntryByWorldY(isTop: false);
AlignByFaceLocalAngles(e.Point);
return e.Value;
}
transform.localEulerAngles = Vector3.Scale(facePoint.localEulerAngles, new Vector3(-1f, -1f, -1f));
private bool TryGetExtremeValue(bool isTop, out int value)
{
value = default;
var e = transform.localEulerAngles;
transform.localEulerAngles = new Vector3(Norm180(e.x), Norm180(e.y), Norm180(e.z));
if (entrySet == null || entrySet.Count == 0)
return false;
bool found = false;
float bestY = isTop ? float.NegativeInfinity : float.PositiveInfinity;
int best = default;
foreach (var e in entrySet)
{
var p = e.Point;
if (!p) continue;
float y = p.position.y;
if (!found || (isTop ? y > bestY : y < bestY))
{
found = true;
bestY = y;
best = e.Value;
}
}
if (!found) return false;
value = best;
return true;
}
private Entry GetExtremeEntryByWorldY(bool isTop)
{
if (entrySet == null || entrySet.Count == 0)
throw new InvalidOperationException("Dice: коллекция пуста.");
bool found = false;
float bestY = isTop ? float.NegativeInfinity : float.PositiveInfinity;
Entry best = default;
foreach (var e in entrySet)
{
var p = e.Point;
if (!p) continue;
float y = p.position.y;
if (!found || (isTop ? y > bestY : y < bestY))
{
found = true;
bestY = y;
best = e;
}
}
if (!found)
throw new InvalidOperationException("Dice: все Transform == null.");
return best;
}
private static float Norm180(float a)
{
a %= 360f;
return a > 180f ? a - 360f : (a < -180f ? a + 360f : a);
}
private void AlignByFaceLocalAngles(Transform facePoint)
{
if (!facePoint) throw new InvalidOperationException("Dice: facePoint == null.");
transform.localEulerAngles = Vector3.Scale(facePoint.localEulerAngles, new Vector3(-1f, -1f, -1f));
var e = transform.localEulerAngles;
transform.localEulerAngles = new Vector3(Norm180(e.x), Norm180(e.y), Norm180(e.z));
}
}
}
}