You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace UltraCombos.Frozen
{
public class GiantSnowflake : MonoBehaviour
{
Material material;
float alpha = 0.0f;
float smooth = 0.025f;
[SerializeField]
DShowClip clip;
DShowMoviePlayer player;
// [SerializeField]
// KinectOpticalFlowMath kinect;
[SerializeField]
float triggerRadius = 1.5f;
[SerializeField]
float stopTime = 2.0f;
[SerializeField]
float cooldown = 2.0f;
bool is_cooling = false;
[SerializeField]
float offset = 1.5f;
Vector3 root;
Coroutine flow = null;
[SerializeField, Range(0, 1)]
float breakProgress = 0.1f;
public UnityEvent onPlay = new UnityEvent();
[SerializeField, Header("Debug")]
bool mode = false;
public string info;
private void Start()
{
root = transform.position;
//StartCoroutine(Flow());
}
private void OnEnable()
{
if (material == null)
material = GetComponent<MeshRenderer>().material;
if (player == null)
{
player = gameObject.AddComponent<DShowMoviePlayer>();
player.VideoAsset = clip;
player.Load();
}
player.Pause();
player.Frame = 0;
if (flow != null)
StopCoroutine(flow);
//flow = StartCoroutine(Flow());
}
private void Update()
{
if (mode == false)
return;
}
private void FixedUpdate()
{
if (SceneController.Instance.rate > 0.5f)
{
alpha = Mathf.Lerp(alpha, is_cooling ? 0.0f : 1.0f, smooth);
}
else
{
alpha = Mathf.Lerp(alpha, 0.0f, smooth);
}
material.color = new Color(1.0f, 1.0f, 1.0f, alpha);
material.mainTexture = player.Texture;
if (flow != null && SceneController.Instance.state != State.Night)
{
StopCoroutine(flow);
flow = null;
}
if (flow == null && SceneController.Instance.state == State.Night)
{
flow = StartCoroutine(Flow());
}
}
private void OnDrawGizmosSelected()
{
/*
var pos = kinect.averagePositinon;
Gizmos.DrawWireSphere(pos, 0.5f);
if (new Vector2(pos.x - transform.position.x, pos.z - transform.position.z).magnitude < triggerRadius)
{
Gizmos.color = Color.red;
}
*/
Gizmos.DrawWireSphere(transform.position, triggerRadius);
Gizmos.color = Color.white;
}
IEnumerator Flow()
{
float video_stamp = 0.0f;
float progress = 0.0f;
uint pause_frame = 20;
float pause_progress = (float)pause_frame / player.TotalNumFrames;
is_cooling = false;
video_stamp = Time.time;
player.Frame = 0;
player.Play();
yield return null;
//Debug.Log("Begin");
while (player.Frame < pause_frame)
{
//Debug.Log("Loop");
yield return null;
}
player.Pause();
//Debug.Log("Pause");
progress = (float)player.Frame / player.TotalNumFrames;
yield return null;
//Debug.Log("Wait");
while (progress < breakProgress)
{
if (mode) info = $"{progress}";
#if false
var pos = kinect.averagePositinon;
float dist = new Vector2(pos.x - transform.position.x, pos.z - transform.position.z).magnitude;
if (dist < triggerRadius)
{
//Debug.Log("OK");
progress += 0.001f;
}
else
{
progress = Mathf.Max(progress - 0.001f, pause_progress);
}
#else
// bool is_hit = false;
// if (kinect.Valid)
// {
// var samples = kinect.Samples;
// int max_sample = 500;
// int inc = Mathf.Max(kinect.HomeCount / max_sample, 1);
// for (int i = 0; i < kinect.HomeCount; i += inc)
// {
// float dist = new Vector2(samples[i].x - transform.position.x, samples[i].z - transform.position.z).magnitude;
// if (dist < triggerRadius)
// {
// //Debug.Log("is_hit");
// progress += 0.001f;
// is_hit = true;
// break;
// }
// }
// }
// if (is_hit == false)
// {
// progress = Mathf.Max(progress - 0.001f, pause_progress);
// }
#endif
player.Frame = (uint)(player.TotalNumFrames * progress);
yield return null;
}
if (mode) info = $"{progress}";
//Debug.Log("Play the rest");
player.Play();
onPlay.Invoke();
while (player.IsPlaying)
{
yield return null;
}
player.Pause();
player.Frame = player.TotalNumFrames;
yield return new WaitForSeconds(stopTime);
is_cooling = true;
yield return new WaitForSeconds(cooldown);
transform.position = root + new Vector3(Random.Range(-offset, offset), 0, 0);
flow = null;
yield return null;
//Debug.Log("Flow done");
}
}
}