This commit is contained in:
Dreitone committed 2026-06-11 16:55:04 -05:00
1 parent 388fa09e10
commit 19b3120d60
4 files changed
+161 -35

No files matched your search

+68 -35
View File
@@ -1,10 +1,12 @@
using CalamityMod.Items.Placeables.Furniture;
using CalamityMod.NPCs;
using CalamityMod.Packets.Entities;
using CalamityMod.Particles;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
@@ -15,6 +17,65 @@ namespace CalamityMod.Items.Tools
{
public new string LocalizationCategory => "Items.Tools";
public static readonly SoundStyle BadBuzzer = new SoundStyle("CalamityMod/Sounds/Custom/BadGiftBuzzer") with { Volume = 0.85f };
public static bool CanApplyToNPC(NPC npc)
{
return npc.isLikeATownNPC && !NPCID.Sets.NoTownNPCHappiness[npc.type] && !NPCID.Sets.IsTownPet[npc.type] && !npc.GetGlobalNPC<CalamityGlobalTownNPC>().TheGiftStatus.HasValue;
}
public static void ApplyGiftEffects(NPC npc, bool positive)
{
Color color = positive ? Color.Green : Color.Red;
SoundEngine.PlaySound(positive ? SoundID.ResearchComplete : BadBuzzer, npc.Center);
// nothing ever happens....
for (int i = 0; i < 6; i++)
{
Vector2 spawnPos = npc.Center + Main.rand.NextVector2Circular(30f, 30f);
HealingPlus sparkle = new(spawnPos, 0.5f, (spawnPos - npc.Center).SafeNormalize(Vector2.Zero) * 0.65f, color, color * 0.2f, 25)
{
Rotation = positive ? 0f : MathHelper.PiOver4
};
GeneralParticleHandler.SpawnParticle(sparkle);
}
for (int j = 0; j < 4; j++)
{
Vector2 spawnPos = npc.Center + Main.rand.NextVector2Circular(50f, 50f);
Vector2 arrowVelocity = -Vector2.UnitY * 0.6f * positive.ToDirectionInt();
StatChangeArrow arrow = new(spawnPos, arrowVelocity, -MathHelper.PiOver2 * positive.ToDirectionInt(), color, color * 0.1f, 0.7f, 40);
GeneralParticleHandler.SpawnParticle(arrow);
}
}
public static bool TryApplyGift(Item item, NPC npc, bool? forcedOutcome = null)
{
if (!item.active || item.type != ModContent.ItemType<TheGift>() || Main.remixWorld || !CanApplyToNPC(npc))
return false;
bool positive = forcedOutcome ?? Main.rand.NextBool();
var townNPC = npc.GetGlobalNPC<CalamityGlobalTownNPC>();
townNPC.TheGiftStatus = positive;
townNPC.TheGiftReset = 0.0;
ApplyGiftEffects(npc, positive);
item.stack--;
if (item.stack <= 0)
{
item.active = false;
item.type = ItemID.None;
}
if (Main.netMode == NetmodeID.Server)
{
npc.netUpdate = true;
NetMessage.SendData(MessageID.SyncItem, -1, -1, null, item.whoAmI);
TheGiftEffectsPacket.Send(npc, positive);
}
return true;
}
public override void SetDefaults()
{
Item.width = 30;
@@ -39,49 +100,21 @@ namespace CalamityMod.Items.Tools
{
// Controls actually applying the effect to an NPC
// None of this should work in Remix because Remix disables happiness
if (Item.noGrabDelay <= 0 && !Main.remixWorld && Item.playerIndexTheItemIsReservedFor == Main.myPlayer)
bool localGiftCollisionCheck = Main.netMode == NetmodeID.SinglePlayer || Item.playerIndexTheItemIsReservedFor == Main.myPlayer;
if (Item.noGrabDelay <= 0 && !Main.remixWorld && localGiftCollisionCheck)
{
foreach (NPC n in Main.ActiveNPCs)
{
// Don't run this code for NPCs that aren't affected by happiness
// Also don't run this if The Gift has already been used on this NPC; they all will hold grudges against you :)
if (!n.isLikeATownNPC || NPCID.Sets.NoTownNPCHappiness[n.type] || NPCID.Sets.IsTownPet[n.type] || n.GetGlobalNPC<CalamityGlobalTownNPC>().TheGiftStatus.HasValue)
if (!CanApplyToNPC(n))
continue;
if (Item.Hitbox.Intersects(n.Hitbox))
{
bool positive = Main.rand.NextBool();
Color c = positive ? Color.Green : Color.Red;
SoundEngine.PlaySound(positive ? SoundID.ResearchComplete : BadBuzzer, n.Center);
n.GetGlobalNPC<CalamityGlobalTownNPC>().TheGiftStatus = positive;
n.GetGlobalNPC<CalamityGlobalTownNPC>().TheGiftReset = 0.0;
if (Main.netMode == NetmodeID.SinglePlayer)
TryApplyGift(Item, n);
else if (Main.netMode == NetmodeID.MultiplayerClient)
RequestApplyTheGiftPacket.Send(Item, n);
// Visual effect to demonstrate that something happened
for (int i = 0; i < 6; i++)
{
Vector2 spawnPos = n.Center + Main.rand.NextVector2Circular(30f, 30f);
HealingPlus s = new(spawnPos, 0.5f, (spawnPos - n.Center).SafeNormalize(Vector2.Zero) * 0.65f, c, c * 0.2f, 25)
{
Rotation = positive ? 0f : MathHelper.PiOver4
};
GeneralParticleHandler.SpawnParticle(s);
}
for (int j = 0; j < 4; j++)
{
Vector2 spawnPos = n.Center + Main.rand.NextVector2Circular(50f, 50f);
Vector2 arrVel = -Vector2.UnitY * 0.6f * positive.ToDirectionInt();
StatChangeArrow arr = new(spawnPos, arrVel, -MathHelper.PiOver2 * positive.ToDirectionInt(), c, c * 0.1f, 0.7f, 40);
GeneralParticleHandler.SpawnParticle(arr);
}
Item.stack--;
if (Item.stack <= 0)
{
Item.active = false;
Item.type = ItemID.None;
}
if (Main.netMode != NetmodeID.SinglePlayer)
NetMessage.SendData(MessageID.SyncItem, -1, -1, null, Item.whoAmI);
// Make sure it can't apply to multiple NPCs at once
break;
}
+18
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using CalamityMod.Events;
using CalamityMod.Items.Accessories;
using CalamityMod.Items.Accessories.Vanity;
@@ -32,6 +33,7 @@ using Terraria.GameContent;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.WorldBuilding;
using static Terraria.ModLoader.ModContent;
@@ -106,6 +108,22 @@ namespace CalamityMod.NPCs
return myClone;
}
// TODO: [The Gift] Move out of this godforsaken class
public override void SendExtraAI(NPC npc, BitWriter bitWriter, BinaryWriter binaryWriter)
{
bitWriter.WriteBit(TheGiftStatus.HasValue);
if (TheGiftStatus.HasValue)
bitWriter.WriteBit(TheGiftStatus.Value);
binaryWriter.Write(TheGiftReset);
}
public override void ReceiveExtraAI(NPC npc, BitReader bitReader, BinaryReader binaryReader)
{
TheGiftStatus = bitReader.ReadBit() ? bitReader.ReadBit() : null;
TheGiftReset = binaryReader.ReadDouble();
}
#region Town NPC Patreon Name Sets
private static readonly string[] AnglerNames =
[
@@ -0,0 +1,39 @@
using System.IO;
using CalamityMod.Items.Tools;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.Packets.Entities
{
internal sealed class RequestApplyTheGiftPacket : CalamityPacket
{
public static RequestApplyTheGiftPacket Instance { get; private set; }
public static void Send(Item item, NPC npc, int toClient = -1, int ignoreClient = -1)
{
if (item is null || npc is null)
return;
var packet = Instance.CreateBasePacket();
packet.Write((short)item.whoAmI);
packet.WriteWhoAmI(npc);
packet.Send(toClient, ignoreClient);
}
public override void HandlePacket(BinaryReader packet, int sender)
{
int itemWhoAmI = packet.ReadInt16();
NPC npc = packet.ReadNPC();
if (!Main.dedServ || npc is null || itemWhoAmI < 0 || itemWhoAmI >= Main.maxItems)
return;
Item item = Main.item[itemWhoAmI];
if (!item.active || item.type != ModContent.ItemType<TheGift>())
return;
TheGift.TryApplyGift(item, npc);
}
}
}
+36
View File
@@ -0,0 +1,36 @@
using System.IO;
using CalamityMod.Items.Tools;
using Terraria;
namespace CalamityMod.Packets.Entities
{
internal sealed class TheGiftEffectsPacket : CalamityPacket
{
public static TheGiftEffectsPacket Instance { get; private set; }
public static void Send(NPC npc, bool positive, int toClient = -1, int ignoreClient = -1)
{
if (npc is null)
return;
var packet = Instance.CreateBasePacket();
packet.WriteWhoAmI(npc);
packet.Write(positive);
packet.Send(toClient, ignoreClient);
}
public override void HandlePacket(BinaryReader packet, int sender)
{
NPC npc = packet.ReadNPC();
bool positive = packet.ReadBoolean();
if (npc is null)
return;
TheGift.ApplyGiftEffects(npc, positive);
if (Main.dedServ)
Send(npc, positive, ignoreClient: sender);
}
}
}