-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
GradiusModPlayer.cs
563 lines (508 loc) · 22.4 KB
/
GradiusModPlayer.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
using ChensGradiusMod.Items;
using ChensGradiusMod.Items.Accessories.Options.Charge;
using ChensGradiusMod.Items.Accessories.Options.Recurve;
using ChensGradiusMod.Items.Accessories.Options.Rotate;
using ChensGradiusMod.Projectiles.Forces;
using ChensGradiusMod.Projectiles.Options;
using ChensGradiusMod.Projectiles.Options.Miscellaneous;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using Terraria;
using Terraria.GameInput;
using Terraria.ModLoader;
using static ChensGradiusMod.GradiusHelper;
namespace ChensGradiusMod
{
public class GradiusModPlayer : ModPlayer
{
private const int MaxFlightPathCount = 60;
private const float EquiAngle = FullAngle / MaxFlightPathCount;
private Vector2 baitPoint;
private int baitDirection;
private float baitAngle;
public int rotateMode;
public sbyte revolveDirection;
public bool wasHolding;
public bool isFreezing;
public bool isAiming;
public bool isSearching;
public bool forceBase;
public bool needleForce;
public Projectile forceProjectile;
public bool optionOne;
public bool optionTwo;
public bool optionThree;
public bool optionFour;
public bool normalOption;
public bool freezeOption;
public bool aimOption;
public bool rotateOption;
public bool searchOption;
public bool chargeMultiple;
public int chargeMode;
public bool optionSeed;
public Projectile seedProjectile;
public sbyte seedRotateDirection;
public bool recurveOption;
public bool recurveSide;
public bool recurveActionMode;
public bool isRecurving;
public float recurveDistance;
public bool spreadOption;
public bool isSpreading;
public bool turretOption;
public bool isTurreting;
public Item[] optionRuleAmmoFilter = new Item[2];
public List<Vector2> optionFlightPath = new List<Vector2>();
public List<int> optionAlreadyProducedProjectiles = new List<int>();
public GradiusModPlayer() => UpdateDead();
public override void ResetEffects()
{
forceBase = false;
needleForce = false;
optionOne = false;
optionTwo = false;
optionThree = false;
optionFour = false;
optionSeed = false;
OptionFlagReset();
optionRuleAmmoFilter[0] = new Item();
optionRuleAmmoFilter[1] = new Item();
}
public override void UpdateDead()
{
ResetEffects();
ResetOptionVariables();
ResetOtherVariables();
}
public override void clientClone(ModPlayer clientClone)
{
GradiusModPlayer clone = clientClone as GradiusModPlayer;
clone.isFreezing = isFreezing;
clone.rotateMode = rotateMode;
clone.seedRotateDirection = seedRotateDirection;
clone.chargeMode = chargeMode;
clone.isSearching = isSearching;
clone.isTurreting = isTurreting;
}
public override void SyncPlayer(int toWho, int fromWho, bool newPlayer)
{
ModPacket packet = mod.GetPacket();
packet.Write((byte)ChensGradiusMod.PacketMessageType.GradiusModSyncPlayer);
packet.Write((byte)player.whoAmI);
packet.Write(isFreezing);
packet.Write(rotateMode);
packet.Write(revolveDirection);
packet.Write(wasHolding);
packet.Write(forceBase);
packet.Write(needleForce);
packet.Write(optionOne);
packet.Write(optionTwo);
packet.Write(optionThree);
packet.Write(optionFour);
packet.Write(normalOption);
packet.Write(freezeOption);
packet.Write(rotateOption);
packet.Write(optionSeed);
packet.Write(seedRotateDirection);
packet.Write(chargeMultiple);
packet.Write(chargeMode);
packet.Write(aimOption);
packet.Write(searchOption);
packet.Write(isSearching);
packet.Write(recurveOption);
packet.Write(spreadOption);
packet.Write(turretOption);
packet.Write(isTurreting);
packet.Send(toWho, fromWho);
}
public override void SendClientChanges(ModPlayer clientPlayer)
{
if (clientPlayer is GradiusModPlayer clone)
{
ModPacket packet;
if (clone.isFreezing != isFreezing)
{
packet = mod.GetPacket();
packet.Write((byte)ChensGradiusMod.PacketMessageType.ClientChangesFreezeOption);
packet.Write((byte)player.whoAmI);
packet.Write(isFreezing);
packet.Write(wasHolding);
packet.Send();
}
if (clone.rotateMode != rotateMode)
{
packet = mod.GetPacket();
packet.Write((byte)ChensGradiusMod.PacketMessageType.ClientChangesRotateOption);
packet.Write((byte)player.whoAmI);
packet.Write(rotateMode);
packet.Write(revolveDirection);
packet.Write(wasHolding);
packet.Send();
}
if (clone.seedRotateDirection != seedRotateDirection)
{
packet = mod.GetPacket();
packet.Write((byte)ChensGradiusMod.PacketMessageType.ClientChangesSeedDirection);
packet.Write((byte)player.whoAmI);
packet.Write(seedRotateDirection);
packet.Send();
}
if (clone.chargeMode != chargeMode)
{
packet = mod.GetPacket();
packet.Write((byte)ChensGradiusMod.PacketMessageType.ClientChangesChargeMultiple);
packet.Write((byte)player.whoAmI);
packet.Write(chargeMode);
packet.Write(wasHolding);
packet.Send();
}
if (clone.isSearching != isSearching)
{
packet = mod.GetPacket();
packet.Write((byte)ChensGradiusMod.PacketMessageType.ClientChangesSearchOption);
packet.Write((byte)player.whoAmI);
packet.Write(isSearching);
packet.Write(wasHolding);
packet.Send();
}
if (clone.isTurreting != isTurreting)
{
packet = mod.GetPacket();
packet.Write((byte)ChensGradiusMod.PacketMessageType.ClientChangesTurretOption);
packet.Write((byte)player.whoAmI);
packet.Write(isTurreting);
packet.Write(wasHolding);
packet.Send();
}
}
}
public override void ProcessTriggers(TriggersSet triggersSet)
{
if (HasAForce() && ChensGradiusMod.forceActionKey.JustReleased &&
forceProjectile.modProjectile is ForceBase fbProj)
{
switch (fbProj.mode)
{
case (int)ForceBase.States.Attached:
fbProj.mode = (int)ForceBase.States.Launched;
fbProj.SpecialDetachActions();
Main.PlaySound(mod.GetLegacySoundSlot(SoundType.Custom, "Sounds/Forces/ForceOut"),
forceProjectile.Center);
break;
case (int)ForceBase.States.Detached:
forceProjectile.tileCollide = false;
forceProjectile.velocity = new Vector2();
fbProj.mode = (int)ForceBase.States.Pulled;
break;
case (int)ForceBase.States.Launched:
case (int)ForceBase.States.Pulled:
break;
}
}
if (freezeOption)
{
if (ChensGradiusMod.optionActionKey.JustPressed) wasHolding = isFreezing = true;
if (ChensGradiusMod.optionActionKey.JustReleased && wasHolding) wasHolding = isFreezing = false;
}
else if (aimOption)
{
if (ChensGradiusMod.optionActionKey.JustPressed) wasHolding = isAiming = true;
if (ChensGradiusMod.optionActionKey.JustReleased && wasHolding) wasHolding = isAiming = false;
}
else if (rotateOption)
{
if (ChensGradiusMod.optionActionKey.JustPressed)
{
wasHolding = true;
rotateMode = (int)RotateOptionBase.States.Grouping;
}
if (ChensGradiusMod.optionActionKey.JustReleased && wasHolding)
{
wasHolding = false;
rotateMode = (int)RotateOptionBase.States.Recovering;
revolveDirection = (sbyte)-revolveDirection;
}
}
else if (searchOption)
{
if (ChensGradiusMod.optionActionKey.JustPressed) wasHolding = isSearching = true;
if (ChensGradiusMod.optionActionKey.JustReleased && wasHolding) wasHolding = isSearching = false;
}
else if (chargeMultiple && HasAnyChargeMultipleAccessory())
{
if (ChensGradiusMod.optionActionKey.JustPressed)
{
wasHolding = true;
chargeMode = (int)ChargeMultipleBase.States.Charging;
}
if (ChensGradiusMod.optionActionKey.JustReleased && wasHolding)
{
wasHolding = false;
chargeMode = (int)ChargeMultipleBase.States.Dying;
}
}
else if (recurveOption)
{
if (ChensGradiusMod.optionActionKey.JustPressed) wasHolding = isRecurving = true;
if (ChensGradiusMod.optionActionKey.JustReleased && wasHolding)
{
wasHolding = isRecurving = false;
recurveActionMode = !recurveActionMode;
}
}
else if (spreadOption)
{
if (ChensGradiusMod.optionActionKey.JustPressed) wasHolding = isSpreading = true;
if (ChensGradiusMod.optionActionKey.JustReleased && wasHolding) wasHolding = isSpreading = false;
}
else if (turretOption)
{
if (ChensGradiusMod.optionActionKey.JustPressed) wasHolding = isTurreting = true;
if (ChensGradiusMod.optionActionKey.JustReleased && wasHolding) wasHolding = isTurreting = false;
}
}
public override void PreUpdate()
{
if (HasAnyOptions())
{
if (optionFlightPath.Count > 0)
{
for (int h = 0; h < optionAlreadyProducedProjectiles.Count; h++)
{
Projectile p = Main.projectile[optionAlreadyProducedProjectiles[h]];
if (!p.active) optionAlreadyProducedProjectiles.RemoveAt(h--);
}
bool isRotateButNotFollowing = rotateMode != (int)RotateOptionBase.States.Following;
if (!recurveOption && (!IsEqualWithThreshold(optionFlightPath[0], player.Center, .01f)
|| isRotateButNotFollowing))
{
if (optionFlightPath.Count >= MaxFlightPathCount) optionFlightPath.RemoveAt(optionFlightPath.Count - 1);
if (freezeOption && isFreezing) FreezeBehavior();
else if (rotateOption && isRotateButNotFollowing)
{
switch (rotateMode)
{
case (int)RotateOptionBase.States.Grouping:
if (baitDirection == 0)
{
baitPoint = player.Center;
baitDirection = player.direction;
baitAngle = baitDirection > 0 ? 0 : 180;
}
RotateBehaviorGrouping();
break;
case (int)RotateOptionBase.States.Rotating:
RotateBehaviorRevolving();
break;
case (int)RotateOptionBase.States.Recovering:
RotateBehaviorRecovering();
break;
}
}
else optionFlightPath.Insert(0, player.Center);
}
else if (recurveOption) RecurveBehavior();
}
else optionFlightPath.Insert(0, player.Center);
}
else ResetOptionVariables();
}
public override void PostUpdate()
{
if (HasAnyOptions()) FreeListData(ref optionAlreadyProducedProjectiles);
if (GradiusGlobalItem.meleeHitbox[player.whoAmI].HasValue) GradiusGlobalItem.meleeHitbox[player.whoAmI] = null;
}
public override void OnHitNPC(Item item, NPC target, int damage, float knockback, bool crit)
{
MakeForceBattle();
MakeOptionSeedBattle();
}
public override void OnHitNPCWithProj(Projectile proj, NPC target, int damage, float knockback, bool crit)
{
MakeForceBattle();
MakeOptionSeedBattle();
}
public override void OnConsumeAmmo(Item weapon, Item ammo)
{
optionRuleAmmoFilter[0] = weapon;
optionRuleAmmoFilter[1] = ammo;
}
public void PopulateOptionFlight()
{
while (optionFlightPath.Count < MaxFlightPathCount)
{
optionFlightPath.Add(player.Center);
}
}
private void ResetOptionVariables()
{
optionFlightPath.Clear();
optionFlightPath = new List<Vector2>();
optionAlreadyProducedProjectiles.Clear();
optionAlreadyProducedProjectiles = new List<int>();
isFreezing = false;
isAiming = false;
rotateMode = (int)RotateOptionBase.States.Following;
baitDirection = 0;
revolveDirection = 1;
wasHolding = false;
chargeMode = (int)ChargeMultipleBase.States.Following;
isSearching = false;
recurveActionMode = false;
recurveDistance = 96f;
isSpreading = false;
isTurreting = false;
}
private void ResetOtherVariables()
{
seedRotateDirection = 0;
recurveSide = false;
}
private void OptionFlagReset()
{
normalOption = false;
rotateOption = false;
freezeOption = false;
aimOption = false;
chargeMultiple = false;
recurveOption = false;
searchOption = false;
spreadOption = false;
turretOption = false;
}
private bool HasAnyOptions()
{
return optionOne ||
(optionTwo && OptionsPredecessorRequirement(this, 2)) ||
(optionThree && OptionsPredecessorRequirement(this, 3)) ||
(optionFour && OptionsPredecessorRequirement(this, 4));
}
private bool HasAnyChargeMultipleAccessory()
{
return FindEquippedAccessory(player, ModContent.ItemType<ChargeMultipleOne>()) != null;
}
private void MakeForceBattle()
{
if (HasAForce() && IsSameClientOwner(forceProjectile) &&
forceProjectile.modProjectile is ForceBase fbProj)
{
fbProj.BattleMode();
}
}
private void MakeOptionSeedBattle()
{
if (optionSeed && IsSameClientOwner(seedProjectile) &&
seedProjectile.modProjectile is OptionSeedObject opSeed)
{
opSeed.BattleMode();
}
}
private bool HasAForce() => forceBase || needleForce;
private void FreezeBehavior(bool rotateActually = false)
{
if (rotateActually) baitPoint += player.position - player.oldPosition;
for (int p = 0; p < optionFlightPath.Count; p++)
{
optionFlightPath[p] += player.position - player.oldPosition;
}
}
private void RotateBehaviorGrouping()
{
FreezeBehavior(rotateActually: true);
Vector2 basisPoint = new Vector2(1, 0);
Vector2 newPosition = basisPoint * baitDirection * RotateOptionBase.Speed;
Vector2 limitPosition = basisPoint * baitDirection * RotateOptionBase.Radius;
if (Vector2.Distance(player.Center, baitPoint + newPosition) <=
Vector2.Distance(player.Center, player.Center + limitPosition))
{
baitPoint += newPosition;
}
else
{
baitPoint = player.Center + limitPosition;
rotateMode = (int)RotateOptionBase.States.Rotating;
baitDirection = 0;
}
optionFlightPath.Insert(0, baitPoint);
}
private void RotateBehaviorRevolving()
{
FreezeBehavior(rotateActually: true);
NormalizeAngleDegrees(ref baitAngle);
baitAngle += EquiAngle * revolveDirection;
baitPoint.X = player.Center.X + ((float)Math.Cos(MathHelper.ToRadians(baitAngle)) * RotateOptionBase.Radius);
baitPoint.Y = player.Center.Y - ((float)Math.Sin(MathHelper.ToRadians(baitAngle)) * RotateOptionBase.Radius);
optionFlightPath.Insert(0, baitPoint);
}
private void RotateBehaviorRecovering()
{
float recoverSpeed = Math.Min(RotateOptionBase.Speed, Vector2.Distance(player.Center, baitPoint));
baitPoint += MoveToward(baitPoint, player.Center, recoverSpeed);
optionFlightPath.Insert(0, baitPoint);
if (IsEqualWithThreshold(baitPoint, player.Center, RotateOptionBase.AcceptedThreshold))
{
rotateMode = (int)RotateOptionBase.States.Following;
}
}
private void RecurveBehavior()
{
PopulateOptionFlight();
if (IsSameClientOwner(player))
{
if (isRecurving)
{
recurveDistance += RecurveOptionBase.AdjustSpeed * recurveActionMode.ToDirectionInt();
recurveDistance = Math.Max(recurveDistance, RecurveOptionBase.LeastAdjustment);
recurveDistance = Math.Min(recurveDistance, RecurveOptionBase.CapAdjustment);
}
int distance = OptionBaseObject.DistanceInterval;
double direction = GetDirection(Main.MouseWorld) - MathHelper.Pi;
float offsetY = recurveDistance * recurveSide.ToDirectionInt();
Vector2 oldPath = optionFlightPath[distance - 1];
optionFlightPath[1 * distance - 1] = player.Center + new Vector2
{
X = (float)Math.Cos(direction) * RecurveOptionBase.FixedAxisDistance +
(float)Math.Cos(direction + MathHelper.PiOver2) * offsetY,
Y = (float)Math.Sin(direction) * RecurveOptionBase.FixedAxisDistance +
(float)Math.Sin(direction + MathHelper.PiOver2) * offsetY
};
optionFlightPath[2 * distance - 1] = player.Center + new Vector2
{
X = (float)Math.Cos(direction) * RecurveOptionBase.FixedAxisDistance +
(float)Math.Cos(direction + MathHelper.PiOver2) * -offsetY,
Y = (float)Math.Sin(direction) * RecurveOptionBase.FixedAxisDistance +
(float)Math.Sin(direction + MathHelper.PiOver2) * -offsetY
};
optionFlightPath[3 * distance - 1] = player.Center + new Vector2
{
X = (float)Math.Cos(direction) * RecurveOptionBase.FixedAxisDistance * 2 +
(float)Math.Cos(direction + MathHelper.PiOver2) * offsetY * 2,
Y = (float)Math.Sin(direction) * RecurveOptionBase.FixedAxisDistance * 2 +
(float)Math.Sin(direction + MathHelper.PiOver2) * offsetY * 2
};
optionFlightPath[4 * distance - 1] = player.Center + new Vector2
{
X = (float)Math.Cos(direction) * RecurveOptionBase.FixedAxisDistance * 2 +
(float)Math.Cos(direction + MathHelper.PiOver2) * -offsetY * 2,
Y = (float)Math.Sin(direction) * RecurveOptionBase.FixedAxisDistance * 2 +
(float)Math.Sin(direction + MathHelper.PiOver2) * -offsetY * 2
};
if (IsNotSinglePlayer() &&
!IsEqualWithThreshold(optionFlightPath[distance - 1], oldPath, .05f))
{
ModPacket packet = mod.GetPacket();
packet.Write((byte)ChensGradiusMod.PacketMessageType.RecurveUpdatePositions);
packet.Write((byte)player.whoAmI);
packet.WriteVector2(optionFlightPath[OptionBaseObject.DistanceInterval - 1]);
packet.WriteVector2(optionFlightPath[OptionBaseObject.DistanceInterval * 2 - 1]);
packet.WriteVector2(optionFlightPath[OptionBaseObject.DistanceInterval * 3 - 1]);
packet.WriteVector2(optionFlightPath[OptionBaseObject.DistanceInterval * 4 - 1]);
packet.Send();
}
}
}
private double GetDirection(Vector2 secondPoint) => (secondPoint - player.Center).ToRotation();
}
}