perf: Collider2DBase NeedsRecalculation Field
This commit is contained in:
parent
ab9181fe3f
commit
0ba8927858
|
@ -12,16 +12,25 @@ public abstract class Collider2DBehaviourBase : BehaviourOverride, ICollider2D
|
||||||
public Action<ICollider2D, ICollider2D>? OnCollisionResolved { get; set; } = null;
|
public Action<ICollider2D, ICollider2D>? OnCollisionResolved { get; set; } = null;
|
||||||
|
|
||||||
|
|
||||||
|
protected bool NeedsRecalculation { get; private set; } = true;
|
||||||
protected IRigidBody2D? _rigidBody2D = null;
|
protected IRigidBody2D? _rigidBody2D = null;
|
||||||
|
|
||||||
|
|
||||||
public IRigidBody2D? RigidBody2D => _rigidBody2D;
|
public IRigidBody2D? RigidBody2D => _rigidBody2D;
|
||||||
|
|
||||||
ITransform IAssignableTransform.Transform => Transform;
|
ITransform IAssignableTransform.Transform => Transform;
|
||||||
Action<IAssignableTransform>? IAssignableTransform.OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; }
|
Action<IAssignableTransform>? IAssignableTransform.OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; }
|
||||||
bool IAssignableTransform.Assign(ITransform transform) => GameObject.Assign(transform);
|
bool IAssignableTransform.Assign(ITransform transform) => GameObject.Assign(transform);
|
||||||
|
|
||||||
public abstract void Recalculate();
|
public void Recalculate()
|
||||||
|
{
|
||||||
|
if (!NeedsRecalculation)
|
||||||
|
return;
|
||||||
|
|
||||||
|
CalculateCollider();
|
||||||
|
NeedsRecalculation = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void CalculateCollider();
|
||||||
|
|
||||||
protected override void OnInitialize()
|
protected override void OnInitialize()
|
||||||
{
|
{
|
||||||
|
@ -29,6 +38,8 @@ public abstract class Collider2DBehaviourBase : BehaviourOverride, ICollider2D
|
||||||
|
|
||||||
BehaviourController.OnBehaviourAdded += OnBehaviourAddedToController;
|
BehaviourController.OnBehaviourAdded += OnBehaviourAddedToController;
|
||||||
BehaviourController.OnBehaviourRemoved += OnBehaviourRemovedFromController;
|
BehaviourController.OnBehaviourRemoved += OnBehaviourRemovedFromController;
|
||||||
|
|
||||||
|
Transform.OnPositionChanged += OnPositionChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnBehaviourAddedToController(IBehaviourController _, IBehaviour behaviour)
|
private void OnBehaviourAddedToController(IBehaviourController _, IBehaviour behaviour)
|
||||||
|
@ -43,9 +54,13 @@ public abstract class Collider2DBehaviourBase : BehaviourOverride, ICollider2D
|
||||||
_rigidBody2D = null;
|
_rigidBody2D = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnPositionChanged(ITransform transform) => NeedsRecalculation = true;
|
||||||
|
|
||||||
protected override void OnFinalize()
|
protected override void OnFinalize()
|
||||||
{
|
{
|
||||||
BehaviourController.OnBehaviourAdded -= OnBehaviourAddedToController;
|
BehaviourController.OnBehaviourAdded -= OnBehaviourAddedToController;
|
||||||
BehaviourController.OnBehaviourRemoved -= OnBehaviourRemovedFromController; ;
|
BehaviourController.OnBehaviourRemoved -= OnBehaviourRemovedFromController;
|
||||||
|
|
||||||
|
Transform.OnPositionChanged -= OnPositionChanged;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,5 +10,5 @@ public class Collider2DCircleBehaviour : Collider2DBehaviourBase, ICircleCollide
|
||||||
public Circle CircleLocal { get; set; } = new(Vector2D.Zero, 1f);
|
public Circle CircleLocal { get; set; } = new(Vector2D.Zero, 1f);
|
||||||
|
|
||||||
|
|
||||||
public override void Recalculate() => CircleWorld = Transform.TransformCircle(CircleLocal);
|
public override void CalculateCollider() => CircleWorld = Transform.TransformCircle(CircleLocal);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,5 +10,5 @@ public class Collider2DShapeBehaviour : Collider2DBehaviourBase, IShapeCollider2
|
||||||
|
|
||||||
protected Shape _shapeWorld = new([new(1f, 1f), new(-1f, 1f), new(-1f, -1f), new(1f, -1f)]);
|
protected Shape _shapeWorld = new([new(1f, 1f), new(-1f, 1f), new(-1f, -1f), new(1f, -1f)]);
|
||||||
|
|
||||||
public override void Recalculate() => Transform.TransformShape(ShapeLocal, ref _shapeWorld);
|
public override void CalculateCollider() => Transform.TransformShape(ShapeLocal, ref _shapeWorld);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue