Skip to content

Commit 1e2ecfa

Browse files
committed
Added events and setters to variables
1 parent fc266ab commit 1e2ecfa

File tree

10 files changed

+188
-100
lines changed

10 files changed

+188
-100
lines changed

ScriptableObjectsInfo.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ MonoBehaviour:
1212
m_Script: {fileID: 11500000, guid: 51c511b357030b347b0d657b55dee79e, type: 3}
1313
m_Name: ScriptableObjectsInfo
1414
m_EditorClassIdentifier:
15-
_buildIndex: 0
15+
_buildIndex: 1
1616
_dependencies:
1717
- {fileID: 11400000, guid: c638a22521017c54e97c0744ffbc5ef9, type: 2}

Scripts/ModeRelocator.cs

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,68 @@
44

55
namespace DuskModules.ScriptableObjects {
66

7-
/// <summary> Reads from an Int variable to determine what mode it is in, and moves itself to match target. </summary>
8-
public class ModeRelocator : MonoBehaviour {
9-
10-
/// <summary> List of targets </summary>
11-
[Tooltip("The list of targets this relocator can move from and to.")]
12-
public List<Transform> targets;
7+
/// <summary> Reads from an Int variable to determine what mode it is in, and moves itself to match target. </summary>
8+
public class ModeRelocator : MonoBehaviour {
139

14-
/// <summary> Current mode to use </summary>
15-
[Tooltip("The int value to use. Preferred to reference an IntVariable ScriptableObject.")]
16-
public IntReference mode;
10+
/// <summary> List of targets </summary>
11+
[Tooltip("The list of targets this relocator can move from and to.")]
12+
public List<Transform> targets;
1713

18-
/// <summary> Speed to move position with. </summary>
19-
[Tooltip("The speed to use to move the position with.")]
20-
public LerpMoveValue positionSpeed;
21-
/// <summary> Speed to move rotation with. </summary>
22-
[Tooltip("The speed to use to move the rotation with.")]
23-
public LerpMoveValue rotationSpeed;
24-
/// <summary> Speed to move scale with. </summary>
25-
[Tooltip("The speed to use to move the scale with.")]
26-
public LerpMoveValue scaleSpeed;
14+
/// <summary> Current mode to use </summary>
15+
[Tooltip("The int value to use. Preferred to reference an IntVariable ScriptableObject.")]
16+
public IntReference mode;
2717

28-
/// <summary> Whether it should ignore out of bounds indexes and keep last position </summary>
29-
[Tooltip("If true, any value falling outside of targets array bounds will be ignored. " +
30-
"Use this to keep the last position for invalid indexes instead of clamping and taking the closest possible target index.")]
31-
public bool ignoreOutOfBounds;
32-
/// <summary> Value the mode must be for it to match with target index 0. </summary>
33-
[Tooltip("The first valid value that can be used. The mode value set here will match with target index 0.")]
34-
public int modeStartValue;
18+
/// <summary> Speed to move position with. </summary>
19+
[Tooltip("The speed to use to move the position with.")]
20+
public LerpMoveValue positionSpeed;
21+
/// <summary> Speed to move rotation with. </summary>
22+
[Tooltip("The speed to use to move the rotation with.")]
23+
public LerpMoveValue rotationSpeed;
24+
/// <summary> Speed to move scale with. </summary>
25+
[Tooltip("The speed to use to move the scale with.")]
26+
public LerpMoveValue scaleSpeed;
3527

36-
/// <summary> The mode to use </summary>
37-
protected int useMode;
28+
/// <summary> Whether it should ignore out of bounds indexes and keep last position </summary>
29+
[Tooltip("If true, any value falling outside of targets array bounds will be ignored. " +
30+
"Use this to keep the last position for invalid indexes instead of clamping and taking the closest possible target index.")]
31+
public bool ignoreOutOfBounds;
32+
/// <summary> Value the mode must be for it to match with target index 0. </summary>
33+
[Tooltip("The first valid value that can be used. The mode value set here will match with target index 0.")]
34+
public int modeStartValue;
3835

39-
// Awaken and set to target immediatly.
40-
protected virtual void Awake() {
36+
/// <summary> The mode to use </summary>
37+
protected int useMode;
38+
39+
// Awaken and set to target immediatly.
40+
protected virtual void Awake() {
4141
useMode = Mathf.Clamp(mode.value, 0, targets.Count - 1);
42-
Transform target = targets[useMode];
42+
Transform target = targets[useMode];
4343

44-
transform.position = target.position;
45-
transform.rotation = target.rotation;
44+
transform.localPosition = target.localPosition;
45+
transform.localRotation = target.localRotation;
4646
transform.localScale = target.localScale;
47-
}
47+
}
4848

49-
// Move self to target mode
50-
protected virtual void Update() {
51-
int modeValue = mode.value - modeStartValue;
52-
if (!ignoreOutOfBounds)
49+
// Move self to target mode
50+
protected virtual void Update() {
51+
int modeValue = mode.value - modeStartValue;
52+
if (!ignoreOutOfBounds)
5353
useMode = Mathf.Clamp(modeValue, 0, targets.Count - 1);
54-
else if (modeValue >= 0 && modeValue < targets.Count)
54+
else if (modeValue >= 0 && modeValue < targets.Count)
5555
useMode = modeValue;
5656

57-
Transform target = targets[useMode];
58-
Vector3 targetPos = target.position;
59-
Quaternion targetRot = target.rotation;
60-
Vector3 targetScale = target.localScale;
57+
Transform target = targets[useMode];
58+
Vector3 targetPos = target.localPosition;
59+
Quaternion targetRot = target.localRotation;
60+
Vector3 targetScale = target.localScale;
6161

62-
if (transform.position != targetPos)
63-
transform.position = positionSpeed.Move(transform.position, targetPos);
64-
if (transform.rotation != targetRot)
65-
transform.rotation = rotationSpeed.Move(transform.localRotation, targetRot);
66-
if (transform.localScale != targetScale)
62+
if (transform.localPosition != targetPos)
63+
transform.localPosition = positionSpeed.Move(transform.localPosition, targetPos);
64+
if (transform.localRotation != targetRot)
65+
transform.localRotation = rotationSpeed.Move(transform.localRotation, targetRot);
66+
if (transform.localScale != targetScale)
6767
transform.localScale = scaleSpeed.Move(transform.localScale, targetScale);
68-
}
68+
}
6969

70-
}
70+
}
7171
}

Scripts/Variables/ColorReference.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33

44
namespace DuskModules.ScriptableObjects {
55

6-
[Serializable]
7-
public class ColorReference {
8-
public bool useConstant = true;
9-
public Color constant;
10-
public ColorVariable variable;
6+
[Serializable]
7+
public class ColorReference {
8+
public bool useConstant = true;
9+
public Color constant;
10+
public ColorVariable variable;
1111

12-
public Color value => useConstant ? constant : variable.value;
13-
}
12+
public Color value {
13+
get => useConstant ? constant : variable.value;
14+
set {
15+
if (this.value != value) {
16+
if (useConstant) constant = value;
17+
else variable.value = value;
18+
}
19+
}
20+
}
21+
}
1422

15-
}
23+
}

Scripts/Variables/ColorVariable.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
45

56
namespace DuskModules.ScriptableObjects {
67

7-
[CreateAssetMenu(menuName = "DuskModules/Variable/ColorVariable")]
8-
public class ColorVariable : ScriptableObject {
9-
public Color value;
10-
}
8+
[CreateAssetMenu(menuName = "DuskModules/Variable/ColorVariable")]
9+
public class ColorVariable : ScriptableObject {
10+
11+
public event Action onChanged;
12+
13+
[SerializeField]
14+
private Color _value;
15+
public Color value {
16+
get => _value;
17+
set {
18+
if (_value != value) {
19+
_value = value;
20+
onChanged?.Invoke();
21+
}
22+
}
23+
}
24+
}
1125

1226
}

Scripts/Variables/FloatReference.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
namespace DuskModules.ScriptableObjects {
44

5-
[Serializable]
6-
public class FloatReference {
7-
public bool useConstant = true;
8-
public float constant;
9-
public FloatVariable variable;
10-
11-
public float value => useConstant ? constant : variable.value;
12-
}
5+
[Serializable]
6+
public class FloatReference {
7+
public bool useConstant = true;
8+
public float constant;
9+
public FloatVariable variable;
10+
11+
public float value {
12+
get => useConstant ? constant : variable.value;
13+
set {
14+
if (this.value != value) {
15+
if (useConstant) constant = value;
16+
else variable.value = value;
17+
}
18+
}
19+
}
20+
}
1321

1422
}

Scripts/Variables/FloatVariable.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
45

56
namespace DuskModules.ScriptableObjects {
67

7-
[CreateAssetMenu(menuName = "DuskModules/Variable/FloatVariable")]
8-
public class FloatVariable : ScriptableObject {
9-
public float value;
10-
}
8+
[CreateAssetMenu(menuName = "DuskModules/Variable/FloatVariable")]
9+
public class FloatVariable : ScriptableObject {
10+
11+
public event Action onChanged;
12+
13+
[SerializeField]
14+
private float _value;
15+
public float value {
16+
get => _value;
17+
set {
18+
if (_value != value) {
19+
_value = value;
20+
onChanged?.Invoke();
21+
}
22+
}
23+
}
24+
}
1125

1226
}

Scripts/Variables/IntReference.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
namespace DuskModules.ScriptableObjects {
44

5-
[Serializable]
6-
public class IntReference {
7-
public bool useConstant = true;
8-
public int constant;
9-
public IntVariable variable;
10-
11-
public int value => useConstant ? constant : variable.value;
12-
}
5+
[Serializable]
6+
public class IntReference {
7+
public bool useConstant = true;
8+
public int constant;
9+
public IntVariable variable;
10+
11+
public int value {
12+
get => useConstant ? constant : variable.value;
13+
set {
14+
if (this.value != value) {
15+
if (useConstant) constant = value;
16+
else variable.value = value;
17+
}
18+
}
19+
}
20+
}
1321

1422
}

Scripts/Variables/IntVariable.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
45

56
namespace DuskModules.ScriptableObjects {
67

78
[CreateAssetMenu(menuName = "DuskModules/Variable/IntVariable")]
89
public class IntVariable : ScriptableObject {
9-
public int value;
10-
}
1110

11+
public event Action onChanged;
12+
13+
[SerializeField]
14+
private int _value;
15+
public int value {
16+
get => _value;
17+
set {
18+
if (_value != value) {
19+
_value = value;
20+
onChanged?.Invoke();
21+
}
22+
}
23+
}
24+
25+
}
1226
}

Scripts/Variables/StringReference.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
namespace DuskModules.ScriptableObjects {
44

5-
[Serializable]
6-
public class StringReference {
7-
public bool useConstant = true;
8-
public string constant;
9-
public StringVariable variable;
10-
11-
public string value => useConstant ? constant : variable.value;
12-
}
5+
[Serializable]
6+
public class StringReference {
7+
public bool useConstant = true;
8+
public string constant;
9+
public StringVariable variable;
10+
11+
public string value {
12+
get => useConstant ? constant : variable.value;
13+
set {
14+
if (this.value != value) {
15+
if (useConstant) constant = value;
16+
else variable.value = value;
17+
}
18+
}
19+
}
20+
}
1321

1422
}
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
45

56
namespace DuskModules.ScriptableObjects {
67

7-
[CreateAssetMenu(menuName = "DuskModules/Variable/StringVariable")]
8-
public class StringVariable : ScriptableObject {
9-
public string value;
10-
}
8+
[CreateAssetMenu(menuName = "DuskModules/Variable/StringVariable")]
9+
public class StringVariable : ScriptableObject {
10+
11+
public event Action onChanged;
12+
13+
[SerializeField]
14+
private string _value;
15+
public string value {
16+
get => _value;
17+
set {
18+
if (_value != value) {
19+
_value = value;
20+
onChanged?.Invoke();
21+
}
22+
}
23+
}
24+
}
1125

1226
}

0 commit comments

Comments
 (0)