-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomBlock.cs
160 lines (136 loc) · 4.75 KB
/
CustomBlock.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
using System;
using System.Text.RegularExpressions;
using UnityEngine;
namespace CustomBlocks
{
[System.Serializable]
public class CustomBlock : MonoBehaviour
{
public static string nameTag = " [CustomBlock]";
public static string startMarker = " mbi::";
public static string endMarker = "::mbi";
public string layer;
public float alpha = 1f;
public Placeable placeable
{
get { return this.gameObject.GetComponent<Placeable>(); }
}
public PlaceableMetadata meta
{
get { return gameObject.GetComponent<PlaceableMetadata>(); }
}
void Awake()
{
Debug.Log("CustomBlockInfo Awake: " + this.gameObject.name);
if (meta.blockSerializeIndex < CustomBlocksMod.magicBackgroundBlockNumber)
{
meta.blockSerializeIndex += CustomBlocksMod.magicBackgroundBlockNumber;
}
ParseNameData();
TagName();
if (CustomBlocksMod.InFreePlace())
{
PlaceableHighlighter.HighlightAlpha(placeable);
} else
{
RestoreLayer();
SetCollide(false);
}
}
void OnDestroy()
{
Debug.Log("CustomBlockInfo destroyed: " + this.gameObject.name);
DeTagName();
if (meta.blockSerializeIndex >= CustomBlocksMod.magicBackgroundBlockNumber)
{
meta.blockSerializeIndex -= CustomBlocksMod.magicBackgroundBlockNumber;
}
this.SetLayer("Default", false);
this.SetCollide(true);
PlaceableHighlighter.ResetAlpha(this.placeable);
}
public void PersistInGOName()
{
if (!this.gameObject.name.Contains(startMarker))
{
this.gameObject.name += startMarker + this.ToJsonString() + endMarker;
}
}
public string ToJsonString()
{
return JsonUtility.ToJson(this);
}
public void ParseNameData()
{
try
{
if (this.gameObject.name.Contains(startMarker))
{
Regex rx = new Regex(startMarker + "(?<mbi>.*)" + endMarker);
MatchCollection matches = rx.Matches(this.gameObject.name);
if (matches.Count != 0)
{
var match = matches[0];
var json_data = match.Groups["mbi"].Value;
JsonUtility.FromJsonOverwrite(json_data, this);
ClearNameData();
}
}
}
catch (Exception e)
{
Debug.LogError("CustomBlockInfo parse failed: " + e);
}
}
public void ClearNameData()
{
this.gameObject.name = Regex.Replace(this.gameObject.name, startMarker + ".*" + endMarker, "");
}
public void TagName()
{
if (!this.name.Contains(nameTag))
{
this.name += nameTag;
}
}
public void DeTagName()
{
if (this.name.Contains(nameTag))
{
this.name = this.name.Replace(nameTag, "");
}
}
public void SetLayer(string layer, bool nochange = true)
{
Placeable pla = this.gameObject.GetComponent<Placeable>();
pla.dontChangeArtLayers = nochange;
foreach (SpriteRenderer spren in pla.gameObject.GetComponents<SpriteRenderer>())
{
spren.sortingLayerName = layer;
}
foreach (SpriteRenderer spren in pla.gameObject.GetComponentsInChildren<SpriteRenderer>())
{
spren.sortingLayerName = layer;
}
}
public void RestoreLayer()
{
Debug.Log("RestoreLayer: " + this.layer);
this.SetLayer(this.layer);
}
public void SetCollide(bool active)
{
this.gameObject.transform.Find("SolidCollider")?.transform.gameObject.SetActive(active);
this.gameObject.transform.Find("InnerHazard")?.transform.gameObject.SetActive(active);
// L Boards Collider misspelled, still evil for ᵉˡᵉᵇᵃⁿᵗ
if(GameState.GetInstance().currentSnapshotInfo.authorDisplayName != "ᵉˡᵉᵇᵃⁿᵗ")
{
this.gameObject.transform.Find("InnerHarzard")?.transform.gameObject.SetActive(active);
}
else
{
UserMessageManager.Instance.UserMessage("ᵉˡᵉᵇᵃⁿᵗ sends his regards!");
}
}
}
}