-
Notifications
You must be signed in to change notification settings - Fork 8
/
SkillManager.cs
178 lines (159 loc) · 6.95 KB
/
SkillManager.cs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using UnityEngine;
using JotunnLib.Entities;
namespace JotunnLib.Managers
{
/// <summary>
/// Handles all logic that has to do with skills, and registering custom skills
/// </summary>
public class SkillManager : Manager
{
/// <summary>
/// Global singleton instance of the manager
/// </summary>
public static SkillManager Instance { get; private set; }
internal Dictionary<Skills.SkillType, Skills.SkillDef> Skills = new Dictionary<Skills.SkillType, Skills.SkillDef>();
// FIXME: Deprecate
private int nextSkillId = 1000;
private void Awake()
{
if (Instance != null)
{
Debug.LogError("Error, two instances of singleton: " + this.GetType().Name);
return;
}
Instance = this;
}
/// <summary>
/// DEPRECATED DUE TO POSSIBLE CONFLICT ISSUE, see: <see href="https://github.com/jotunnlib/jotunnlib/issues/18">GitHub Issue</see>
/// <para/>
/// Register a new skill with given parameters, and registers translations for it in the current localization
/// </summary>
/// <param name="name">Name of the new skill</param>
/// <param name="description">Description of the new skill</param>
/// <param name="increaseStep"></param>
/// <param name="icon">Icon for the skill</param>
/// <returns>The SkillType of the newly registered skill</returns>
[Obsolete(
"Use `RegisterSkill(SkillConfig config)` instead. This method could potentially cause users to lose skill save progress",
true)]
public Skills.SkillType RegisterSkill(string name, string description, float increaseStep = 1f, Sprite icon = null)
{
LocalizationManager.Instance.RegisterTranslation("skill_" + nextSkillId, name);
LocalizationManager.Instance.RegisterTranslation("skill_" + nextSkillId + "_description", description);
Skills.SkillDef skillDef = new Skills.SkillDef()
{
m_skill = (Skills.SkillType)nextSkillId,
m_description = "$skill_" + nextSkillId + "_description",
m_increseStep = increaseStep, // nice they spelled increase wrong
m_icon = icon
};
Skills.Add((Skills.SkillType)nextSkillId, skillDef);
nextSkillId++;
return skillDef.m_skill;
}
/// <summary>
/// Register a new skill with given SkillConfig object, and registers translations for it in the current localization
/// </summary>
/// <param name="skillConfig">SkillConfig object representing new skill to register</param>
/// <returns>The SkillType of the newly registered skill</returns>
public Skills.SkillType RegisterSkill(SkillConfig skillConfig, bool registerLocalizations = true)
{
if (string.IsNullOrEmpty(skillConfig.Identifier))
{
Debug.LogError("Failed to register skill with invalid identifier: " + skillConfig.Identifier);
return global::Skills.SkillType.None;
}
if (Skills.ContainsKey(skillConfig.UID))
{
Debug.LogError("Failed to register skill with conflicting UID (m_skill): " + skillConfig.UID);
return global::Skills.SkillType.None;
}
if ((int)skillConfig.UID < 1000)
{
Debug.LogError("Failed to register skill with invalid UID (m_skill), please use a UID >= 1000: " + (int)skillConfig.UID);
return global::Skills.SkillType.None;
}
if (registerLocalizations)
{
LocalizationManager.Instance.RegisterTranslation("skill_" + skillConfig.UID, skillConfig.Name);
LocalizationManager.Instance.RegisterTranslation("skill_" + skillConfig.UID + "_description", skillConfig.Description);
}
Skills.SkillDef skillDef = new Skills.SkillDef()
{
m_skill = skillConfig.UID,
m_description = "$skill_" + skillConfig.UID + "_description",
m_increseStep = skillConfig.IncreaseStep,
m_icon = skillConfig.Icon
};
Skills.Add(skillConfig.UID, skillDef);
return skillDef.m_skill;
}
/// <summary>
/// Register a new skill with given parameters, and registers translations for it in the current localization
/// </summary>
/// <param name="identifer">Unique identifier of the new skill, ex: "com.jotunnlib.testmod.testskill"</param>
/// <param name="name">Name of the new skill</param>
/// <param name="description">Description of the new skill</param>
/// <param name="increaseStep"></param>
/// <param name="icon">Icon for the skill</param>
/// <returns>The SkillType of the newly registered skill</returns>
public Skills.SkillType RegisterSkill(
string identifer,
string name,
string description,
float increaseStep = 1f,
Sprite icon = null,
bool registerLocalizations = true)
{
return RegisterSkill(new SkillConfig()
{
Identifier = identifer,
Name = name,
Description = description,
IncreaseStep = increaseStep,
Icon = icon
}, registerLocalizations);
}
/// <summary>
/// Gets a custom skill with given SkillType
/// </summary>
/// <param name="skillType">SkillType to look for</param>
/// <returns>Custom skill with given SkillType</returns>
public Skills.SkillDef GetSkill(Skills.SkillType skillType)
{
if (Skills.ContainsKey(skillType))
{
return Skills[skillType];
}
if (Player.m_localPlayer != null)
{
if (Player.m_localPlayer.GetSkills() != null)
{
foreach (Skills.SkillDef skill in Player.m_localPlayer.GetSkills().m_skills)
{
if (skill.m_skill == skillType)
{
return skill;
}
}
}
}
return null;
}
/// <summary>
/// Gets a custom skill with given skill identifier
/// </summary>
/// <param name="identifier">String indentifer of SkillType to look for</param>
/// <returns>Custom skill with given SkillType</returns>
public Skills.SkillDef GetSkill(string identifier)
{
if (string.IsNullOrEmpty(identifier))
{
return null;
}
return GetSkill((Skills.SkillType)identifier.GetStableHashCode());
}
}
}