Skip to content

Commit

Permalink
Fix glitches which made the range sphere or the lines appear with som…
Browse files Browse the repository at this point in the history
…e delay
  • Loading branch information
rockfactory committed Feb 11, 2024
1 parent e30580f commit 2f31479
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
23 changes: 15 additions & 8 deletions src/CommNext/Rendering/Behaviors/MapConnectionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ public void Configure(Map3DFocusItem source, Map3DFocusItem target,
_lineRenderer.SetPropertyBlock(_materialPropertyBlock);

_isConnected = true;

UpdateLinePositions();
}

private void UpdateLinePositions()
{
var positions = new Vector3[10];
for (var i = 0; i < 10; i++)
{
var t = i / 9f;
positions[i] = Vector3.Lerp(SourceItem!.transform.position, TargetItem!.transform.position, t);
}

_lineRenderer.SetPositions(positions);
}

public void Update()
Expand All @@ -87,14 +101,7 @@ public void Update()
return;
}

var positions = new Vector3[10];
for (var i = 0; i < 10; i++)
{
var t = i / 9f;
positions[i] = Vector3.Lerp(SourceItem.transform.position, TargetItem.transform.position, t);
}

_lineRenderer.SetPositions(positions);
UpdateLinePositions();
}

public void OnDestroy()
Expand Down
3 changes: 2 additions & 1 deletion src/CommNext/Rendering/Behaviors/MapRulerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void Track(Map3DFocusItem target, NetworkNode networkNode, ConnectionGrap
sphereComponent.Configure(connectionNode.MaxRange,
networkNode.IsRelay ? null : Color.gray);

// Simple relay placeholder
#if SHOW_RELAY_PLACEHOLDER
// Simple relay placeholder
if (networkNode.IsRelay)
{
var placeholderObject = Instantiate(ConnectionsRenderer.RulerSpherePrefab, gameObject.transform);
Expand All @@ -48,6 +48,7 @@ public void Track(Map3DFocusItem target, NetworkNode networkNode, ConnectionGrap
}
#endif

transform.position = target.transform.position;
_isTracking = true;
}

Expand Down
10 changes: 8 additions & 2 deletions src/CommNext/Rendering/Behaviors/MapSphereRulerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void Configure(double range, Color? color)
}

_range = range;
ScaleByRange();
}

public void SetColor(Color color)
Expand All @@ -40,9 +41,14 @@ public void SetColor(Color color)
_meshRenderer.SetPropertyBlock(_propertyBlock); // TODO Needed?
}

private void Update()
private void ScaleByRange()
{
var radius = (float)(_range / ConnectionsRenderer.Instance.GetMap3dScaleInv());
var radius = (float)(_range / ConnectionsRenderer.GetMap3dScaleInv());
transform.localScale = new Vector3(radius, radius, radius);
}

private void Update()
{
ScaleByRange();
}
}
41 changes: 25 additions & 16 deletions src/CommNext/Rendering/ConnectionsRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ private void Start()

private void ToggleUpdateTaskIfNeeded()
{
// We want to trigger them right away so that UI is updated
// right after the UI click.
if (_isConnectionsEnabled || _isRulersEnabled) UpdateRenderings();

switch (_isConnectionsEnabled || _isRulersEnabled)
{
case true when _updateTask == null:
Expand Down Expand Up @@ -112,26 +116,31 @@ public void ClearRulers()
{
while (true)
{
// TODO Start/Stop tasks based on the state of the game
if (MessageListener.IsInMapView &&
CommunicationsManager.Instance.TryGetConnectionGraphNodesAndIndexes(out var nodes,
out var prevIndexes) &&
nodes != null)
try
{
if (_isConnectionsEnabled) UpdateConnections(nodes!, prevIndexes);
if (_isRulersEnabled) UpdateRulers(nodes!, prevIndexes);
}
catch (Exception e)
{
Logger.LogError("Error updating connections: " + e);
}

UpdateRenderings();
yield return new WaitForSeconds(0.5f);
}
// ReSharper disable once IteratorNeverReturns
}

private void UpdateRenderings()
{
if (!MessageListener.IsInMapView ||
!CommunicationsManager.Instance.TryGetConnectionGraphNodesAndIndexes(
out var nodes,
out var prevIndexes) ||
nodes == null) return;

try
{
if (_isConnectionsEnabled) UpdateConnections(nodes!, prevIndexes);
if (_isRulersEnabled) UpdateRulers(nodes!, prevIndexes);
}
catch (Exception e)
{
Logger.LogError("Error updating connections: " + e);
}
}

private void UpdateConnections(List<ConnectionGraphNode> nodes, int[] prevIndexes)
{
// TODO Add some events-based logic.
Expand Down Expand Up @@ -233,7 +242,7 @@ private void UpdateRulers(List<ConnectionGraphNode> nodes, int[] prevIndexes)
}
}

public double GetMap3dScaleInv()
public static double GetMap3dScaleInv()
{
return _mapCore.map3D.GetSpaceProvider().Map3DScaleInv;
}
Expand Down

0 comments on commit 2f31479

Please sign in to comment.