-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBouncyCoins.cs
379 lines (330 loc) · 11.7 KB
/
BouncyCoins.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using System;
using System.Collections.Generic;
using System.Security.Policy;
using Terraria.ModLoader.IO;
namespace BouncyCoins
{
public class BouncyCoins : Mod
{
// (c) jofairden
// version 0.1.3
// Use a boolean to check if the appropriate mod is loaded
public bool LoadedFKTModSettings;
public static int[] VanillaCoinSet =
{
ItemID.CopperCoin, ItemID.SilverCoin, ItemID.GoldCoin, ItemID.PlatinumCoin
};
internal static BouncyCoins instance;
public BouncyCoins()
{
Properties = ModProperties.AutoLoadAll;
}
public override void Load()
{
instance = this;
Version reqVer = new Version(0, 8, 3, 5);
if (ModLoader.version < reqVer)
{
string message = "\nBouncy Coins uses a functionality only present in tModLoader version " + reqVer.ToString() + " or higher. " +
"Please update tModLoader to use this mod.\n\n";
throw new Exception(message);
}
// Mod Settings support
LoadedFKTModSettings = ModLoader.GetMod("FKTModSettings") != null;
if (LoadedFKTModSettings)
{
// Needs to be in a method otherwise it throws a namespace error
try { LoadModSettings(); }
catch { }
}
}
private void LoadModSettings()
{
FKTModSettings.ModSetting setting = FKTModSettings.ModSettingsAPI.CreateModSettingConfig(this);
// Enable auto-saving between sessions
setting.EnableAutoConfig();
setting.AddComment("Here you can configure settings for Bouncy Coins." +
" Please keep in mind these settings will only reflect on your client." +
" Also please keep in my the changes are only visual, to coin locations are never changed." +
" The settings are saved with the player.", 1f);
setting.AddComment("This setting will control the amplitude of the coin bounce.");
setting.AddDouble(
"bounceAmplitude",
"Bounce amplitude",
-100d,
100d,
true);
setting.AddComment("This setting will control the amplitude multiplier. Use this for even more finetuning");
setting.AddDouble(
"bounceAmpMult",
"Bounce amplitude multiplier",
-10d,
10d,
true);
setting.AddComment("Decides the speed of the coin bounce.");
setting.AddDouble(
"bounceSpeed",
"Bounce speed",
-1000f,
1000f,
true);
setting.AddComment("Decides the offset of the bounce.");
setting.AddDouble(
"bounceOffset",
"Bounce offset",
-80f,
80f,
true);
setting.AddComment("Decides whether the coins bounce evenly or individually.");
setting.AddBool(
"bounceEvenly",
"Bounce coins evenly?",
true);
setting.AddComment("Decides whether the bounce is calculated using cosine or sine.");
setting.AddBool(
"bounceByCosine",
"Bounce coins by cosine (true) or sine (false)?",
true);
setting.AddComment("Decides whether modded coins are affected or not.");
setting.AddBool(
"bounceModItems",
"Allow modded coins to bounce?",
true);
}
public override void PostUpdateInput()
{
if (LoadedFKTModSettings && !Main.gameMenu)
{
// Needs to be in a method otherwise it throws a namespace error
try
{
UpdateModSettings();
}
catch (Exception e)
{
Main.NewTextMultiline(e.ToString(), c: Color.Red);
}
}
}
private void UpdateModSettings()
{
FKTModSettings.ModSetting setting;
if (FKTModSettings.ModSettingsAPI.TryGetModSetting(this, out setting))
{
CoinPlayer player = CoinPlayer.GetModPlayer(Main.LocalPlayer);
setting.Get("bounceAmplitude", ref player.amplitude);
setting.Get("bounceAmpMult", ref player.ampMult);
setting.Get("bounceSpeed", ref player.speed);
setting.Get("bounceOffset", ref player.bounceOffset);
setting.Get("bounceEvenly", ref player.bounceEvenly);
setting.Get("bounceModItems", ref player.bounceModItems);
setting.Get("bouncebyCosine", ref player.byCosine);
}
}
}
public class CoinItem : GlobalItem
{
/*
* Is it a vanilla coin?
*/
internal static bool IsVanillaCoin(int type)
=> BouncyCoins.VanillaCoinSet.Any(x => x == type);
/*
* Bouncy coin logic here
*/
public override bool PreDrawInWorld(Item item, SpriteBatch spriteBatch, Color lightColor, Color alphaColor, ref float rotation, ref float scale, int whoAmI)
{
CoinPlayer player = CoinPlayer.GetModPlayer(Main.LocalPlayer);
// Do not bounce if any of these conditions are true
if ((!player.bounceModItems && item.modItem != null)
|| !player.bouncyItems.Contains(item.type)
|| item.velocity.Length() > 0f)
return true;
// Get coin texture
Texture2D texture = Main.itemTexture[item.type];
// Get appropiate keyframe
var keyFrame = (player.keyFrameActions.ContainsKey(item.type) && player.keyFrameActions[item.type] != null)
? player.keyFrameActions[item.type].Invoke(item, whoAmI)
: CoinPlayer.coinKeyFrameAction(item, whoAmI);
// Logic
Texture2D animTexture = keyFrame.AnimationTexture;
rotation = keyFrame.Rotation;
float offsetY = item.height - texture.Height;
float offsetX = item.width * 0.5f - texture.Width * 0.5f;
int frameHeight = keyFrame.FrameHeight;
// What is our angle for bouncing?
// If we bounce evenly, all coins bounce based on gametime
// If not, they will bounce based on their spawntime and whoAmI
int angle =
player.bounceEvenly
? (int)Main.time
: whoAmI % 60 + item.spawnTime;
//angle += (int)player.universalOffset;
Rectangle? frameRect = new Rectangle?(
new Rectangle(
0,
Main.itemFrame[whoAmI] * frameHeight + 1,
texture.Width,
frameHeight
));
// Do we want to bounce by cosine or sine?
double bounceByAngle =
player.byCosine
? Math.Cos(angle * player.speed / 1000)
: Math.Sin(angle * player.speed / 1000);
bounceByAngle *= player.amplitude * player.ampMult;
// Drawing
Vector2 center = new Vector2(0f, frameHeight * 0.5f);
Vector2 offset = new Vector2(0f, (float)bounceByAngle - (float)player.amplitude + (float)player.bounceOffset);
Vector2 pos = new Vector2(item.position.X - Main.screenPosition.X + animTexture.Width * 0.5f + offsetX,
item.position.Y - Main.screenPosition.Y + frameHeight * 0.5f + offsetY)
- center + offset;
Vector2 origin = new Vector2(texture.Width * 0.5f, frameHeight * 0.5f);
Main.spriteBatch.Draw(animTexture, pos, frameRect, alphaColor, rotation, origin, scale, SpriteEffects.None, 0f);
return false;
}
}
public class CoinPlayer : ModPlayer
{
public static CoinPlayer GetModPlayer(Player player)
=> player.GetModPlayer<CoinPlayer>(BouncyCoins.instance);
public delegate KeyFrameActionResult keyFrameActionDelegate(Item item, int whoAmI);
internal static keyFrameActionDelegate coinKeyFrameAction
=> GenerateBasicKeyFrameActionDelegate(1, 5, 8);
public class KeyFrameActionResult
{
public KeyFrameActionResult(Texture2D text, float rot, int height)
{
AnimationTexture = text;
Rotation = rot;
FrameHeight = height;
}
public Texture2D AnimationTexture { get; protected set; }
public float Rotation { get; protected set; }
public int FrameHeight { get; protected set; }
}
/// <summary>
/// Will generate a basic keyframeaction delegate for you, but if it's not a vanilla coin the offset returned will be 0.
/// </summary>
/// <param name="frameIncrement">How fast the itemFrameCounter increments</param>
/// <param name="maxFrameCount">The amount of frames itemFrameCounter will count to</param>
/// <param name="maxFrames">The maximum amount of frames</param>
/// <returns></returns>
public static keyFrameActionDelegate GenerateBasicKeyFrameActionDelegate(int frameIncrement, int maxFrameCount, int maxFrames)
{
return delegate (Item item, int whoAmI)
{
Main.itemFrameCounter[whoAmI] += frameIncrement;
if (Main.itemFrameCounter[whoAmI] > maxFrameCount)
{
Main.itemFrameCounter[whoAmI] = 0;
Main.itemFrame[whoAmI] = (Main.itemFrame[whoAmI] + 1) % maxFrames;
}
return CoinItem.IsVanillaCoin(item.type)
? new KeyFrameActionResult(Main.coinTexture[item.type - 71], item.velocity.X* 0.2f, Main.coinTexture[item.type - 71].Height / maxFrames)
: new KeyFrameActionResult(Main.itemTexture[item.type], 0f, Main.itemTexture[item.type].Height / maxFrames);
};
}
public void AddBouncyItem(int type, keyFrameActionDelegate keyFrameAction)
{
if (bouncyItems.Contains(type)) return;
bouncyItems.Add(type);
keyFrameActions.Add(type, keyFrameAction);
}
public bool RemoveBouncyItem(int type)
{
if (!bouncyItems.Contains(type)) return false;
bool b = bouncyItems.Remove(type);
return keyFrameActions.Remove(type) && b;
}
internal Version modVersion = new Version(0, 1, 3, 2);
internal string DeconstructModversion(Version v)
=> $"{v.Major}.{v.Minor}.{v.Build}.{v.Revision}";
internal Version ConstrucModversion(string v)
{
if (v.Length < 4)
throw new Exception("Version length was invalid.");
return new Version(v);
}
internal bool bounceEvenly; // bounce evenly?
internal bool bounceModItems; // do not bounce moditems?
internal bool byCosine; // bounce by cosine?
internal List<int> bouncyItems; // list of items that will bounce
internal double amplitude; // amp of bounce
internal double ampMult; // amp of bounce
internal double speed; // total speed of bounce
internal double bounceOffset; // some offset added to angle
internal Dictionary<int, keyFrameActionDelegate> keyFrameActions; // keyframing
public override void Initialize()
{
bounceEvenly = false;
bounceModItems = true;
byCosine = true;
bouncyItems = new List<int>(BouncyCoins.VanillaCoinSet);
amplitude = 10d;
ampMult = 1d;
speed = 75d;
bounceOffset = 5d;
keyFrameActions = new Dictionary<int, keyFrameActionDelegate>();
}
public override TagCompound Save()
{
return new TagCompound
{
["bounceEvenly"] = bounceEvenly,
["bounceModItems"] = bounceModItems,
["byCosine"] = byCosine,
["bouncyItems"] = new List<int>(bouncyItems),
["amplitude"] = amplitude,
["ampMult"] = ampMult,
["speed"] = speed,
["bounceOffset"] = bounceOffset,
["modVersion"] = DeconstructModversion(modVersion),
};
}
public override void Load(TagCompound tag)
{
// cross compat. from 0.1.3
if (tag.Any(x => x.Value is float))
{
if (tag.ContainsKey("amplitude"))
amplitude = (double)tag.GetAsShort("amplitude");
if (tag.ContainsKey("speed"))
speed = (double)tag.GetAsShort("speed");
if (tag.ContainsKey("universalOffset"))
bounceOffset = (double)tag.GetAsShort("universalOffset");
if (tag.ContainsKey("bounceEvenly"))
bounceEvenly = tag.Get<bool>("bounceEvenly");
if (tag.ContainsKey("disallowModItems"))
bounceModItems = !tag.Get<bool>("disallowModItems");
}
else
{
if (tag.ContainsKey("amplitude"))
amplitude = tag.GetAsDouble("amplitude");
if (tag.ContainsKey("ampMult"))
ampMult = tag.GetAsDouble("ampMult");
if (tag.ContainsKey("speed"))
speed = tag.GetAsDouble("speed");
if (tag.ContainsKey("bounceOffset"))
bounceOffset = tag.GetAsDouble("bounceOffset");
if (tag.ContainsKey("bounceEvenly"))
bounceEvenly = tag.Get<bool>("bounceEvenly");
if (tag.ContainsKey("bounceModItems"))
bounceModItems = tag.Get<bool>("bounceModItems");
if (tag.ContainsKey("byCosine"))
byCosine = tag.Get<bool>("byCosine");
if (tag.ContainsKey("modVersion"))
modVersion = ConstrucModversion(tag.GetString("modVersion"));
}
if (tag.ContainsKey("bouncyItems"))
bouncyItems = new List<int>(tag.GetList<int>("bouncyItems"));
}
}
}