-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChunkLoaderMono.cs
185 lines (161 loc) · 6.81 KB
/
ChunkLoaderMono.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
179
180
181
182
183
184
185
namespace ChunkLoader;
public class ChunkLoaderMono : SlowUpdate, Hoverable, Interactable
{
public const string m_name = "$piece_ChunkLoader_stone";
public const string burningZDOKey = "IsBurning";
private static readonly float disabledEmission = 6;
public static float m_startFuel = 1f;
public static float m_maxFuel = 100f;
public static bool m_infiniteFuel;
public static ItemDrop m_fuelItem;
public static Color flashColor;
public static int minutesForOneFuelItem = 5;
public ZNetView m_nview;
public Piece m_piece;
public EffectList m_fuelAddedEffects = new();
public float m_holdRepeatInterval = 0.2f;
public float m_lastUseTime;
private Renderer m_renderer;
private Color matColor = Color.clear;
public override void Awake()
{
base.Awake();
m_fuelItem = ObjectDB.instance.GetItem("Thunderstone");
m_nview = gameObject.GetComponent<ZNetView>();
m_piece = gameObject.GetComponent<Piece>();
if (!m_nview || m_nview.m_ghost) return;
if (m_nview.GetZDO() == null) return;
if (m_nview.IsOwner() && m_nview.GetZDO().GetFloat(ZDOVars.s_fuel, -1f) == -1)
{
m_nview.GetZDO().Set(ZDOVars.s_fuel, m_startFuel);
if (m_startFuel > 0) m_fuelAddedEffects.Create(transform.position, transform.rotation);
}
m_nview.Register("AddFuel", RPC_AddFuel);
InvokeRepeating(nameof(UpdateFireplace), 0, 2f);
m_renderer = transform.FindChildByName("SM_cloumn").GetComponent<Renderer>();
if (m_renderer && m_renderer.material.HasProperty("_EmissionColor"))
matColor = m_renderer.material.GetColor("_EmissionColor");
}
public string GetHoverText()
{
if (!m_nview || m_nview.m_ghost || !m_nview.IsValid()) return string.Empty;
var result = m_name;
if (!m_infiniteFuel)
result += " ( $piece_fire_fuel "
+ Mathf.Ceil(m_nview.GetZDO().GetFloat(ZDOVars.s_fuel)) + "/"
+ (int)m_maxFuel
+ " )\n[<color=yellow><b>$KEY_Use</b></color>] $piece_use "
+ m_fuelItem.m_itemData.m_shared.m_name
+ "\n[<color=yellow><b>1-8</b></color>] $piece_useitem";
result += "\n[<color=yellow><b>$KEY_AltPlace</b></color>] $showChunkArea";
return result.Localize();
}
public string GetHoverName() { return m_name; }
public bool Interact(Humanoid user, bool hold, bool alt)
{
if (hold && (m_holdRepeatInterval <= 0.0 || Time.time - m_lastUseTime < m_holdRepeatInterval))
return false;
if (!m_nview.HasOwner()) m_nview.ClaimOwnership();
if (alt)
{
FindHeightmap(transform.position).m_meshRenderer.Flash(flashColor, Color.white, 1.5f);
return true;
}
var inventory = user.GetInventory();
if (inventory == null) return true;
if (m_infiniteFuel) return false;
if (inventory.HaveItem(m_fuelItem.m_itemData.m_shared.m_name))
{
if (Mathf.CeilToInt(m_nview.GetZDO().GetFloat(ZDOVars.s_fuel)) >= m_maxFuel)
{
user.Message(Center,
Localization.instance.Localize("$msg_cantaddmore", m_fuelItem.m_itemData.m_shared.m_name));
return false;
}
user.Message(Center,
Localization.instance.Localize("$msg_fireadding", m_fuelItem.m_itemData.m_shared.m_name));
inventory.RemoveItem(m_fuelItem.m_itemData.m_shared.m_name, 1);
m_nview.InvokeRPC("AddFuel");
return true;
}
user.Message(Center, "$msg_outof " + m_fuelItem.m_itemData.m_shared.m_name);
return false;
}
public bool UseItem(Humanoid user, ItemData item)
{
if (item.m_shared.m_name == m_fuelItem.m_itemData.m_shared.m_name && !m_infiniteFuel)
{
if (Mathf.CeilToInt(m_nview.GetZDO().GetFloat(ZDOVars.s_fuel)) >= m_maxFuel)
{
user.Message(Center,
Localization.instance.Localize("$msg_cantaddmore", item.m_shared.m_name));
return true;
}
var inventory = user.GetInventory();
user.Message(Center,
Localization.instance.Localize("$msg_fireadding", item.m_shared.m_name));
inventory.RemoveItem(item, 1);
m_nview.InvokeRPC("AddFuel");
return true;
}
return false;
}
public override void SUpdate()
{
var zone = transform.position.GetZone();
if (IsBurning())
{
if (m_renderer && matColor != Color.clear && m_renderer.material.HasProperty("_EmissionColor"))
m_renderer.material.SetColor("_EmissionColor", matColor);
m_nview.GetZDO().Set(burningZDOKey, true);
} else
{
if (m_renderer && m_renderer.material.HasProperty("_EmissionColor"))
m_renderer.material.SetColor("_EmissionColor", Color.red * disabledEmission);
m_nview.GetZDO().Set(burningZDOKey, false);
}
}
public bool IsBurning()
{
if (!m_nview || m_nview.m_ghost) return false;
return m_infiniteFuel || m_nview.GetZDO().GetFloat(ZDOVars.s_fuel) > 0.0;
}
public void RPC_AddFuel(long sender)
{
if (!m_nview.IsOwner()) return;
var f = m_nview.GetZDO().GetFloat(ZDOVars.s_fuel);
if (Mathf.CeilToInt(f) >= m_maxFuel) return;
var num = Mathf.Clamp(Mathf.Clamp(f, 0, m_maxFuel) + 1f, 0, m_maxFuel);
m_nview.GetZDO().Set(ZDOVars.s_fuel, num);
m_fuelAddedEffects.Create(transform.position, transform.rotation);
SUpdate();
}
public void UpdateFireplace()
{
if (!m_nview.IsValid()) return;
var secPerFuel = TimeSpan.FromMinutes(minutesForOneFuelItem).TotalSeconds;
if (m_nview.IsOwner() && secPerFuel > 0)
{
var savedFuel = m_nview.GetZDO().GetFloat(ZDOVars.s_fuel);
var timeSinceLastUpdate = GetTimeSinceLastUpdate();
if (IsBurning() && !m_infiniteFuel)
{
var fuelSpent = timeSinceLastUpdate / secPerFuel;
var newFuel = savedFuel - fuelSpent;
if (newFuel <= 0.0) newFuel = 0;
m_nview.GetZDO().Set(ZDOVars.s_fuel, (float)newFuel);
}
}
SUpdate();
}
public double GetTimeSinceLastUpdate()
{
var time = ZNet.instance.GetTime();
DateTime dateTime = new(m_nview.GetZDO().GetLong(ZDOVars.s_lastTime, time.Ticks));
var timeSpan = time - dateTime;
m_nview.GetZDO().Set(ZDOVars.s_lastTime, time.Ticks);
var timeSinceLastUpdate = timeSpan.TotalSeconds;
if (timeSinceLastUpdate < 0) timeSinceLastUpdate = 0;
return timeSinceLastUpdate;
}
}