using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public class ShinyThings : MonoBehaviour { public WorldCursor Cursor; //Attributes for Highlighting of Facts when Mouse-Over private List<FactObject> LastFactSelection = new(); //Variables for Pushout-Highlighting private static float timerDuration = 2.5f; private static float lerpTime = 1f; private IEnumerator rain_wait; private IEnumerator rain; public Light directionalLight; private Color darkColor = new(0.2f, 0.2f, 0.2f); private Color light_colour; private GameObject active_rainwork; public GameObject Fireworks_Animation, RainPrefab; private void Awake() { CommunicationEvents.PushoutFactEvent.AddListener(HighlightFact); CommunicationEvents.PushoutFactFailEvent.AddListener(LetItRain); CommunicationEvents.AnimateExistingFactEvent.AddListener(HighlightFact); CommunicationEvents.AnimateExistingAsSolutionEvent.AddListener(HighlightWithFireworks); rain = rain_wait = IEnumeratorExtensions.yield_break; if (Cursor == null) Cursor = GetComponent<WorldCursor>(); if (directionalLight == null) directionalLight = FindObjectOfType<Light>(); light_colour = directionalLight.color; } public void Update() { if (Cursor?.Hits != null) HighlightCurserHit(Cursor.Hits); } private void HighlightCurserHit(RaycastHit[] hits) { List<FactObject> selected_fact_objs = hits .Select(h => h.transform?.GetComponentInChildren<FactObject>()) .Where(f => f != null) .ToList(); //Set the last Facts unselected foreach (var lastFact in LastFactSelection) if (lastFact != null && !selected_fact_objs.Contains(lastFact)) ApplyMaterial(lastFact, lastFact.Default); //Set the Facts that were Hit as selected foreach (var fact_obj in selected_fact_objs) if (!LastFactSelection.Contains(fact_obj)) ApplyMaterial(fact_obj, fact_obj.Selected); LastFactSelection = selected_fact_objs; return; void ApplyMaterial(FactObject root, Material new_mat) => root.CoroutineCascadeForMeAndChildrenAllRenderer( (_, renderer) => renderer.ProgrammMaterialChange(new[] { (0f, lerpTime, new_mat), }) ); } public static void HighlightFact(Fact startFact, FactObject.FactMaterials tmp_mat) { if (startFact.Representation == null) return; FactObject selected_fact_obj = startFact.Representation.GetComponentInChildren<FactObject>(); selected_fact_obj.CoroutineCascadeForMeAndChildrenAllRenderer( (fact_obj, renderer) => renderer.ProgrammMaterialChange(new[] { (0f, lerpTime, fact_obj.materials[(int) tmp_mat]), (GlobalBehaviour.hintAnimationDuration, lerpTime, fact_obj.Default), }) ); } public void HighlightWithFireworks(Fact fact, FactObject.FactMaterials mat) { rain_wait = IEnumeratorExtensions.yield_break; //stop rain StartCoroutine(BlossomAndDie()); HighlightFact(fact, mat); IEnumerator BlossomAndDie() { GameObject firework = GameObject.Instantiate (Fireworks_Animation , fact.Representation.transform ); yield return new WaitForSeconds(timerDuration); firework.transform.GetChild(0) .GetComponent<ParticleSystem>() .Stop(); var sparks = firework.transform.GetChild(1) .GetComponent<ParticleSystem>(); sparks.Stop(); while (sparks.IsAlive()) yield return null; GameObject.Destroy(firework); } } public void LetItRain(Fact startFact, Scroll.ScrollApplicationInfo _) { // check if couroutine is waiting or finished if (!rain_wait.MoveNext() || !rain.MoveNext()) { StopCoroutine(rain); StartCoroutine(rain = BlossomAndDie()); } rain_wait = 0f.LerpInTime(0, timerDuration); //reset timer IEnumerator BlossomAndDie() { Destroy(active_rainwork); active_rainwork = GameObject.Instantiate(RainPrefab, new Vector3(0, 40, 0), Quaternion.identity); Color start = directionalLight.color; // may not be original one for (IEnumerator<float> lerper = MathfExtensions.LerpInTime(0, 1, lerpTime) ; lerper.MoveNext();) { directionalLight.color = Color.Lerp(start, darkColor, lerper.Current); yield return null; } while (rain_wait.MoveNext()) yield return null; for (IEnumerator<float> lerper = MathfExtensions.LerpInTime(0, 1, lerpTime) ; lerper.MoveNext();) { directionalLight.color = Color.Lerp(darkColor, light_colour, lerper.Current); yield return null; } GameObject.Destroy(active_rainwork); } } }