-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifier.cs
More file actions
102 lines (84 loc) · 2.86 KB
/
Copy pathModifier.cs
File metadata and controls
102 lines (84 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using ollyisonit.UnityAccessors;
using System;
using System.Collections;
using UnityEngine;
namespace ollyisonit.UnityAnimationModifiers
{
/// <summary>
/// Modifies a value of type T over time according to some method.
/// </summary>
[Serializable]
public abstract class Modifier<T>
{
[Tooltip("Should the modification be added on top of the original value?")]
public bool additive = true;
[Tooltip("How much should the modifier affect the target value? " +
"An intensity of 1 will make the modifier have its full effect, and 0 will make it have no effect at all.")]
public float intensity = 1;
/// <summary>
/// Stores what value was before it was modified.
/// </summary>
protected T originalValue;
/// <summary>
/// How much time has elapsed since the modifier started?
/// </summary>
[HideInInspector]
public float time;
/// <summary>
/// Resets modifier back to its original values.
/// </summary>
/// <param name="gameObject">GameObject that modifier is associated with.</param>
public virtual void Reset(MonoBehaviour mb)
{
ResetAccessors.Reset(this, mb);
additive = true;
intensity = 1;
}
/// <summary>
/// Should be called before the first frame update.
/// </summary>
public virtual void Awake()
{
}
/// <summary>
/// Gets value that is being modified.
/// </summary>
protected abstract T GetValue();
/// <summary>
/// Sets value that is being modified.
/// </summary>
protected abstract void SetValue(T value);
/// <summary>
/// Given an original value, gets what the value should be modified to be.
/// </summary>
protected abstract T GetModifiedValue(T originalValue);
/// <summary>
/// Should be called in the Unity LateUpdate method.
/// </summary>
/// <param name="mb">Monobehaviour associated with the modifier.</param>
public void LateUpdate(MonoBehaviour mb)
{
if (mb.isActiveAndEnabled)
{
if (additive)
{
originalValue = GetValue();
mb.StartCoroutine(SetValueBack());
}
SetValue(GetModifiedValue(GetValue()));
time += Time.deltaTime;
}
}
/// <summary>
/// Coroutine that sets the modified value back to its original value at the end of the frame.
/// </summary>
private IEnumerator SetValueBack()
{
yield return new WaitForEndOfFrame();
if (additive)
{
SetValue(originalValue);
}
}
}
}