Skip to content

Commit

Permalink
Fix issues caused by AstroObjects getting disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
SkutteOleg committed Feb 8, 2023
1 parent 4ddeb2e commit e07999c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
19 changes: 16 additions & 3 deletions TrajectoryPrediction/AstroObjectTrajectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,25 @@ private void Start()
_astroObject = GetComponent<AstroObject>();
_body = _astroObject.GetOWRigidbody();
_gravityVolume = _astroObject.GetGravityVolume();

if (_gravityVolume)
_visualizer = gameObject.AddComponent<TrajectoryVisualizer>();

Enable();
}

private void Enable()
{
if (!_astroObject)
return;

TrajectoryPrediction.AstroObjectToTrajectoryMap[_astroObject] = this;
if (_gravityVolume)
{
if (_gravityVolume.GetOWTriggerVolume().GetShape())
_triggerRadius = _gravityVolume.GetOWTriggerVolume().GetShape().localBounds.radius;
if (_gravityVolume.GetOWTriggerVolume().GetOWCollider())
_triggerRadius = ((SphereCollider)_gravityVolume.GetOWTriggerVolume().GetOWCollider().GetCollider()).radius;
_triggerRadius = ((SphereCollider) _gravityVolume.GetOWTriggerVolume().GetOWCollider().GetCollider()).radius;

TrajectoryPrediction.GravityVolumeToTrajectoryMap[_gravityVolume] = this;
TrajectoryPrediction.AddTrajectory(this);
Expand All @@ -46,7 +54,12 @@ private void Start()
TrajectoryPrediction.OnEndFrame += EndFrame;
}

private void OnDestroy()
private void OnEnable()
{
Enable();
}

private void OnDisable()
{
TrajectoryPrediction.AstroObjectToTrajectoryMap.Remove(_astroObject);
if (_gravityVolume)
Expand Down
16 changes: 15 additions & 1 deletion TrajectoryPrediction/MapMarkerTrajectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ private void Start()

_forceDetector = _body.GetAttachedForceDetector();
_visualizer = gameObject.AddComponent<TrajectoryVisualizer>();

Enable();
}

private void Enable()
{
if (!_marker)
return;

ApplyConfig();

TrajectoryPrediction.OnConfigUpdate += ApplyConfig;
Expand All @@ -44,7 +53,12 @@ private void Start()
}
}

private void OnDestroy()
private void OnEnable()
{
Enable();
}

private void OnDisable()
{
TrajectoryPrediction.OnConfigUpdate -= ApplyConfig;
TrajectoryPrediction.OnBeginFrame -= BeginFrame;
Expand Down
26 changes: 20 additions & 6 deletions TrajectoryPrediction/TrajectoryVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ private void Start()
material.SetTexture(MainTex, Resources.FindObjectsOfTypeAll<Texture2D>().First(texture => texture.name == "Effects_SPA_OrbitLine_Dotted_d"));
_lineRenderer.material = material;
_lineRenderer.textureMode = LineTextureMode.RepeatPerSegment;

Enable();
}

private void Enable()
{
if (_trajectory == null)
return;

TrajectoryPrediction.AddVisualizer(this);
UpdateVisibility();
UpdateColor();
Expand All @@ -33,7 +42,12 @@ private void Start()
TrajectoryPrediction.OnBeginFrame += BeginFrame;
}

private void OnDestroy()
private void OnEnable()
{
Enable();
}

private void OnDisable()
{
TrajectoryPrediction.RemoveVisualizer(this);
GlobalMessenger.RemoveListener("EnterMapView", OnEnterMapView);
Expand All @@ -50,9 +64,9 @@ public void SetVisibility(bool value)

private void UpdateVisibility()
{
if (!_lineRenderer)
if (!_lineRenderer)
return;

_lineRenderer.enabled = _visible;
}

Expand All @@ -64,9 +78,9 @@ public void SetColor(Color color)

private void UpdateColor()
{
if (!_lineRenderer)
if (!_lineRenderer)
return;

_lineRenderer.startColor = _color;
_lineRenderer.endColor = new Color(_color.r, _color.g, _color.b, 0);
}
Expand Down Expand Up @@ -110,7 +124,7 @@ private void Update()

for (var i = 0; i < _lineRenderer.positionCount; i++)
{
int step = TrajectoryPrediction.HighPrecisionMode ? Mathf.Min((int)((i + _timeSinceUpdate) / Time.fixedDeltaTime), _trajectory.TrajectoryCache.Length - 1) : i;
var step = TrajectoryPrediction.HighPrecisionMode ? Mathf.Min((int) ((i + _timeSinceUpdate) / Time.fixedDeltaTime), _trajectory.TrajectoryCache.Length - 1) : i;

if (_trajectory.TrajectoryCache[step] == Vector3.zero)
{
Expand Down
8 changes: 2 additions & 6 deletions TrajectoryPrediction/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
"filename": "TrajectoryPrediction.dll",
"author": "Oleg Skutte",
"name": "Trajectory Prediction",
"warning": {
"title": "Incremental GC is recommended",
"body": "Trajectory Prediction uses a lot of RAM and may cause lag spikes if incremental GC is disabled.\nYou can enable incremental GC in the mod manager settings."
},
"uniqueName": "OlegSkutte.TrajectoryPrediction",
"version": "0.6.4",
"owmlVersion": "2.7.5",
"version": "0.6.5",
"owmlVersion": "2.9.0",
"dependencies": []
}

0 comments on commit e07999c

Please sign in to comment.