Polishes
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
using System.Collections.Generic;
|
||||
using ProjectM.Simulation;
|
||||
using Unity.Entities;
|
||||
using Unity.Mathematics;
|
||||
using Unity.NetCode;
|
||||
using Unity.Transforms;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace ProjectM.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// Client-only ENEMY VISIBILITY markers — makes living enemies (especially the last one) easy to find so a room
|
||||
/// never devolves into hunting an unseen straggler. Observe-only presentation <see cref="SystemBase"/> in
|
||||
/// <see cref="PresentationSystemGroup"/> (same shape as <see cref="OnboardingSystem"/> / <see cref="HudSystem"/>:
|
||||
/// reads replicated state, never mutates the sim, never destroys a ghost). Owns its own runtime UIDocument
|
||||
/// (sortingOrder 48 — just under the HUD's 50) and a POOL of marker glyphs: each living enemy within
|
||||
/// <see cref="FeelConfig.EnemyMarkerRange"/> of the local player gets either a clamped EDGE ARROW when off-screen
|
||||
/// (rotated toward it — the "it's over here" cue, reusing OnboardingSystem's WorldToScreen edge math) or a subtle
|
||||
/// overhead PIP when on-screen. The pips fade toward <see cref="FeelConfig.EnemyMarkerMinAlpha"/> as the on-screen
|
||||
/// enemy count rises (declutter a swarm); off-screen arrows always stay full so a straggler is never missed. The
|
||||
/// boss is excluded (it has its own HUD presence bar); corpses (<see cref="Dying"/>) are excluded.
|
||||
/// </summary>
|
||||
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
|
||||
[UpdateInGroup(typeof(PresentationSystemGroup))]
|
||||
public partial class EnemyMarkerSystem : SystemBase
|
||||
{
|
||||
const int MaxMarkers = 32; // pool cap (a room never fields this many; when few remain, all are marked)
|
||||
const float Margin = 48f; // screen-edge inset for off-screen arrows (panel units)
|
||||
|
||||
GameObject _go;
|
||||
UIDocument _doc;
|
||||
bool _built;
|
||||
readonly List<Label> _pool = new();
|
||||
readonly List<float3> _positions = new();
|
||||
|
||||
protected override void OnStartRunning()
|
||||
{
|
||||
if (_go != null) return;
|
||||
_go = new GameObject("~EnemyMarkers");
|
||||
_doc = _go.AddComponent<UIDocument>();
|
||||
_doc.panelSettings = MenuUi.LoadPanelSettings();
|
||||
_doc.sortingOrder = 48; // just below the HUD (50)
|
||||
}
|
||||
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
if (_go != null) Object.Destroy(_go);
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
if (_doc == null) return;
|
||||
var root = _doc.rootVisualElement;
|
||||
if (root == null) return;
|
||||
if (!_built)
|
||||
{
|
||||
root.style.position = Position.Absolute;
|
||||
root.style.left = 0; root.style.right = 0; root.style.top = 0; root.style.bottom = 0;
|
||||
root.pickingMode = PickingMode.Ignore; // never eat world clicks
|
||||
_built = true;
|
||||
}
|
||||
|
||||
var cam = Camera.main;
|
||||
if (!FeelConfig.EnemyMarkerEnabled || cam == null)
|
||||
{
|
||||
HideFrom(0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Local player position (range gate — scopes markers to the current room / region).
|
||||
bool haveLocal = false; float3 localPos = default;
|
||||
foreach (var lt in SystemAPI.Query<RefRO<LocalTransform>>().WithAll<GhostOwnerIsLocal, PlayerTag>())
|
||||
{ haveLocal = true; localPos = lt.ValueRO.Position; break; }
|
||||
|
||||
// Collect living enemies within range (nearest-capped by MaxMarkers).
|
||||
_positions.Clear();
|
||||
float rangeSq = FeelConfig.EnemyMarkerRange * FeelConfig.EnemyMarkerRange;
|
||||
foreach (var lt in SystemAPI.Query<RefRO<LocalTransform>>().WithAll<EnemyTag>().WithNone<Dying, BossState>())
|
||||
{
|
||||
float3 p = lt.ValueRO.Position;
|
||||
if (haveLocal && math.distancesq(p, localPos) > rangeSq) continue;
|
||||
_positions.Add(p);
|
||||
if (_positions.Count >= MaxMarkers) break;
|
||||
}
|
||||
|
||||
float pw = root.layout.width, ph = root.layout.height;
|
||||
if (pw <= 1f || ph <= 1f) { HideFrom(0); return; }
|
||||
|
||||
// First pass: how many are on-screen (drives the pip declutter fade).
|
||||
int onScreen = 0;
|
||||
for (int i = 0; i < _positions.Count; i++)
|
||||
if (!IsOffScreen(cam, _positions[i], pw, ph)) onScreen++;
|
||||
int fadeStart = math.max(1, FeelConfig.EnemyMarkerFadeStartCount);
|
||||
float pipAlpha = math.lerp(1f, math.clamp(FeelConfig.EnemyMarkerMinAlpha, 0f, 1f),
|
||||
math.saturate((onScreen - fadeStart) / (float)fadeStart));
|
||||
|
||||
// Second pass: place each marker.
|
||||
Color baseCol = FeelConfig.EnemyMarkerColor;
|
||||
float size = FeelConfig.EnemyMarkerSize;
|
||||
for (int i = 0; i < _positions.Count; i++)
|
||||
{
|
||||
var lbl = GetMarker(i, root);
|
||||
Place(lbl, cam, _positions[i], pw, ph, baseCol, size, pipAlpha);
|
||||
}
|
||||
HideFrom(_positions.Count);
|
||||
}
|
||||
|
||||
bool IsOffScreen(Camera cam, float3 world, float pw, float ph)
|
||||
{
|
||||
Vector3 sp = cam.WorldToScreenPoint((Vector3)world);
|
||||
bool behind = sp.z < 0f;
|
||||
float px = (sp.x / Mathf.Max(1f, Screen.width)) * pw;
|
||||
float py = (1f - sp.y / Mathf.Max(1f, Screen.height)) * ph;
|
||||
if (behind) { px = pw - px; py = ph - py; }
|
||||
return behind || px < Margin || px > pw - Margin || py < Margin || py > ph - Margin;
|
||||
}
|
||||
|
||||
void Place(Label lbl, Camera cam, float3 world, float pw, float ph, Color baseCol, float size, float pipAlpha)
|
||||
{
|
||||
Vector3 sp = cam.WorldToScreenPoint((Vector3)world);
|
||||
bool behind = sp.z < 0f;
|
||||
float px = (sp.x / Mathf.Max(1f, Screen.width)) * pw;
|
||||
float py = (1f - sp.y / Mathf.Max(1f, Screen.height)) * ph;
|
||||
if (behind) { px = pw - px; py = ph - py; }
|
||||
|
||||
bool off = behind || px < Margin || px > pw - Margin || py < Margin || py > ph - Margin;
|
||||
float cx = pw * 0.5f, cy = ph * 0.5f;
|
||||
float dx = px - cx, dy = py - cy;
|
||||
float len = Mathf.Sqrt(dx * dx + dy * dy);
|
||||
if (len < 0.001f) { dx = 0f; dy = -1f; len = 1f; }
|
||||
float ndx = dx / len, ndy = dy / len;
|
||||
|
||||
lbl.style.fontSize = size;
|
||||
|
||||
if (off)
|
||||
{
|
||||
// Clamp to the margin rectangle along the center->enemy ray, rotate a triangle toward it.
|
||||
float tx = (ndx > 0 ? (pw - Margin - cx) : (Margin - cx)) / (Mathf.Abs(ndx) < 1e-4f ? (ndx < 0 ? -1e-4f : 1e-4f) : ndx);
|
||||
float ty = (ndy > 0 ? (ph - Margin - cy) : (Margin - cy)) / (Mathf.Abs(ndy) < 1e-4f ? (ndy < 0 ? -1e-4f : 1e-4f) : ndy);
|
||||
float tt = Mathf.Min(Mathf.Abs(tx), Mathf.Abs(ty));
|
||||
float ax = cx + ndx * tt, ay = cy + ndy * tt;
|
||||
float angle = Mathf.Atan2(dy, dx) * Mathf.Rad2Deg; // "▶" points +x at 0 deg
|
||||
lbl.text = "▶";
|
||||
lbl.style.left = ax - size * 0.5f;
|
||||
lbl.style.top = ay - size * 0.5f;
|
||||
lbl.style.rotate = new StyleRotate(new Rotate(new Angle(angle)));
|
||||
lbl.style.color = new Color(baseCol.r, baseCol.g, baseCol.b, baseCol.a); // arrows stay full alpha
|
||||
}
|
||||
else
|
||||
{
|
||||
// Overhead pip pointing down at the enemy; fades when the screen is crowded.
|
||||
lbl.text = "▾";
|
||||
lbl.style.left = px - size * 0.5f;
|
||||
lbl.style.top = py - size - 20f; // float above the enemy
|
||||
lbl.style.rotate = new StyleRotate(new Rotate(new Angle(0f)));
|
||||
lbl.style.color = new Color(baseCol.r, baseCol.g, baseCol.b, baseCol.a * pipAlpha);
|
||||
}
|
||||
lbl.style.display = DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
Label GetMarker(int i, VisualElement root)
|
||||
{
|
||||
while (_pool.Count <= i)
|
||||
{
|
||||
var l = new Label("▶");
|
||||
l.style.position = Position.Absolute;
|
||||
l.style.unityFontStyleAndWeight = FontStyle.Bold;
|
||||
l.style.unityTextAlign = TextAnchor.MiddleCenter;
|
||||
l.pickingMode = PickingMode.Ignore;
|
||||
l.style.display = DisplayStyle.None;
|
||||
root.Add(l);
|
||||
_pool.Add(l);
|
||||
}
|
||||
return _pool[i];
|
||||
}
|
||||
|
||||
void HideFrom(int start)
|
||||
{
|
||||
for (int i = start; i < _pool.Count; i++)
|
||||
_pool[i].style.display = DisplayStyle.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user