Newer
Older
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShinyThings : MonoBehaviour
{
public WorldCursor Cursor;
//Attributes for Highlighting of Facts when Mouse-Over
private FactObject lastFactSelection;
private static float timerDuration = 2.5f;
John Schihada
committed
public GameObject directionalLight;
private Color darkColor = new (0.3f, 0.3f, 0.3f);
Marco Zimmer
committed
public GameObject
Fireworks_Animation,
RainPrefab;
// Start is called before the first frame update
if (Cursor == null)
Cursor = GetComponent<WorldCursor>();
if (directionalLight == null)
directionalLight = FindObjectOfType<Light>().gameObject;
CommunicationEvents.PushoutFactEvent.AddListener(HighlightFact);
John Schihada
committed
CommunicationEvents.PushoutFactFailEvent.AddListener(StartPushoutFactFailHighlighting);
CommunicationEvents.AnimateExistingFactEvent.AddListener(HighlightWithFireworks);
}
// Update is called once per frame
}
private void Highlighting(RaycastHit hit)
{
FactObject selected_fact_obj = hit.transform?.GetComponentInChildren<FactObject>();
//Set the last Fact unselected
if (this.lastFactSelection != null
&& (selected_fact_obj == null || this.lastFactSelection != selected_fact_obj))
ApplyMaterial(lastFactSelection, lastFactSelection.Default);
this.lastFactSelection = null;
//Set the Fact that was Hit as selected
if (selected_fact_obj != null && hit.transform != null
&& (hit.transform.CompareTag("Selectable") || hit.transform.CompareTag("SnapZone"))
&& (this.lastFactSelection == null || this.lastFactSelection != selected_fact_obj))
ApplyMaterial(selected_fact_obj, selected_fact_obj.Selected);
this.lastFactSelection = selected_fact_obj;
void ApplyMaterial(FactObject root, Material new_mat) =>
root.CoroutineCascadeForChildrenAllRenderer(
(_, renderer) =>
renderer.ProgrammMaterialChange(new[] {
(0f, 1f, new_mat),
})
);
public static void HighlightFact(Fact startFact, FactObject.FactMaterials tmp_mat)
FactObject selected_fact_obj = startFact.Representation.GetComponentInChildren<FactObject>();
selected_fact_obj.CoroutineCascadeForChildrenAllRenderer(
(fact_obj, renderer) =>
renderer.ProgrammMaterialChange(new[] {
(0f, 1f, fact_obj.materials[(int) tmp_mat]),
(timerDuration, 1f, fact_obj.Default),
})
);
public void HighlightWithFireworks(Fact fact)
{
StartCoroutine(BlossomAndDie());
HighlightFact(fact, FactObject.FactMaterials.Solution);
GameObject firework = GameObject.Instantiate(Fireworks_Animation);
firework.transform.position = fact.Representation.transform.position;
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);
}
John Schihada
committed
public void StartPushoutFactFailHighlighting(Fact startFact)
{
StartCoroutine(BlossomAndDie());
John Schihada
committed
John Schihada
committed
{
Color start = directionalLight.GetComponent<Light>().color;
IEnumerator<float> num = LerpfTime(0, 1, 1);
while (num.MoveNext()) {
directionalLight.GetComponent<Light>().color = Color.Lerp(start, darkColor, num.Current);
yield return null;
John Schihada
committed
}
GameObject rainwork = GameObject.Instantiate(RainPrefab, new Vector3(0, 40, 0), Quaternion.identity);
yield return new WaitForSeconds(2*timerDuration);
GameObject.Destroy(rainwork);
num = LerpfTime(0, 1, 1);
while (num.MoveNext())
John Schihada
committed
{
directionalLight.GetComponent<Light>().color = Color.Lerp(darkColor, start, num.Current);
yield return null;
John Schihada
committed
}
}
IEnumerator<float> LerpfTime(float start, float end, float time)
John Schihada
committed
{
float
start_time = Time.time,
end_time = start_time + time;
John Schihada
committed
while(end_time >= Time.time)
yield return Mathf.Lerp(start, end, (Time.time - start_time) / time);
John Schihada
committed
}
}