
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using UnityEngine;

public class PhysicsUtils : MonoBehaviour
{
    public string exportName;
    public GameObject startPoint;
    public GameObject endPoint;

    /// <summary>
    /// 角度间隔
    /// </summary>
    public float angleInterval = 10.0f;
    /// <summary>
    /// 距离间隔
    /// </summary>
    public float distanceInterval = 0.1f;

    //private int[][] list;
    // Start is called before the first frame update
    void Start()
    {
        StartRaycast();
    }

    void StartRaycast()
    {
        float dis = Vector3.Distance(startPoint.transform.position, endPoint.transform.position);
        int disCount = (int)(dis / distanceInterval);
        int angleCount = (int)(360.0f / angleInterval);

        Debug.Log("开始导出:"+angleCount+"_"+ disCount);
        //list = new int[angleCount][];

        Vector3 pos = new Vector3();
        Vector3 direction = new Vector3();
        Quaternion quat = new Quaternion();
        pos.Set(startPoint.transform.position.x, startPoint.transform.position.y, startPoint.transform.position.z);

        string data = "{";
        //物理块的大小
        data += "\"angleInterval\":" + this.angleInterval.ToString();
        data += ",\"tileHeight\":" + this.distanceInterval.ToString();
        //物理数据
        data += ",\"data\":\r\n[";
        int rayState = 0;
        for (int zIndex = 0; zIndex < disCount; zIndex++)
        {
            //list[xIndex] = new int[disCount];
            if (zIndex > 0)
            {
                data += ","+"\r\n"+"[";
            }
            else
            {
                data += "\r\n"+"[";
            }
            for (int xIndex = 0; xIndex < angleCount; xIndex++)
            {
                //重置方向
                direction.Set(0, -1, 0);
                pos.z = startPoint.transform.position.z + zIndex * distanceInterval;
                //旋转向量
                direction = Quaternion.Euler(0, 0, xIndex * angleInterval) * direction;
                if (Physics.Raycast(pos, direction))
                {
                    rayState = 0;
                }
                else
                {
                    rayState = 1;
                }
                if (xIndex > 0)
                {
                    data += ",";
                }
                //list[xIndex][zIndex] = rayState;
                data += rayState.ToString();
            }
            data += "]";
        }
        data += "]";
        data += "}";
        WriteFile("D:/cocos/StarRacing/StarRacingClient/assets/res/mainPackage/configs/" + exportName+".json", data);
        Debug.Log("导出完成！");
    }

    /// <summary>
    /// path需要包含文件的后缀名
    /// </summary>
    /// <param name="path">路径</param>
    /// <param name="con">需要写入的数据</param>
    public void WriteFile(string path, string con = "")
    {
        string str = con;
        //若是直接在文件里添加数据，则采用重写方法，将append设置为true
        var file = new StreamWriter(path, false);
        //var file = new StreamWriter(path);
        //file.WriteLine(str);
        file.Write(str);
        file.Close();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}