using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathReader : MonoBehaviour
{
public GameObject MarkStart, MarkGoal, MarkFP;
public float smooth_distance;
public TextAsset filename;
//private List<string> file_data_line;
private List<Vector3> positions, rotations;
private LineRenderer lineRenderer;
void Awake()
{
lineRenderer = GetComponent<LineRenderer>();
//file_data = System.IO.File.ReadAllLines(filename_path);
//Debug.Log(filename);
}
void Start()
{
List<string> file_data_each_line = TextAssetToListString(filename);
ListStringToListVector(file_data_each_line, out positions, out rotations, smooth_distance);
//MapToGroundPosition(); //cannot change the value of List<Vector3> after assigned.
//Assign positinos to line renderer
lineRenderer.positionCount = positions.Count;
lineRenderer.SetPositions(positions.ToArray());
//Assign Mark object position
MarkStart.transform.position = positions[0];
MarkGoal.transform.position = positions[positions.Count-1];
MarkFP.transform.position = positions[1];
Debug.Log("Positions count = " + positions.Count);
}
private List<string> TextAssetToListString(TextAsset ta)
{
return new List<string>(ta.text.Split('\n'));
}
private void ListStringToListVector(List<string> list_text, out List<Vector3> list_pos, out List<Vector3> list_rot, float smd)
{
list_pos = new List<Vector3>();
list_rot = new List<Vector3>();
// 1st line is label
// 2nd line add to thevector
string[] columns = list_text[1].Split(',');
Vector3 load_position = new Vector3(float.Parse(columns[1]), 0f, float.Parse(columns[3])); //set y to ground
Vector3 load_rotation = new Vector3(float.Parse(columns[4]), float.Parse(columns[5]), float.Parse(columns[6]));
positions.Add(load_position);
rotations.Add(load_rotation);
for (int i = 2; i < list_text.Count; i++) // from 3nd line, check distance before add to the vector
{
//Debug.Log(line);
columns = list_text[i].Split(',');
if (columns.Length != 7) // not enough information to extract (end of line)
return;
//Debug.Log(i+" "+columns.Length);
//Vector3 load_position = new Vector3(float.Parse(columns[1]), float.Parse(columns[2]), float.Parse(columns[3]));
load_position = new Vector3(float.Parse(columns[1]), 0f, float.Parse(columns[3])); //set y to ground
load_rotation = new Vector3(float.Parse(columns[4]), float.Parse(columns[5]), float.Parse(columns[6]));
if(Vector3.Distance(positions[positions.Count-1], load_position) > smd)
{
positions.Add(load_position);
rotations.Add(load_rotation);
}
}
}
}