using OpenTK.Mathematics;

namespace DoomDeathmatch.Component.Util.Collision;

public class ColliderForceFieldComponent : Engine.Scene.Component.Component
{
  private AABBColliderComponent _collider = null!;

  public override void Awake()
  {
    _collider = GameObject.GetComponent<AABBColliderComponent>()!;
    _collider.OnCollision += OnCollision;
  }

  private void OnCollision(AABBColliderComponent parCollider)
  {
    var rigidbody = parCollider.GameObject.GetComponent<RigidbodyComponent>();
    if (rigidbody == null)
      return;

    var normal = _collider.Collider.GetCollisionNormal(parCollider.Collider);
    var speedAlongNormal = Vector3.Dot(rigidbody.Velocity, normal);
    if (speedAlongNormal >= 0)
      return;

    rigidbody.AddVelocity(-normal * speedAlongNormal);
  }
}