[ShootingSystem]
LockOnRange = 30.0
LockOnStrength = 0.5
MaxAimAssistAngle = 30.0
SpreadIncrement = 2.0
MaxSpread = 10.0
SpreadDecay = 5.0
DefaultFOV = 60.0
AimFOV = 50.0
AimTransitionSpeed = 10.0
TargetLayer = 9  ; 假设目标层是第9层
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public static class INIParser
{
    public static Dictionary<string, Dictionary<string, string>> Parse(string filePath)
    {
        var config = new Dictionary<string, Dictionary<string, string>>();

        if (!File.Exists(filePath))
        {
            Debug.LogError("INI file not found: " + filePath);
            return config;
        }

        string[] lines = File.ReadAllLines(filePath);
        string currentSection = null;

        foreach (string line in lines)
        {
            string trimmedLine = line.Trim();
            if (string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith(";"))
                continue;

            if (trimmedLine.StartsWith("[") && trimmedLine.EndsWith("]"))
            {
                currentSection = trimmedLine.Trim('[', ']');
                config[currentSection] = new Dictionary<string, string>();
            }
            else if (currentSection != null)
            {
                string[] parts = trimmedLine.Split('=');
                if (parts.Length == 2)
                {
                    string key = parts[0].Trim();
                    string value = parts[1].Trim();
                    config[currentSection][key] = value;
                }
            }
        }

        return config;
    }
}
using UnityEngine;
using System.Collections.Generic;

public class EnhancedShootingSystem : MonoBehaviour
{
    public Transform playerCamera; // 玩家的相机
    public float lockOnRange = 30f; // 锁头范围
    public float lockOnStrength = 0.5f; // 锁头强度
    public float maxAimAssistAngle = 30f; // 辅助瞄准的最大角度
    public float spreadIncrement = 2f; // 每次射击增加的扩散值
    public float maxSpread = 10f; // 扩散上限
    public float spreadDecay = 5f; // 扩散恢复速度
    public float defaultFOV = 60f; // 默认视野范围（平板视野）
    public float aimFOV = 50f; // 瞄准时的视野范围（小身位视野）
    public float aimTransitionSpeed = 10f; // 瞄准视野切换速度
    public LayerMask targetLayer = 1 << 9; // 目标所在的Layer

    private Transform currentTarget; // 当前辅助瞄准目标
    private float spread = 0f; // 当前扩散值
    private bool isAiming = false; // 是否正在瞄准
    private float currentFOV; // 当前视野

    void Start()
    {
        string filePath = Path.Combine(Application.streamingAssetsPath, "Anim.ini");
        LoadConfig(filePath);
        currentFOV = defaultFOV;
        Camera.main.fieldOfView = currentFOV;
    }

    private void LoadConfig(string filePath)
    {
        var config = INIParser.Parse(filePath);
        if (config.ContainsKey("ShootingSystem"))
        {
            var section = config["ShootingSystem"];
            lockOnRange = float.Parse(section["LockOnRange"]);
            lockOnStrength = float.Parse(section["LockOnStrength"]);
            maxAimAssistAngle = float.Parse(section["MaxAimAssistAngle"]);
            spreadIncrement = float.Parse(section["SpreadIncrement"]);
            maxSpread = float.Parse(section["MaxSpread"]);
            spreadDecay = float.Parse(section["SpreadDecay"]);
            defaultFOV = float.Parse(section["DefaultFOV"]);
            aimFOV = float.Parse(section["AimFOV"]);
            aimTransitionSpeed = float.Parse(section["AimTransitionSpeed"]);
            targetLayer = 1 << int.Parse(section["TargetLayer"]); // 将层索引转换为 LayerMask
        }
    }

    void Update()
    {
        DetectTarget();
        AimAtTarget();
        DecaySpread(); // 准心扩散恢复
        AdjustFOV(); // 调整视野
    }

    // 检测目标
    void DetectTarget()
    {
        RaycastHit hit;
        if (Physics.Raycast(playerCamera.position, playerCamera.forward, out hit, lockOnRange, targetLayer))
        {
            float angle = Vector3.Angle(playerCamera.forward, hit.transform.position - playerCamera.position);
            if (angle <= maxAimAssistAngle)
            {
                currentTarget = hit.transform;
            }
        }
        else
        {
            currentTarget = null;
        }
    }

    // 辅助瞄准
    void AimAtTarget()
    {
        if (currentTarget != null)
        {
            Vector3 direction = currentTarget.position - playerCamera.position;
            Quaternion targetRotation = Quaternion.LookRotation(direction);
            playerCamera.rotation = Quaternion.Slerp(playerCamera.rotation, targetRotation, lockOnStrength * Time.deltaTime);
        }
    }

    // 扩散恢复
    void DecaySpread()
    {
        spread = Mathf.Max(spread - spreadDecay * Time.deltaTime, 0f);
    }

    // 射击逻辑
    public void Shoot()
    {
        if (currentTarget != null)
        {
            spread += spreadIncrement;
            spread = Mathf.Min(spread, maxSpread);

            // 模拟子弹扩散
            Vector3 spreadDirection = Random.insideUnitSphere.normalized * spread;
            RaycastHit hit;
            if (Physics.Raycast(playerCamera.position, playerCamera.forward + spreadDirection, out hit, lockOnRange))
            {
                Debug.Log("Hit target with spread: " + spread);
            }
            else
            {
                Debug.Log("Missed target with spread: " + spread);
            }
        }
    }

    // 调整视野
    void AdjustFOV()
    {
        if (isAiming)
        {
            currentFOV = Mathf.Lerp(currentFOV, aimFOV, aimTransitionSpeed * Time.deltaTime);
        }
        else
        {
            currentFOV = Mathf.Lerp(currentFOV, defaultFOV, aimTransitionSpeed * Time.deltaTime);
        }
        Camera.main.fieldOfView = currentFOV;
    }

    // 切换瞄准状态
    public void ToggleAim()
    {
        isAiming = !isAiming;
    }
}
