/
BERiftWard.cs
215 lines (176 loc) · 6.99 KB
/
BERiftWard.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
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
using System.Text;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
using Vintagestory.API.Datastructures;
using Vintagestory.API.Server;
namespace Vintagestory.GameContent
{
public class BlockEntityRiftWard : BlockEntity
{
protected ICoreServerAPI sapi;
protected ILoadedSound ambientSound;
protected double fuelDays = 0;
protected double lastUpdateTotalDays = 0;
protected bool HasFuel => fuelDays > 0;
int riftsBlocked = 0;
public bool On { get; set; }
BlockEntityAnimationUtil animUtil
{
get { return GetBehavior<BEBehaviorAnimatable>().animUtil; }
}
public override void Initialize(ICoreAPI api)
{
base.Initialize(api);
sapi = api as ICoreServerAPI;
if (sapi != null)
{
api.ModLoader.GetModSystem<ModSystemRifts>().OnRiftSpawned += BlockEntityRiftWard_OnRiftSpawned;
RegisterGameTickListener(OnServerTick, 5000);
}
lastUpdateTotalDays = api.World.Calendar.TotalDays;
if (sapi == null) animUtil?.InitializeAnimator("riftward");
if (sapi == null && On)
{
Activate();
}
}
public void ToggleAmbientSound(bool on)
{
if (Api?.Side != EnumAppSide.Client) return;
if (on)
{
if (ambientSound == null || !ambientSound.IsPlaying)
{
ambientSound = ((IClientWorldAccessor)Api.World).LoadSound(new SoundParams()
{
Location = new AssetLocation("sounds/block/riftward.ogg"),
ShouldLoop = true,
Position = Pos.ToVec3f().Add(0.5f, 0.5f, 0.5f),
DisposeOnFinish = false,
Volume = 0f,
Range = 6,
SoundType = EnumSoundType.Ambient
});
if (ambientSound != null)
{
ambientSound.Start();
ambientSound.FadeTo(0.5f, 1f, (s)=> { });
ambientSound.PlaybackPosition = ambientSound.SoundLengthSeconds * (float)Api.World.Rand.NextDouble();
}
} else
{
if (ambientSound.IsPlaying) ambientSound.FadeTo(0.5f, 1f, (s) => { });
}
}
else
{
ambientSound?.FadeOut(0.5f, (s) => { s.Dispose(); ambientSound = null; });
}
}
private void OnServerTick(float dt)
{
if (On)
{
double dayspassed = Api.World.Calendar.TotalDays - lastUpdateTotalDays;
fuelDays -= dayspassed;
MarkDirty(false);
}
if (!HasFuel)
{
Deactivate();
}
lastUpdateTotalDays = Api.World.Calendar.TotalDays;
}
public void Activate()
{
if (!HasFuel || Api == null) return;
On = true;
lastUpdateTotalDays = Api.World.Calendar.TotalDays;
animUtil?.StartAnimation(new AnimationMetaData() { Animation = "on-spin", Code = "on-spin", EaseInSpeed = 1, EaseOutSpeed = 2, AnimationSpeed = 1f });
MarkDirty(true);
ToggleAmbientSound(true);
}
public void Deactivate()
{
animUtil?.StopAnimation("on-spin");
On = false;
ToggleAmbientSound(false);
MarkDirty(true);
}
public bool OnInteract(BlockSelection blockSel, IPlayer byPlayer)
{
var slot = byPlayer.InventoryManager.ActiveHotbarSlot;
if (slot.Empty)
{
Api.World.PlaySoundAt(new AssetLocation("sounds/toggleswitch"), Pos.X + 0.5, Pos.Y + 0.5, Pos.Z + 0.5, byPlayer, false, 16);
if (On) Deactivate();
else Activate();
return true;
}
if (slot.Itemstack.ItemAttributes?.IsTrue("riftwardFuel") == true && fuelDays < 0.5)
{
fuelDays += slot.Itemstack.ItemAttributes["rifwardfuelDays"].AsDouble(14);
slot.TakeOut(1);
(Api as ICoreClientAPI)?.World.Player.TriggerFpAnimation(EnumHandInteract.HeldItemInteract);
Activate();
}
return true;
}
private void BlockEntityRiftWard_OnRiftSpawned(Rift rift)
{
// Instead of preventing rift spawn, we set the size to 0, which makes it invisible and inactive,
// so as to still consume a "rift slot"
if (HasFuel && sapi.World.Rand.NextDouble() <= 0.95 && rift.Position.DistanceTo(Pos.X + 0.5, Pos.Y + 1, Pos.Z + 0.5) < 30)
{
rift.Size = 0;
riftsBlocked++;
MarkDirty();
}
}
public override void OnBlockUnloaded()
{
base.OnBlockUnloaded();
if (sapi != null)
{
sapi.ModLoader.GetModSystem<ModSystemRifts>().OnRiftSpawned -= BlockEntityRiftWard_OnRiftSpawned;
}
ambientSound?.Dispose();
}
public override void OnBlockRemoved()
{
base.OnBlockRemoved();
ambientSound?.Dispose();
}
public override void FromTreeAttributes(ITreeAttribute tree, IWorldAccessor worldAccessForResolve)
{
base.FromTreeAttributes(tree, worldAccessForResolve);
fuelDays = tree.GetDouble("fuelUntilTotalDays", 0);
lastUpdateTotalDays = tree.GetDouble("lastUpdateTotalDays");
On = tree.GetBool("on");
riftsBlocked = tree.GetInt("riftsBlocked");
if (On) Activate();
else Deactivate();
}
public override void ToTreeAttributes(ITreeAttribute tree)
{
base.ToTreeAttributes(tree);
tree.SetDouble("lastUpdateTotalDays", lastUpdateTotalDays);
tree.SetDouble("fuelUntilTotalDays", fuelDays);
tree.SetBool("on", On);
tree.SetInt("riftsBlocked", riftsBlocked);
}
public override void GetBlockInfo(IPlayer forPlayer, StringBuilder dsc)
{
base.GetBlockInfo(forPlayer, dsc);
if (fuelDays <= 0)
{
dsc.AppendLine(Lang.Get("Out of power. Recharge with temporal gears."));
} else
{
dsc.AppendLine(Lang.Get("Charge for {0:0.#} days", fuelDays));
}
dsc.AppendLine(Lang.Get("Rifts blocked: {0}", riftsBlocked));
}
}
}