forked from romen-h/ONI-Mods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDumpingSign.cs
More file actions
232 lines (192 loc) · 6.37 KB
/
DumpingSign.cs
File metadata and controls
232 lines (192 loc) · 6.37 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections.Generic;
using System.Linq;
using RomenH.Common;
using UnityEngine;
namespace RomenH.DumpingSign
{
public class DumpingSign : KMonoBehaviour
{
[MyCmpReq]
private Storage storage;
[MyCmpGet]
private KBatchedAnimController anim;
[MyCmpGet]
private TreeFilterable filterable;
protected FilteredStorage filteredStorage;
private GameObject signObject;
private KBatchedAnimController signAnim;
//private static readonly KAnimFile genericIconsKanim = Assets.GetAnim("dumping_sign_icons_kanim");
private static readonly Dictionary<Tag, KAnimFile> categoryKanims = new Dictionary<Tag, KAnimFile>();
private static readonly Dictionary<Tag, Vector2> iconOffsets = new Dictionary<Tag, Vector2>();
private static readonly Dictionary<Tag, Vector2> iconScales = new Dictionary<Tag, Vector2>();
private static bool init = false;
private static void Init()
{
if (init) return;
categoryKanims[GameTags.Agriculture] = Assets.GetAnim("farmtilerotating_kanim");
categoryKanims[GameTags.BuildableProcessed] = Assets.GetAnim("rockrefinery_kanim");
categoryKanims[GameTags.Clothes] = Assets.GetAnim("clothes_kanim");
categoryKanims[GameTags.Compostable] = Assets.GetAnim("compost_kanim");
categoryKanims[GameTags.Egg] = Assets.GetAnim("incubator_kanim");
categoryKanims[GameTags.Farmable] = Assets.GetAnim("farmtilerotating_kanim");
categoryKanims[GameTags.Filter] = Assets.GetAnim("waterpurifier_kanim");
categoryKanims[GameTags.IndustrialProduct] = Assets.GetAnim("metalrefinery_kanim");
categoryKanims[GameTags.Liquifiable] = Assets.GetAnim("liquid_tank_kanim");
categoryKanims[GameTags.ManufacturedMaterial] = Assets.GetAnim("supermaterial_refinery_kanim");
categoryKanims[GameTags.Metal] = Assets.GetAnim("metalrefinery_kanim");
categoryKanims[GameTags.RefinedMetal] = Assets.GetAnim("metalrefinery_kanim");
categoryKanims[GameTags.RareMaterials] = Assets.GetAnim("supermaterial_refinery_kanim");
categoryKanims[GameTags.Seed] = Assets.GetAnim("farmtilerotating_kanim");
init = true;
}
protected override void OnPrefabInit()
{
Init();
ChoreType choreType = Db.Get().ChoreTypes.Get(Db.Get().ChoreTypes.StorageFetch.Id);
filteredStorage = new FilteredStorage(this, null, null, false, choreType);
filteredStorage.SetHasMeter(false);
}
protected override void OnSpawn()
{
anim.SetSymbolVisiblity("item_target", false);
Vector3 pos = this.transform.position;
signObject = new GameObject();
signObject.transform.parent = this.gameObject.transform;
signObject.transform.position = new Vector3(pos.x, pos.y + 0.66f, pos.z - 0.1f);
signObject.SetActive(false);
signAnim = signObject.AddComponent<KBatchedAnimController>();
signAnim.AnimFiles = new KAnimFile[] { Assets.GetAnim("egg_hatch_fixed_kanim") };
signAnim.initialAnim = "ui";
signAnim.sceneLayer = Grid.SceneLayer.BuildingFront;
signAnim.animScale *= 0.33f;
signObject.SetActive(true);
signAnim.enabled = false;
filterable.OnFilterChanged = (Action<HashSet<Tag>>)Delegate.Combine(filterable.OnFilterChanged, new Action<HashSet<Tag>>(this.OnFilterChanged));
storage.SetOnlyFetchMarkedItems(true);
Subscribe((int)GameHashes.OnStorageChange, OnStorageChanged);
Refresh();
}
protected override void OnCleanUp()
{
filterable.OnFilterChanged = (Action<HashSet<Tag>>)Delegate.Remove(filterable.OnFilterChanged, new Action<HashSet<Tag>>(this.OnFilterChanged));
filteredStorage.CleanUp();
GameObject.DestroyImmediate(signObject);
}
private void OnStorageChanged(object data)
{
storage.DropAll(vent_gas: true, dump_liquid: true);
}
private void Refresh()
{
OnFilterChanged(filterable.GetTags());
}
private void OnFilterChanged(HashSet<Tag> tags)
{
try
{
if (tags == null || tags.Count == 0)
{
anim.SetSymbolVisiblity("disabled", true);
signAnim.enabled = false;
}
else
{
anim.SetSymbolVisiblity("disabled", false);
KAnimFile[] animFiles = null;
string animToPlay = null;
Vector2 offset = Vector2.zero;
float scale = 0.0015f;
if (tags.Count == 1)
{
var prefab = Assets.GetPrefab(tags.First());
if (prefab != null)
{
animFiles = prefab.GetComponent<KBatchedAnimController>()?.AnimFiles;
animToPlay = "ui";
var id = prefab.GetComponent<KPrefabID>();
if (id.HasTag(GameTags.Seed))
{
scale *= 1.5f;
}
else if (id.HasTag(GameTags.Egg))
{
scale *= 1.5f;
offset = new Vector2(0, -0.2f);
}
}
}
else
{
int discoveredCount = DiscoveredResources.Instance.GetDiscovered().Count;
if (tags.Count != discoveredCount)
{
Tag category = GetMajorityCategory(tags);
if (categoryKanims.TryGetValue(category, out KAnimFile kanim))
{
if (kanim != null)
{
animFiles = new KAnimFile[] { kanim };
animToPlay = "ui";
offset = new Vector2(0, -0.2f);
scale *= 1.25f;
}
}
}
}
if (animFiles == null)
{
animFiles = new KAnimFile[] { Assets.GetAnim("box_kanim") };
animToPlay = "ui";
offset = new Vector2(0, -0.2f);
scale *= 1.5f;
}
if (animFiles != null && animFiles.Length > 0 && animFiles[0] != null)
{
signAnim.SwapAnims(animFiles);
signAnim.Play(animToPlay);
signAnim.Offset = offset;
signAnim.animScale = scale;
}
else
{
Debug.LogWarning("DumpingSign: Failed to find a valid animation for the selected tags. Icon did not update.");
}
signAnim.enabled = true;
}
}
catch (Exception ex)
{
Debug.LogWarning("DumpingSign: Caught error in OnFilterChanged.");
Debug.LogException(ex);
}
}
private Tag GetMajorityCategory(HashSet<Tag> tags)
{
Tag majorityCategory = Tag.Invalid;
foreach (Tag category in storage.storageFilters)
{
int count = GetNumTagsFromCategory(category, tags);
bool majority = count > (tags.Count - count);
if (majority)
{
majorityCategory = category;
break;
}
}
return majorityCategory;
}
private int GetNumTagsFromCategory(Tag category, HashSet<Tag> tags)
{
int c = 0;
foreach (Tag tag in DiscoveredResources.Instance.GetDiscoveredResourcesFromTag(category))
{
if (tags.Contains(tag))
{
c++;
}
}
return c;
}
}
}