This commit is contained in:
2026-07-07 20:51:18 -07:00
parent d2203c8aa1
commit cc2e7ad95e
20 changed files with 794 additions and 35 deletions
@@ -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;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5b754936521bd1443b6daa41a9ae0fc8
@@ -161,6 +161,20 @@ namespace ProjectM.Client
public static float StrikeBeepMaxDistSq;
// ---- Enemy visibility markers (EnemyMarkerSystem: off-screen edge arrows + on-screen overhead pips) ----
/// <summary>Master gate for the enemy visibility markers.</summary>
public static bool EnemyMarkerEnabled;
/// <summary>Only mark living enemies within this world distance of the local player (scopes to the current room; avoids cross-region arrow spam).</summary>
public static float EnemyMarkerRange;
/// <summary>On-screen enemy count above which the overhead pips fade toward EnemyMarkerMinAlpha (declutter a swarm; off-screen arrows stay full).</summary>
public static int EnemyMarkerFadeStartCount;
/// <summary>Faded pip alpha once the on-screen count is well past EnemyMarkerFadeStartCount.</summary>
public static float EnemyMarkerMinAlpha;
/// <summary>Marker tint (pip + off-screen arrow).</summary>
public static Color EnemyMarkerColor;
/// <summary>Marker glyph font size (px).</summary>
public static float EnemyMarkerSize;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
public static void ResetDefaults()
{
@@ -240,6 +254,13 @@ namespace ProjectM.Client
StrikeBeepLeadTicks = 8;
StrikeBeepMaxDistSq = 225f; // 15 m
// Enemy visibility markers (EnemyMarkerSystem)
EnemyMarkerEnabled = true;
EnemyMarkerRange = 140f;
EnemyMarkerFadeStartCount = 6;
EnemyMarkerMinAlpha = 0.28f;
EnemyMarkerColor = new Color(1f, 0.86f, 0.3f, 1f); // warm amber, reads over the cool world
EnemyMarkerSize = 24f;
}
}
}
@@ -0,0 +1,103 @@
using System.Collections.Generic;
using ProjectM.Simulation;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace ProjectM.Client
{
/// <summary>
/// Client-only harvest BODY feedback for resource nodes + Blight clutter — the "you're mining this down" cue that
/// complements <see cref="WorldFeedbackSystem"/>'s chip particles/SFX. Observe-only presentation (SystemBase, main
/// thread, no Burst) in <see cref="PresentationSystemGroup"/>: it edge-detects each node/clutter ghost's replicated
/// <c>Remaining</c> and drives a CLIENT-OWNED <see cref="PostTransformMatrix"/> scale on the ghost root so the node
/// (1) SHRINKS as Remaining falls toward 0 and (2) POPS briefly larger on each hit — a clear per-hit reaction plus
/// a running "how much is left" read. PostTransformMatrix composes on top of the replicated LocalTransform and is
/// NOT a ghost field, so this is pure client cosmetics: no wire change, no sim mutation, and it never destroys a
/// ghost (GhostDespawnSystem owns despawn; the shatter burst is WorldFeedbackSystem's prune path). Uses a scale
/// reaction rather than a <c>_BaseColor</c> flash because node materials (unlike the white-based enemy/player
/// bodies) aren't guaranteed white, so a base-color override could clobber their rest tint. Knobs in
/// <see cref="WorldFeelConfig"/> (<c>NodeMinScale</c> / <c>NodeHitPopScale</c> / <c>NodePopDurationSec</c>).
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
[UpdateInGroup(typeof(PresentationSystemGroup))]
public partial class NodeFeedbackSystem : SystemBase
{
class Entry
{
public int LastRemaining;
public int MaxRemaining; // client "full" reference = the largest Remaining ever seen for this node
public float Pop; // 1 on a fresh hit, decays to 0 (the per-hit scale-up)
}
readonly Dictionary<Entity, Entry> _tracked = new();
readonly HashSet<Entity> _seen = new();
readonly List<Entity> _stale = new();
protected override void OnUpdate()
{
if (!WorldFeelConfig.Enabled) { RestoreAll(); return; }
float dt = SystemAPI.Time.DeltaTime;
EntityManager.CompleteDependencyBeforeRO<ResourceNode>();
EntityManager.CompleteDependencyBeforeRO<BlightClutter>();
_seen.Clear();
var ecb = new EntityCommandBuffer(Unity.Collections.Allocator.Temp);
float popDecay = dt / math.max(0.01f, WorldFeelConfig.NodePopDurationSec);
float minScale = math.clamp(WorldFeelConfig.NodeMinScale, 0.05f, 1f);
float popAmt = math.max(0f, WorldFeelConfig.NodeHitPopScale);
foreach (var (node, entity) in SystemAPI.Query<RefRO<ResourceNode>>().WithEntityAccess())
Drive(entity, node.ValueRO.Remaining, popDecay, minScale, popAmt, ecb);
foreach (var (clut, entity) in SystemAPI.Query<RefRO<BlightClutter>>().WithEntityAccess())
Drive(entity, clut.ValueRO.Remaining, popDecay, minScale, popAmt, ecb);
ecb.Playback(EntityManager);
ecb.Dispose();
// Prune despawned (depleted/shattered) nodes — their PostTransformMatrix dies with the ghost.
if (_tracked.Count != _seen.Count)
{
_stale.Clear();
foreach (var kv in _tracked) if (!_seen.Contains(kv.Key)) _stale.Add(kv.Key);
for (int i = 0; i < _stale.Count; i++) _tracked.Remove(_stale[i]);
}
}
void Drive(Entity e, int remaining, float popDecay, float minScale, float popAmt, EntityCommandBuffer ecb)
{
_seen.Add(e);
if (!_tracked.TryGetValue(e, out var entry))
{
entry = new Entry { LastRemaining = remaining, MaxRemaining = math.max(1, remaining), Pop = 0f };
_tracked[e] = entry;
if (!EntityManager.HasComponent<PostTransformMatrix>(e))
ecb.AddComponent(e, new PostTransformMatrix { Value = float4x4.Scale(1f) }); // present next frame
}
else
{
if (remaining < entry.LastRemaining) entry.Pop = 1f; // a hit landed
entry.LastRemaining = remaining;
if (remaining > entry.MaxRemaining) entry.MaxRemaining = remaining;
}
entry.Pop = math.max(0f, entry.Pop - popDecay);
// Progressive shrink toward NodeMinScale as Remaining -> 0, plus a brief per-hit pop.
float frac = math.saturate(remaining / (float)math.max(1, entry.MaxRemaining));
float scale = math.lerp(minScale, 1f, frac) * (1f + entry.Pop * popAmt);
if (EntityManager.HasComponent<PostTransformMatrix>(e))
EntityManager.SetComponentData(e, new PostTransformMatrix { Value = float4x4.Scale(scale) });
}
// Toggled off (WorldFeelConfig.Enabled=false) -> restore every tracked node to full size and stop tracking.
void RestoreAll()
{
foreach (var kv in _tracked)
if (EntityManager.Exists(kv.Key) && EntityManager.HasComponent<PostTransformMatrix>(kv.Key))
EntityManager.SetComponentData(kv.Key, new PostTransformMatrix { Value = float4x4.Scale(1f) });
_tracked.Clear();
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1fca48a6fde1ed94b9f2445b4d9c73d3
@@ -81,7 +81,7 @@ namespace ProjectM.Client
SystemAPI.Query<RefRO<ResourceNode>, RefRO<LocalTransform>>().WithEntityAccess())
{
_seen.Add(e);
Observe(e, node.ValueRO.Remaining, xf.ValueRO.Position, false, TintForResource(node.ValueRO.ResourceId));
Observe(e, node.ValueRO.Remaining, xf.ValueRO.Position, false, TintForResource(node.ValueRO.ResourceId), haveLocal && math.distancesq(xf.ValueRO.Position, localPos) <= WorldFeelConfig.ProximityRange * WorldFeelConfig.ProximityRange);
}
// Blight clutter — chip on damage.
@@ -89,7 +89,7 @@ namespace ProjectM.Client
SystemAPI.Query<RefRO<BlightClutter>, RefRO<LocalTransform>>().WithEntityAccess())
{
_seen.Add(e);
Observe(e, clutter.ValueRO.Remaining, xf.ValueRO.Position, true, WorldFeelConfig.WildTint);
Observe(e, clutter.ValueRO.Remaining, xf.ValueRO.Position, true, WorldFeelConfig.WildTint, haveLocal && math.distancesq(xf.ValueRO.Position, localPos) <= WorldFeelConfig.ProximityRange * WorldFeelConfig.ProximityRange);
}
// Prune: a despawn = the server destroyed it (node depleted / clutter shattered). Gate on proximity so
@@ -119,12 +119,17 @@ namespace ProjectM.Client
}
}
void Observe(Entity e, int remaining, float3 pos, bool isClutter, Color tint)
void Observe(Entity e, int remaining, float3 pos, bool isClutter, Color tint, bool nearLocal)
{
if (_cache.TryGetValue(e, out var prev) && remaining < prev.Remaining)
{
EmitTinted(_chipFx, (Vector3)pos + Vector3.up * 0.6f, WorldFeelConfig.ChipBurstCount, tint);
PlayClip(_chipClip, (Vector3)pos, WorldFeelConfig.ChipSfxVolume);
if (nearLocal) // tiny per-hit camera impact, only for harvesting near the local player (not a teammate's far node)
{
if (WorldFeelConfig.ChipFovKick > 0f) PrototypeCameraRig.PunchFov(WorldFeelConfig.ChipFovKick, 90f);
if (WorldFeelConfig.ChipShake > 0f) PrototypeCameraRig.AddShake(WorldFeelConfig.ChipShake);
}
}
_cache[e] = new Cache { Remaining = remaining, Pos = pos, IsClutter = isClutter, Tint = tint };
}
@@ -42,13 +42,25 @@ namespace ProjectM.Client
/// <summary>Tint for Biomass-node chips (HDR sickly green).</summary>
public static Color BiomassTint;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
public static void ResetDefaults()
// ---- Node harvest BODY feedback (NodeFeedbackSystem: client-only PostTransformMatrix scale). ----
/// <summary>Smallest a node/clutter shrinks to just before it depletes (fraction of its full size).</summary>
public static float NodeMinScale;
/// <summary>Brief extra scale-up on each harvest hit (the per-hit "reaction" pop); decays over NodePopDurationSec.</summary>
public static float NodeHitPopScale;
/// <summary>Seconds for the per-hit scale-pop to decay back.</summary>
public static float NodePopDurationSec;
/// <summary>Tiny camera FOV kick per harvest hit near the player (impact); 0 = off.</summary>
public static float ChipFovKick;
/// <summary>Tiny camera shake per harvest hit near the player.</summary>
public static float ChipShake;
public static void ResetDefaults()
{
Enabled = true;
ChipBurstCount = 6;
ClearBurstCount = 18;
ChipSfxVolume = 0.30f;
ChipBurstCount = 12; // beefed from 6 -> a clearly visible chip spray per harvest hit
ClearBurstCount = 22;
ChipSfxVolume = 0.42f; // beefed from 0.30 -> a punchier "tink"
ClearSfxVolume = 0.55f;
ClearFovKick = 0.8f;
ClearShake = 0.12f;
@@ -56,6 +68,11 @@ namespace ProjectM.Client
WildTint = new Color(3.0f, 1.1f, 0.25f);
OreTint = new Color(2.6f, 1.9f, 0.7f);
BiomassTint = new Color(0.9f, 2.4f, 0.8f);
NodeMinScale = 0.35f; // node shrinks to ~1/3 size as it nears depletion
NodeHitPopScale = 0.16f; // brief +16% pop on each hit
NodePopDurationSec = 0.16f;
ChipFovKick = 0.35f; // subtle per-hit camera impact near the player
ChipShake = 0.05f;
}
}
}