using System.Collections.Generic;
using BepuPhysics;
using BepuPhysics.Collidables;
using BepuUtilities;
using BepuUtilities.Memory;
using Plugins.JNGame.BepuPhysics.Components;
using Plugins.JNGame.BepuPhysics.Utilities;
using UnityEngine;
using Vector3 = System.Numerics.Vector3;

namespace Plugins.JNGame.BepuPhysics
{
    public class JNBepuPhysics
    {

        public static Dictionary<int,JNBepuPhysics> Physics = new();
        
        private UnityEngine.Vector3 _gravity = new (0,10,0);
        public Simulation Simulation { get; protected set; }
        public BufferPool BufferPool { get; private set; }

        public List<BPhysicsBase> Shapes = new();

        
        public JNBepuPhysics(int index = 0)
        {
            Debug.Log($"初始化物理引擎");
            Physics[index] = this;
            BufferPool = new BufferPool();
            Simulation = Simulation.Create(BufferPool,new JNNarrowPhaseCallbacks(),new JNPoseIntegratorCallbacks(new Vector3(_gravity.x, -_gravity.y, _gravity.z)),new PositionFirstTimestepper());
        }
        
        
        
        //更新
        public void OnUpdate(float dt)
        {
            Simulation.Timestep(dt);
            Shapes.ForEach(shapes =>
            {
                shapes.OnPhysicsUpdate(dt);
            });
        }
        
    }
}