using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CinematicEffects; public class ScreenEffects : MonoBehaviour { public DepthOfField depthOfField; public float strength = 0f; public CanvasGroup flashCanvas; public float alertFlashAlpha = 0f; private float baseFlashAlpha; private float originalNearRadius; private float originalFarRadius; private int flashID; void Start () { depthOfField = GetComponent(); if (depthOfField == null) depthOfField = gameObject.AddComponent(); originalNearRadius = depthOfField.focus.nearBlurRadius; originalFarRadius = depthOfField.focus.farBlurRadius; NotificationServer.register("show GameUI", showGame); NotificationServer.register("hide GameUI", hideGame); NotificationServer.register("flash damage", flashDamage); NotificationServer.register("statechange Searchlight", searchlightStateChanged); } public void showGame() { LeanTween.cancel(gameObject, false); float startNear = depthOfField.focus.nearBlurRadius; float startFar = depthOfField.focus.farBlurRadius; DepthOfField.FocusSettings focus = depthOfField.focus; LeanTween.value(gameObject, 1f, 1f*strength, 0.5f).setEaseInOutQuad().setOnUpdate((float val)=>{ focus.nearBlurRadius = startNear * val; focus.farBlurRadius = startFar * val; }).setOnComplete(()=>{ if (strength == 0f) depthOfField.enabled = false; }).setIgnoreTimeScale(true); float target = 0f; if (Searchlight.state == Searchlight.SearchState.Chasing) target = alertFlashAlpha; LeanTween.cancel(flashID); flashID = LeanTween.alphaCanvas(flashCanvas, target, 0.5f).setEaseInOutCubic().setIgnoreTimeScale(true).uniqueId; } public void hideGame() { LeanTween.cancel(gameObject, false); depthOfField.enabled = true; float startNear = depthOfField.focus.nearBlurRadius; float startFar = depthOfField.focus.farBlurRadius; DepthOfField.FocusSettings focus = depthOfField.focus; LeanTween.value(gameObject, 1f*strength, 1f, 0.5f).setEaseInOutQuad().setOnUpdate((float val)=>{ focus.nearBlurRadius = startNear + (originalNearRadius-startNear) * val; focus.farBlurRadius = startFar + (originalFarRadius-startFar) * val; }).setIgnoreTimeScale(true); } public void flashDamage() { LeanTween.cancel(flashID); flashID = LeanTween.alphaCanvas(flashCanvas, 1f, 0.2f).setEaseInOutCubic().setOnComplete(()=>{ float target = 0f; if (Searchlight.state == Searchlight.SearchState.Chasing) target = alertFlashAlpha; flashID = LeanTween.alphaCanvas(flashCanvas, target, 0.8f).setEaseInOutCubic().uniqueId; }).uniqueId; } public void searchlightStateChanged() { float target = 0f; if (Searchlight.state == Searchlight.SearchState.Chasing) target = alertFlashAlpha; LeanTween.cancel(flashID); flashID = LeanTween.alphaCanvas(flashCanvas, target, 0.5f).setEaseInOutCubic().uniqueId; } }