-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkillTreeComponents.cs
More file actions
158 lines (127 loc) · 6.03 KB
/
SkillTreeComponents.cs
File metadata and controls
158 lines (127 loc) · 6.03 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
SKILL TREE COMPONENT SCRIPT:
* The script is for a Skill Tree system in Unity.
* It has several serialized fields that can be set in the Unity Editor.
* These fields include whether the node is automatically unlocked, the required nodes for unlocking this node, and various visual elements for displaying the node’s progression and state.
* The script has an event for when the node is unlocked.
* The script updates the node’s progression and visual state based on whether its requirements have been met.
* If all requirements are met and the node is not yet unlocked, it can be unlocked by clicking on it (buying it).
* Once unlocked, it will display the unlocked visual and invoke the OnUnlocked event.
EDITOR PARAMETERS:
* isAutoUnlock: A boolean value that determines whether the node is automatically unlocked or not. should be used for links between nodes or nodes that automatically unlock if their requirement is unlocked.
* requiredNodes: A list of SkillTreeComponents that represents the required nodes for unlocking this node.
* ProgressionVisuals: A list of GameObjects that represent the visual elements for displaying the node’s progression toward being unlocked.
* buyableVisual: A GameObject that represents the visual element for when the node can be bought (i.e. when all requirements are met but the node is not yet unlocked).
* unlockedVisual: A GameObject that represents the visual element for when the node is unlocked.
SCENE SETUP:
* Simply put the script as compoent on your node and links. use game obects as visuals
* Links are just nodes with different visuals and with the "auto-unlock" parameter to true. They also listen to a node's "unlock" state and changes state accordingly. Normally nodes never require links. Only Links need a node to listen to.
*/
using System;
using System.Collections.Generic;
using UnityEngine;
public class SkillTreeComponents : MonoBehaviour
{
[Tooltip("Whether this node should unlock automatically when its requirements are met. Use for links between nodes or nodes that should unlock without player input.")]
[SerializeField]
private bool isAutoUnlock = false;
[Tooltip("List of nodes that need to be unlocked before this node can be unlocked.")]
[SerializeField]
private List<SkillTreeComponents> requiredNodes;
[Tooltip("Visual elements corresponding to different stages of progression towards unlocking this node. Order matters: The first element corresponds to the first unlocked node, the second to the second, and so on.")]
[SerializeField]
private List<GameObject> ProgressionVisuals;
[Tooltip("Visual element that is displayed when the node can be unlocked (all requirements are met but the player hasn't unlocked it yet).")]
[SerializeField]
private GameObject buyableVisual;
[Tooltip("Visual element that is displayed when the node has been unlocked.")]
[SerializeField]
private GameObject unlockedVisual;
private int progressionIndex = 0;
public event Action OnUnlocked;
private void Awake()
{
IsUnlocked = false;
foreach (var node in requiredNodes)
{
node.OnUnlocked += UpdateProgression;
}
// Update initial state.
UpdateProgression();
}
private void UpdateProgression()
{
// Check each required node.
progressionIndex = 0;
foreach (var node in requiredNodes)
{
if (node.IsUnlocked)
{
progressionIndex++;
}
}
Debug.Log($"{gameObject.name} - Progression updated. Progression index: {progressionIndex}");
// Set the visual state.
if (ProgressionVisuals.Count > 0 && progressionIndex < requiredNodes.Count)
{
// Not all requirements are met, so show the appropriate visual.
SetActiveVisual(ProgressionVisuals[progressionIndex]);
Debug.Log($"{gameObject.name} - Showing visual for incomplete requirements.");
}
else if (!IsUnlocked && progressionIndex == requiredNodes.Count)
{
// All requirements are met
if (isAutoUnlock)
{
// Auto-unlock nodes (links) should automatically unlock
Debug.Log($"{gameObject.name} - Auto-unlock is enabled, unlocking...");
Unlock();
}
else
{
// Other nodes should show the buyable visual
SetActiveVisual(buyableVisual);
Debug.Log($"{gameObject.name} - All requirements met, showing buyable visual.");
}
}
}
private bool CanBuy()
{
// We can buy the node if all requirements are met.
return progressionIndex >= requiredNodes.Count;
}
public void Unlock()
{
if (!IsUnlocked && (isAutoUnlock || CanBuy()))
{
// The node is not yet unlocked, so unlock it and show the unlocked visual.
IsUnlocked = true;
SetActiveVisual(unlockedVisual);
Debug.Log($"{gameObject.name} - Node unlocked.");
OnUnlocked?.Invoke();
}
}
private void SetActiveVisual(GameObject visual)
{
// Turn off all visuals, then turn on the specified one.
foreach (var v in ProgressionVisuals)
{
v.SetActive(false);
}
buyableVisual.SetActive(false);
unlockedVisual.SetActive(false);
visual.SetActive(true);
Debug.Log($"{gameObject.name} - Active visual set to: {visual.name}");
}
private void OnMouseDown()
{
// Requires a Collider on the object to be clicked to register
// When the node is clicked, if we can buy it, then unlock it.
if (CanBuy())
{
Debug.Log($"{gameObject.name} - Node clicked and can be bought, unlocking...");
Unlock();
}
}
public bool IsUnlocked { get; private set; }
}