using System.Collections; using System.Collections.Generic; using UnityEngine; 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 cooldown = 2.0f; [SerializeField] float offset = 1.5f; Vector3 root; Coroutine flow = null; private void Start() { root = transform.position; //StartCoroutine(Flow()); } private void OnEnable() { if (material == null) material = GetComponent().material; if (player == null) { player = gameObject.AddComponent(); player.VideoAsset = clip; player.Load(); } if (flow != null) StopCoroutine(flow); flow = StartCoroutine(Flow()); } private void FixedUpdate() { if (SceneController.Instance.rate > 0.5f) { alpha = Mathf.Lerp(alpha, 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) 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; } [SerializeField, Range(0, 1)] float breakProgress = 0.1f; IEnumerator Flow() { float video_stamp = 0.0f; float progress = 0.0f; video_stamp = Time.time; progress = 0.0f; player.Pause(); player.Frame = 0; //Debug.Log("Begin"); while (progress < breakProgress) { 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, 0.0f); } player.Frame = (uint)(player.TotalNumFrames * progress); yield return null; } //Debug.Log("Play the rest"); player.Play(); while (player.IsFinished == false) { yield return null; } yield return new WaitForSeconds(cooldown); transform.position = root + new Vector3(Random.Range(-offset, offset), 0, 0); yield return null; //Debug.Log("Flow done"); } } }