Refactored Ghost Entities
Game Context was creating an empty OrbitalEntity for every planet; this was an ugly hack so I refactored it, as well as refactoring some object references to GUIDs as they should be
This commit is contained in:
parent
35fb4381ed
commit
4f29bf24d0
13 changed files with 199 additions and 191 deletions
|
|
@ -8,45 +8,15 @@ using float2 = Unity.Mathematics.float2;
|
|||
public class OrbitalEntity : Entity
|
||||
{
|
||||
private Guid _orbitData;
|
||||
private static Dictionary<Guid, float2> _orbitPositions = new Dictionary<Guid, float2>();
|
||||
|
||||
public OrbitalEntity(GameContext context, Gear hull, IEnumerable<Gear> items, IEnumerable<ItemInstance> cargo, Guid orbit) : base(context, hull, items, cargo)
|
||||
{
|
||||
_orbitData = orbit;
|
||||
}
|
||||
|
||||
public static void ClearOrbits()
|
||||
{
|
||||
_orbitPositions.Clear();
|
||||
}
|
||||
|
||||
// Determine orbital position recursively, caching parent positions to avoid repeated calculations
|
||||
private float2 GetOrbitPosition(Guid orbit)
|
||||
{
|
||||
// Root orbit is fixed at center
|
||||
if(orbit==Guid.Empty)
|
||||
return float2.zero;
|
||||
|
||||
if (!_orbitPositions.ContainsKey(orbit))
|
||||
{
|
||||
var orbitData = Context.Cache.Get<OrbitData>(orbit);
|
||||
_orbitPositions[orbit] = GetOrbitPosition(orbitData.Parent) + (orbitData.Period < .01f ? float2.zero :
|
||||
OrbitData.Evaluate((float) (Context.Time / -orbitData.Period * Context.GlobalData.OrbitSpeedMultiplier + orbitData.Phase)) *
|
||||
orbitData.Distance);
|
||||
}
|
||||
|
||||
var position = _orbitPositions[orbit];
|
||||
if (float.IsNaN(position.x))
|
||||
{
|
||||
Context.Log("Orbit position is NaN, something went very wrong!");
|
||||
return float2.zero;
|
||||
}
|
||||
return _orbitPositions[orbit];
|
||||
}
|
||||
|
||||
public override void Update(float delta)
|
||||
{
|
||||
var newPosition = GetOrbitPosition(_orbitData);
|
||||
var newPosition = Context.GetOrbitPosition(_orbitData);
|
||||
Velocity = (newPosition - Position) / delta;
|
||||
Position = newPosition;
|
||||
base.Update(delta);
|
||||
|
|
|
|||
Loading…
Reference in a new issue