SHOW:
|
|
- or go back to the newest paste.
| 1 | using UnityEngine; | |
| 2 | using System.Collections; | |
| 3 | ||
| 4 | public class FertileGround : MonoBehaviour | |
| 5 | {
| |
| 6 | public float maxRadius = 10.0f; | |
| 7 | public ToBeHealed[] toBeHealed; | |
| 8 | public FertileGroundRingParticles Ring; | |
| 9 | public GrassGrow Grass; | |
| 10 | ||
| 11 | float healingSpeed = 2.0f; | |
| 12 | ||
| 13 | public enum State | |
| 14 | {
| |
| 15 | wasteland, | |
| 16 | transition, | |
| 17 | greenland | |
| 18 | } | |
| 19 | ||
| 20 | State _state = State.wasteland; | |
| 21 | public State state | |
| 22 | {
| |
| 23 | get { return _state; }
| |
| 24 | set | |
| 25 | {
| |
| 26 | State oldState = _state; | |
| 27 | _state = value; | |
| 28 | UpdateState(oldState); | |
| 29 | } | |
| 30 | } | |
| 31 | ||
| 32 | float healingStartTime = 0.0f; | |
| 33 | ||
| 34 | void Update() | |
| 35 | {
| |
| 36 | if( Input.GetMouseButtonDown(0) ) | |
| 37 | StartTransition(); | |
| 38 | ||
| 39 | if (state == State.transition) | |
| 40 | UpdateHealing(); | |
| 41 | } | |
| 42 | ||
| 43 | public void StartTransition() | |
| 44 | {
| |
| 45 | state = State.transition; | |
| 46 | } | |
| 47 | ||
| 48 | public void SetToGreenland() | |
| 49 | {
| |
| 50 | state = State.greenland; | |
| 51 | Grass.SetToGrown(); | |
| 52 | } | |
| 53 | ||
| 54 | void UpdateState(State oldState) | |
| 55 | {
| |
| 56 | foreach (ToBeHealed tbh in toBeHealed) | |
| 57 | {
| |
| 58 | if (!tbh) | |
| 59 | continue; | |
| 60 | tbh.state = state; | |
| 61 | } | |
| 62 | ||
| 63 | if (oldState != State.transition && state == State.transition) | |
| 64 | {
| |
| 65 | healingStartTime = Time.time; | |
| 66 | Ring.StartRing(); | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | void UpdateHealing() | |
| 71 | {
| |
| 72 | float t = Time.time - healingStartTime; | |
| 73 | float progress = Mathf.PingPong(t * 0.05f * healingSpeed, 1.0f); | |
| 74 | float radius = t * 0.17f * healingSpeed + 0.7f; | |
| 75 | radius = Mathf.Pow( radius, 3 ); | |
| 76 | ||
| 77 | if (radius > maxRadius) | |
| 78 | {
| |
| 79 | state = State.greenland; | |
| 80 | Ring.StopRing(); | |
| 81 | return; | |
| 82 | } | |
| 83 | ||
| 84 | Vector3 pos = transform.position; | |
| 85 | ||
| 86 | foreach (ToBeHealed tbh in toBeHealed) | |
| 87 | {
| |
| 88 | if (!tbh) | |
| 89 | continue; | |
| 90 | ||
| 91 | //these operations transform vectors and points needed in the shader to | |
| 92 | //local space of the object undergoing the transition | |
| 93 | Vector3 scale = tbh.transform.localScale; | |
| 94 | ||
| 95 | tbh.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); | |
| 96 | ||
| 97 | Vector3 origin = tbh.transform.InverseTransformPoint(pos); | |
| 98 | Vector3 zeroDirection = tbh.transform.InverseTransformDirection(Vector3.forward); | |
| 99 | Vector3 zeroDirectionPerp = tbh.transform.InverseTransformDirection(Vector3.right); | |
| 100 | ||
| 101 | tbh.transform.localScale = scale; | |
| 102 | ||
| 103 | tbh.UpdateMaterialProperties(zeroDirection, zeroDirectionPerp, origin, radius, progress); | |
| 104 | } | |
| 105 | ||
| 106 | Ring.UpdateRing(radius); | |
| 107 | Grass.UpdateVisibility(0.98f * radius, transform.position); | |
| 108 | } | |
| 109 | } |