Skip to content

Commit f3280da

Browse files
committed
feat(core): add tutorial 50 code
1 parent 043a479 commit f3280da

File tree

9 files changed

+235
-13
lines changed

9 files changed

+235
-13
lines changed

‎Assets/Scripts/DebugConsole/DebugConsole.cs‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ private void Awake()
9494
b.SetConstructionHP(x);
9595
});
9696

97+
new DebugCommand<string>("unlock_tech", "Unlocks the given technology tree node.", "unlock_tech <code>", (x) =>
98+
{
99+
TechnologyNodeData node;
100+
if (TechnologyNodeData.TECH_TREE_NODES.TryGetValue(x, out node))
101+
node.Unlock();
102+
});
103+
97104
_displayType = DisplayType.None;
98105
}
99106

@@ -228,7 +235,12 @@ private void _HandleConsoleInput()
228235
Debug.LogError("Missing parameter!");
229236
return;
230237
}
231-
if (command is DebugCommand<int> dcInt)
238+
239+
if (command is DebugCommand<string> dcString)
240+
{
241+
dcString.Invoke(inputParts[1]);
242+
}
243+
else if (command is DebugCommand<int> dcInt)
232244
{
233245
int i;
234246
if (int.TryParse(inputParts[1], out i))
@@ -239,7 +251,7 @@ private void _HandleConsoleInput()
239251
return;
240252
}
241253
}
242-
if (command is DebugCommand<float> dcFloat)
254+
else if (command is DebugCommand<float> dcFloat)
243255
{
244256
float f;
245257
if (float.TryParse(inputParts[1], out f))
@@ -250,7 +262,7 @@ private void _HandleConsoleInput()
250262
return;
251263
}
252264
}
253-
if (command is DebugCommand<string, int> dcStringInt)
265+
else if (command is DebugCommand<string, int> dcStringInt)
254266
{
255267
int i;
256268
if (int.TryParse(inputParts[2], out i))
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
[CreateAssetMenu(
5+
fileName = "Technology Node",
6+
menuName = "Scriptable Objects/Technology Tree/Node")]
7+
public class TechnologyNodeData : ScriptableObject
8+
{
9+
public static string ROOT_NODE_CODE = "root";
10+
public static Dictionary<string, TechnologyNodeData> TECH_TREE_NODES;
11+
12+
public string code;
13+
public string displayName;
14+
public float researchTime;
15+
public List<ResourceValue> researchCost;
16+
public List<TechnologyNodeData> children;
17+
18+
private bool _unlocked;
19+
public bool Unlocked => _unlocked;
20+
21+
private TechnologyNodeData _parent;
22+
public TechnologyNodeData Parent => _parent;
23+
24+
public static void LoadTechnologyTree()
25+
{
26+
TECH_TREE_NODES = new Dictionary<string, TechnologyNodeData>();
27+
TechnologyNodeData[] nodesData = Resources.LoadAll<TechnologyNodeData>(
28+
"ScriptableObjects/TechnologyTree");
29+
30+
// (record parents for further re-assign)
31+
Dictionary<string, string> parents = new Dictionary<string, string>();
32+
foreach (TechnologyNodeData nodeData in nodesData)
33+
{
34+
// (reset unlocked flag, because the Unity Editor keeps the changes)
35+
nodeData._unlocked = false;
36+
37+
TECH_TREE_NODES[nodeData.code] = nodeData;
38+
foreach (TechnologyNodeData child in nodeData.children)
39+
parents[child.code] = nodeData.code;
40+
}
41+
42+
// re-assign parents after all nodes have been loaded and referenced
43+
foreach (KeyValuePair<string, string> p in parents)
44+
TECH_TREE_NODES[p.Key]._parent = TECH_TREE_NODES[p.Value];
45+
46+
// auto-unlock root
47+
TECH_TREE_NODES[ROOT_NODE_CODE]._unlocked = true;
48+
}
49+
50+
public static string[] GetUnlockedNodeCodes()
51+
{
52+
List<string> unlockedCodes = new List<string>();
53+
foreach (KeyValuePair<string, TechnologyNodeData> p in TECH_TREE_NODES)
54+
if (p.Value._unlocked)
55+
unlockedCodes.Add(p.Key);
56+
return unlockedCodes.ToArray();
57+
}
58+
59+
public static void SetUnlockedNodes(string[] unlockedNodeCodes)
60+
{
61+
foreach (string code in unlockedNodeCodes)
62+
if (TECH_TREE_NODES.ContainsKey(code))
63+
TECH_TREE_NODES[code].Unlock();
64+
}
65+
66+
public void Unlock()
67+
{
68+
// parent node not unlocked: this node is unreachable - abort!
69+
if (_parent != null && !_parent._unlocked) return;
70+
// node already unlocked - abort!
71+
if (_unlocked) return;
72+
73+
_unlocked = true;
74+
TechnologyNodeActioners.Apply(code);
75+
}
76+
77+
}

‎Assets/Scripts/ScriptableObjects/TechnologyNodeData.cs.meta‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Scripts/TechnologyTree.meta‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
public class TechnologyNodeActioners
5+
{
6+
7+
private static Dictionary<string, float> _MULTIPLIERS =
8+
new Dictionary<string, float>();
9+
10+
private static Dictionary<string, System.Action> _ACTIONERS =
11+
new Dictionary<string, System.Action>()
12+
{
13+
{ "attack_booster", () =>
14+
{
15+
// store multiplier for future units
16+
float multiplier = 2f;
17+
_MULTIPLIERS["attack_booster"] = multiplier;
18+
19+
// re-assign my current units attack, if there are any
20+
if (GameManager.instance == null) return;
21+
22+
int myPlayerId =
23+
GameManager.instance.gamePlayersParameters.myPlayerId;
24+
if (Unit.UNITS_BY_OWNER != null)
25+
foreach (Unit unit in Unit.UNITS_BY_OWNER[myPlayerId])
26+
unit.SetAttackDamage((int) (unit.AttackDamage * multiplier));
27+
} },
28+
{ "attack_booster_2", () =>
29+
{
30+
// store multiplier for future units
31+
float multiplier = 4f;
32+
_MULTIPLIERS["attack_booster"] = multiplier;
33+
34+
// re-assign my current units attack, if there are any
35+
if (GameManager.instance == null) return;
36+
37+
int myPlayerId =
38+
GameManager.instance.gamePlayersParameters.myPlayerId;
39+
if (Unit.UNITS_BY_OWNER != null)
40+
foreach (Unit unit in Unit.UNITS_BY_OWNER[myPlayerId])
41+
unit.SetAttackDamage((int) (unit.AttackDamage * multiplier));
42+
} },
43+
{ "cost_reducer_buy", () =>
44+
{
45+
// store multiplier for future buys
46+
_MULTIPLIERS["cost_reducer_buy"] = 0.9f;
47+
} },
48+
{ "cost_reducer_buy_2", () =>
49+
{
50+
// store multiplier for future buys
51+
_MULTIPLIERS["cost_reducer_buy"] = 0.8f;
52+
} },
53+
};
54+
55+
public static void Apply(string code)
56+
{
57+
System.Action action;
58+
if (_ACTIONERS.TryGetValue(code, out action))
59+
action();
60+
#if UNITY_EDITOR
61+
else
62+
Debug.LogWarning(
63+
$"No actioner defined for tech tree node '{code}' - can't apply!");
64+
#endif
65+
}
66+
67+
public static float GetMultiplier(string code)
68+
{
69+
float booster;
70+
if (_MULTIPLIERS.TryGetValue(code, out booster))
71+
return booster;
72+
else
73+
{
74+
#if UNITY_EDITOR
75+
Debug.LogWarning(
76+
$"No booster defined for tech tree node '{code}' - using default of 1");
77+
#endif
78+
return 1f;
79+
}
80+
}
81+
82+
}

‎Assets/Scripts/TechnologyTree/TechnologyNodeActioners.cs.meta‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Scripts/UI/UIManager.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
33
using System.Reflection;
44
using UnityEngine;
@@ -358,7 +358,12 @@ private void _OnHoveredSkillButton(object data)
358358
s.type == SkillType.INSTANTIATE_BUILDING
359359
)
360360
{
361-
cost = s.unitReference.cost;
361+
float costReducerBuy =
362+
TechnologyNodeActioners.GetMultiplier("cost_reducer_buy");
363+
foreach (ResourceValue rv in s.unitReference.cost)
364+
cost.Add(new ResourceValue(
365+
rv.code,
366+
(int) (rv.amount * costReducerBuy)));
362367
}
363368
SetInfoPanel(title, desc, cost);
364369
ShowInfoPanel(true);

‎Assets/Scripts/Units/Unit.cs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public Unit(UnitData data, int owner, List<ResourceValue> production)
5252
_production = production.ToDictionary(rv => rv.code, rv => rv.amount);
5353
_fieldOfView = data.fieldOfView;
5454
_owner = owner;
55-
_attackDamage = data.attackDamage;
55+
_attackDamage = (int) (
56+
data.attackDamage *
57+
TechnologyNodeActioners.GetMultiplier("attack_booster"));
5658
_attackRange = data.attackRange;
5759

5860
GameObject g = GameObject.Instantiate(data.prefab) as GameObject;
@@ -92,6 +94,12 @@ public void SetPosition(Vector3 position)
9294
_transform.position = position;
9395
}
9496

97+
public void SetAttackDamage(int dmg)
98+
{
99+
_attackDamage = dmg;
100+
_levelUpData = _GetLevelUpData();
101+
}
102+
95103
public Dictionary<InGameResource, int> ComputeProduction()
96104
{
97105
if (_data.canProduce.Length == 0) return null;
@@ -145,13 +153,16 @@ public virtual void Place(bool fromSavedData = false)
145153
// for collisions with units
146154
_transform.GetComponent<BoxCollider>().isTrigger = false;
147155

148-
// update game resources: remove the cost of the building
156+
// update game resources: remove the cost of the unit
149157
// from each game resource
150158
if (!fromSavedData)
151159
{
160+
float costReducerBuy =
161+
TechnologyNodeActioners.GetMultiplier("cost_reducer_buy");
152162
foreach (ResourceValue resource in _data.cost)
153163
{
154-
Globals.GAME_RESOURCES[_owner][resource.code].AddAmount(-resource.amount);
164+
Globals.GAME_RESOURCES[_owner][resource.code].AddAmount(
165+
(int) (-resource.amount * costReducerBuy));
155166
}
156167
EventManager.TriggerEvent("UpdatedResources");
157168
}

‎ProjectSettings/EditorSettings.asset‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
--- !u!159 &1
44
EditorSettings:
55
m_ObjectHideFlags: 0
6-
serializedVersion: 9
7-
m_ExternalVersionControlSupport: Visible Meta Files
6+
serializedVersion: 11
87
m_SerializationMode: 2
98
m_LineEndingsForNewScripts: 0
109
m_DefaultBehaviorMode: 0
@@ -18,18 +17,24 @@ EditorSettings:
1817
m_EtcTextureBestCompressor: 4
1918
m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd;asmdef;rsp;asmref
2019
m_ProjectGenerationRootNamespace:
21-
m_CollabEditorSettings:
22-
inProgressEnabled: 1
2320
m_EnableTextureStreamingInEditMode: 1
2421
m_EnableTextureStreamingInPlayMode: 1
2522
m_AsyncShaderCompilation: 1
23+
m_CachingShaderPreprocessor: 1
24+
m_PrefabModeAllowAutoSave: 1
2625
m_EnterPlayModeOptionsEnabled: 0
2726
m_EnterPlayModeOptions: 3
28-
m_ShowLightmapResolutionOverlay: 1
27+
m_GameObjectNamingDigits: 1
28+
m_GameObjectNamingScheme: 0
29+
m_AssetNamingUsesSpace: 1
2930
m_UseLegacyProbeSampleCount: 0
31+
m_SerializeInlineMappingsOnOneLine: 0
32+
m_DisableCookiesInLightmapper: 1
3033
m_AssetPipelineMode: 1
3134
m_CacheServerMode: 0
3235
m_CacheServerEndpoint:
3336
m_CacheServerNamespacePrefix: default
3437
m_CacheServerEnableDownload: 1
3538
m_CacheServerEnableUpload: 1
39+
m_CacheServerEnableAuth: 0
40+
m_CacheServerEnableTls: 0

0 commit comments

Comments
 (0)