using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Graph : MonoBehaviour
{
public Transform pointPrefab;
[Range(10, 100)]
public int resolution = 10; //prefab의 개수
[Range(0.1f, 5)]
public float height = .5f;
[Range(1, 50)]
public float length = 2f;
[Range(0, 100)]
public float rotationSpeed = 50f;
[Range(.1f, 2f)]
public float prefabScale = 1f;
public GraphName function; //열거형(enum) 형태
private Transform[] points;
private void Awake()
{
points = new Transform[resolution];
for(int i=0; i < points.Length; i++)
{
Transform point = Instantiate(pointPrefab);
point.SetParent(transform, false);
points[i] = point;
}
}
private void Update()
{
float step = length / resolution;
Vector3 scale = Vector3.one * prefabScale;
float t = Time.time;
//delegate를 통한 함수의 배열 가져오기
GraphFuction[] functions = {
SineFunction, MultiSineFunction
};
//delegate 함수 중 하나 선택
GraphFuction f = functions[(int)function];
for (int i=0; i<points.Length; i++)
{
//point wave animation
Transform point = points[i];
Vector3 position = point.localPosition;
position.x = (i + 0.5f) * step - (length / 2);
position.y = f(position.x, t);
position.z = 0;
point.localPosition = position;
point.localScale = scale;
}
//x축으로 회전
Quaternion rotation = transform.localRotation;
rotation = Quaternion.Euler(t * rotationSpeed, 0, 0);
transform.localRotation = rotation;
}
public static float SineFunction(float x, float t)//static을 다른 스크립트에서 접근 가능
{
return Mathf.Sin(Mathf.PI * (x + t));
}
public static float MultiSineFunction(float x, float t)
{
float y = Mathf.Sin(Mathf.PI * (x + t));
y += Mathf.Sin(2f * Mathf.PI * (x + 0.5f * t))/2f;
y *= 2f / 3f;
return y;
}
}