Add visibility tracker to quantum planets

This commit is contained in:
Nick 2022-05-10 23:49:48 -04:00
parent 923cd149fe
commit c648fbe428
2 changed files with 30 additions and 5 deletions

View File

@ -18,6 +18,7 @@ namespace NewHorizons.Components
public class QuantumPlanet : QuantumObject
{
public List<State> states = new List<State>();
public State groundState;
private int _currentIndex;
private NHAstroObject _astroObject;
@ -33,6 +34,8 @@ namespace NewHorizons.Components
_detector = GetComponentInChildren<ConstantForceDetector>();
_alignment = GetComponent<AlignWithTargetBody>();
_rb = GetComponent<OWRigidbody>();
GlobalMessenger.AddListener("PlayerBlink", new Callback(OnPlayerBlink));
}
public override void Start()
@ -62,8 +65,8 @@ namespace NewHorizons.Components
var oldState = states[_currentIndex];
var newState = states[newIndex];
if (newState.sector != null) SetNewSector(oldState, newState);
if(newState.orbit != null) SetNewOrbit(newState);
if (newState.sector != null && newState.sector != oldState.sector) SetNewSector(oldState, newState);
if (newState.orbit != null && newState.orbit != oldState.orbit) SetNewOrbit(newState);
_currentIndex = newIndex;
@ -94,6 +97,19 @@ namespace NewHorizons.Components
_rb.SetVelocity(currentOrbit.GetOrbitalParameters(primaryGravity, secondaryGravity).InitialVelocity);
}
private void OnPlayerBlink()
{
if (base.IsVisible())
{
base.Collapse(true);
}
}
public override bool IsPlayerEntangled()
{
return states[_currentIndex].sector.ContainsAnyOccupants(DynamicOccupant.Player);
}
public class State
{
public Sector sector { get; set; }

View File

@ -166,7 +166,16 @@ namespace NewHorizons.Handlers
var rootSector = quantumPlanet.GetComponentInChildren<Sector>();
var groundOrbit = _dict[ao].Config.Orbit;
quantumPlanet.states.Add(new QuantumPlanet.State(rootSector, groundOrbit));
quantumPlanet.groundState = new QuantumPlanet.State(rootSector, groundOrbit);
quantumPlanet.states.Add(quantumPlanet.groundState);
var visibilityTracker = new GameObject("VisibilityTracker_Sphere");
visibilityTracker.transform.parent = existingPlanet.transform;
visibilityTracker.transform.localPosition = Vector3.zero;
var sphere = visibilityTracker.AddComponent<SphereShape>();
sphere.radius = GetSphereOfInfluence(_dict[ao]);
var tracker = visibilityTracker.AddComponent<ShapeVisibilityTracker>();
quantumPlanet._visibilityTrackers = new VisibilityTracker[] { tracker };
}
var rb = existingPlanet.GetComponent<OWRigidbody>();
@ -177,10 +186,10 @@ namespace NewHorizons.Handlers
SharedGenerateBody(body, existingPlanet, sector, rb);
// If nothing was generated then forget the sector
if (sector.transform.childCount == 0) sector = null;
if (sector.transform.childCount == 0) sector = quantumPlanet.groundState.sector;
// If semimajor axis is 0 then forget the orbit
var orbit = body.Config.Orbit.SemiMajorAxis == 0 ? null : body.Config.Orbit;
var orbit = body.Config.Orbit.SemiMajorAxis == 0 ? quantumPlanet.groundState.orbit : body.Config.Orbit;
quantumPlanet.states.Add(new QuantumPlanet.State(sector, orbit));
}