From 90e02d9bd76783b2e878ba02f2f7b3cccc1fcd71 Mon Sep 17 00:00:00 2001 From: GinYuH <72181395+GinYuH@users.noreply.github.com> Date: Thu, 11 Jan 2024 01:49:24 -0800 Subject: [PATCH] Added Teslastaff --- Effects/CalamityShaders.cs | 9 + Effects/TeslaTrail.fx | 56 ++++ Effects/TeslaTrail.xnb | Bin 0 -> 2020 bytes Items/Weapons/Magic/Teslastaff.cs | 54 ++++ Items/Weapons/Magic/Teslastaff.png | Bin 0 -> 892 bytes ...Mods.CalamityMod.Items.Weapons.Magic.hjson | 5 + .../Mods.CalamityMod.Projectiles.Magic.hjson | 1 + Projectiles/Magic/Teslabeam.cs | 242 ++++++++++++++++++ 8 files changed, 367 insertions(+) create mode 100644 Effects/TeslaTrail.fx create mode 100644 Effects/TeslaTrail.xnb create mode 100644 Items/Weapons/Magic/Teslastaff.cs create mode 100644 Items/Weapons/Magic/Teslastaff.png create mode 100644 Projectiles/Magic/Teslabeam.cs diff --git a/Effects/CalamityShaders.cs b/Effects/CalamityShaders.cs index edd6b46474..47c10cb3e8 100644 --- a/Effects/CalamityShaders.cs +++ b/Effects/CalamityShaders.cs @@ -192,6 +192,10 @@ namespace CalamityMod.Effects internal static Effect DeerclopsShadowShader; #endregion + #region YuH's Shaders + internal static Effect TeslaTrailShader; + #endregion + // Shorthand to register a loaded shader in Terraria's graphics engine // All shaders registered this way are accessible under GameShaders.Misc // They will use the prefix described above @@ -406,6 +410,11 @@ namespace CalamityMod.Effects RegisterMiscShader(DeerclopsShadowShader, "ShadowPass", "DeerclopsShadowShader"); #endregion + + #region Loading YuH's Shaders + TeslaTrailShader = LoadShader("TeslaTrail"); + RegisterMiscShader(TeslaTrailShader, "TrailPass", "TeslaTrail"); + #endregion } } } diff --git a/Effects/TeslaTrail.fx b/Effects/TeslaTrail.fx new file mode 100644 index 0000000000..3d1e36fe99 --- /dev/null +++ b/Effects/TeslaTrail.fx @@ -0,0 +1,56 @@ +sampler uImage0 : register(s0); +sampler uImage1 : register(s1); +float3 uColor; +float3 uSecondaryColor; +float uOpacity; +float uSaturation; +float uRotation; +float uTime; +float4 uSourceRect; +float2 uWorldPosition; +float uDirection; +float3 uLightSource; +float2 uImageSize0; +float2 uImageSize1; +matrix uWorldViewProjection; +float4 uShaderSpecificData; + +struct VertexShaderInput +{ + float4 Position : POSITION0; + float4 Color : COLOR0; + float2 TextureCoordinates : TEXCOORD0; +}; + +struct VertexShaderOutput +{ + float4 Position : SV_POSITION; + float4 Color : COLOR0; + float2 TextureCoordinates : TEXCOORD0; +}; + +VertexShaderOutput VertexShaderFunction(in VertexShaderInput input) +{ + VertexShaderOutput output = (VertexShaderOutput)0; + float4 pos = mul(input.Position, uWorldViewProjection); + output.Position = pos; + + output.Color = input.Color; + output.TextureCoordinates = input.TextureCoordinates; + + return output; +} + +float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 +{ + return input.Color; +} + +technique Technique1 +{ + pass TrailPass + { + VertexShader = compile vs_2_0 VertexShaderFunction(); + PixelShader = compile ps_2_0 PixelShaderFunction(); + } +} diff --git a/Effects/TeslaTrail.xnb b/Effects/TeslaTrail.xnb new file mode 100644 index 0000000000000000000000000000000000000000..dddb5a1b3a691aa829347fa06a8df3df3be89ea5 GIT binary patch literal 2020 zcmb_cO-~b16n!%VErP*?8W&BR#0@57Qfg~T0*O*oj8&Vq!7fb5Fny&X)0sM*5iuST71QJmeqo7x!iKqdyNLy-7?qe+!p z6*N&y2r-nyyn=1o4vTZP1i?u;RJ&KwSBXdZ zkw}1;s2?GHMMH-*7j2LAQ>W;uCtlfh1NHIx<|+Dfq^}w+j%RenFM^itK%iiO1$Eh; zqw_N6C(hjy#4@A;WuxHUGutuiCA(wz$BEA~LgLUUC(0WJ_GS$Sd(iRYeP#uHRCkn5 z&x@2-G?tof#V?X#k#0@M?XS^4mp@+>;|*pk%K7%sIl;`4JVnW&Q6I_AYFDs8(8GCy zuaq47McA3aAR5Q2L$y`Rw&+uI3D(EvWt*&#iXz4{OR zpx)qj(3Q{|!kG2042=JJ+?=#aeS&`uxa@N&<^w*LW1jSR0^IL28^w4Y1U+Yo-_ZFz F{|lGk2$cW; literal 0 HcmV?d00001 diff --git a/Items/Weapons/Magic/Teslastaff.cs b/Items/Weapons/Magic/Teslastaff.cs new file mode 100644 index 0000000000..8943ba5bd7 --- /dev/null +++ b/Items/Weapons/Magic/Teslastaff.cs @@ -0,0 +1,54 @@ +using CalamityMod.Items.Materials; +using CalamityMod.Projectiles.Magic; +using CalamityMod.Rarities; +using CalamityMod.Sounds; +using Terraria; +using Terraria.ID; +using Terraria.ModLoader; + +namespace CalamityMod.Items.Weapons.Magic +{ + public class Teslastaff : ModItem, ILocalizedModType + { + public new string LocalizationCategory => "Items.Weapons.Magic"; + public override void SetStaticDefaults() + { + Item.staff[Type] = true; + } + + public override void SetDefaults() + { + Item.width = 38; + Item.height = 38; + Item.damage = 40; + Item.DamageType = DamageClass.Magic; + Item.mana = 10; + Item.useTime = 10; + Item.useAnimation = 10; + Item.useStyle = ItemUseStyleID.Shoot; + Item.UseSound = CommonCalamitySounds.LightningSound with { Pitch = 1.1f }; + Item.noMelee = true; + Item.channel = true; + Item.knockBack = 0f; + Item.value = CalamityGlobalItem.Rarity12BuyPrice; + Item.rare = ModContent.RarityType(); + Item.Calamity().donorItem = true; + Item.shoot = ModContent.ProjectileType(); + Item.shootSpeed = 30f; + Item.reuseDelay = 60; + } + + public override bool CanUseItem(Player player) => player.ownedProjectileCounts[Item.shoot] <= 0; + + public override void AddRecipes() + { + CreateRecipe(). + AddIngredient(ItemID.ThunderStaff). + AddRecipeGroup("AnyCopperBar", 20). + AddIngredient(2). + AddIngredient(6). + AddTile(TileID.LunarCraftingStation). + Register(); + } + } +} diff --git a/Items/Weapons/Magic/Teslastaff.png b/Items/Weapons/Magic/Teslastaff.png new file mode 100644 index 0000000000000000000000000000000000000000..d36c5aec94f86a7a24b9344263aa8b08f2617285 GIT binary patch literal 892 zcmV-?1B3jDP)Px#AY({UO#lFTCIA3{ga82g0001h=l}q9FaQARU;qF* zm;eA5aGbhPJOBUy24YJ`L;(K){{a7>y{D4^00PQML_t(o!{ygsNRv?<#_{i|HRu24 ztW8mR8!L4YC`E-`6-5MflSH%IC;1q&QrB(_7M*wh-faN9jcx&(N$lX^LjZU*zopcf-fpG+{Pmd99yoUr zu(6n9y&iNa*ako>lLR30>V?sW~1FOXfrSipOeA%Q9;PbdF`|mt@0zhY15Xeim zow%rs+dN`2{k?SnJiT=t04;P?=`SQDV_UIxt7FGC9{?TRS^%=ij1DX0rka7goLTDe zU_SsJ-|h;l{IXuje9!y=V8KxZz~t<_4sd#ZCjjAhQ2=fp?ExS%x1h|I{YOHV1wcIZ ziOLX@QtcjNBY%nKl>61o_;0MFcDWk_z~^6cI>1n0pCLFr3_w$DjR2TQr2&|n{%LQq zHS1U*DY@HZTvM7NB_$Hc`J&G^OUaaHs~RUB+{x5dePR6aeqzb3mExO3OcF%~LG^ z#->w1zM=>38g)v3b61|JD@~+~@hxuSmVusA`sE3sRp%_FY%eDK-e${PCnd~h-&$>% zV@r2_SF5J<@AYgK03YMCWh!>QyA^=9-?ECFtW$EgtraO}j+F8>8b?Shkt`}@e=&I` z9%ykYa`MJ0vqDa}!!^~`05FqETkh^srED+e$gjYVle=bUWYluE$}G?GF85pBhD(*Q zz1XT%7W(GQb4nE`HMSM|3)XK8zn}%`O#lD@5_CmabVXQ6MmkS&X=QG7VRUJ4ZU8Za SSnTfr0000 "Projectiles.Magic"; + public override string Texture => "CalamityMod/Projectiles/InvisibleProj"; + public Player Owner => Main.player[Projectile.owner]; + public override Color LightCastColor => new Color(92, 144, 245); + public override float Lifetime => 18000f; + public override float MaxScale => 1f; + public override float MaxLaserLength => 1600f; //100 tiles + public override Texture2D LaserBeginTexture => ModContent.Request("CalamityMod/Projectiles/InvisibleProj", AssetRequestMode.ImmediateLoad).Value; + public override Texture2D LaserMiddleTexture => ModContent.Request("CalamityMod/Projectiles/InvisibleProj", AssetRequestMode.ImmediateLoad).Value; + public override Texture2D LaserEndTexture => ModContent.Request("CalamityMod/Projectiles/InvisibleProj", AssetRequestMode.ImmediateLoad).Value; + + List offsetPoints = new List(); + NPC Victim; + public float damageMultiplier = 1f; // a multiplier for how much damage the beam deals + public bool damageShouldDecay = false; // if this is true, the weapon's damage will decay if the grace period is over + public float decayGracePeriod = 0; // while the grace period is above 0, the weapon's damage won't decay + + public const float MaxDamageMultiplier = 5f; // the maximum damage multiplier + public const float GracePeriod = 10; // this controls the amount of frames that the grace period lasts for. It is combined with the beam's i frame cooldown + public const float DamagePerHit = 0.05f; // how much the damage multiplier increases for each enemy hit + public const float DamageDecayPerFrame = 0.01f; // how much the damage multiplier decreases if damage is decaying + public const float AimResponsiveness = 0.99f; // Last Prism is 0.92f. Lower makes the laser turn faster. + + public override void SetStaticDefaults() + { + Main.projFrames[Projectile.type] = 1; + } + + public override void SetDefaults() + { + Projectile.width = 10; + Projectile.height = 10; + Projectile.friendly = true; + Projectile.DamageType = DamageClass.Magic; + Projectile.penetrate = -1; + Projectile.tileCollide = false; + Projectile.hide = true; + Projectile.timeLeft = 18000; + Projectile.usesIDStaticNPCImmunity = true; + Projectile.idStaticNPCHitCooldown = 10; + } + public override void SendExtraAI(BinaryWriter writer) + { + writer.Write(damageMultiplier); + writer.Write(damageShouldDecay); + writer.Write(decayGracePeriod); + } + + public override void ReceiveExtraAI(BinaryReader reader) + { + damageMultiplier = reader.ReadSingle(); + decayGracePeriod = reader.ReadSingle(); + damageShouldDecay = reader.ReadBoolean(); + } + + public override bool PreAI() + { + // Multiplayer support here, only run this code if the client running it is the owner of the projectile + if (Projectile.owner == Main.myPlayer) + { + Vector2 rrp = Owner.RotatedRelativePoint(Owner.MountedCenter, true); + UpdateAim(rrp); + Projectile.direction = Main.MouseWorld.X > Owner.Center.X ? 1 : -1; + Projectile.netUpdate = true; + } + + int dir = Math.Sign(Projectile.velocity.X); + Projectile.rotation = Projectile.velocity.ToRotation() + MathHelper.PiOver2; + Projectile.Center = Owner.Center + Projectile.velocity * 56f; //Distance offset + Projectile.timeLeft = 18000; //Infinite lifespan + Owner.ChangeDir(dir); + Owner.heldProj = Projectile.whoAmI; + Owner.itemTime = 2; + Owner.itemAnimation = 2; + Owner.itemRotation = ((Projectile.rotation + MathHelper.PiOver2).ToRotationVector2() * -Owner.direction).ToRotation(); + + if (!Owner.channel) + { + Projectile.Kill(); + return false; + } + // Do we still have enough mana? If not, we kill the projectile because we cannot use it anymore + if (Owner.miscCounter % 10 == 0 && !Owner.CheckMana(Owner.ActiveItem(), -1, true)) + { + Projectile.Kill(); + return false; + } + + Projectile.ai[2]++; + + // Play zap sounds idly + if (Projectile.ai[2] % 5 == 0) + { + SoundEngine.PlaySound(SoundID.DD2_LightningBugZap with { Pitch = 1.1f }, Projectile.position); + } + // If the weapon's target hasn't just been hit, tick down the grace period timer + if (decayGracePeriod > 0 && damageShouldDecay) + { + decayGracePeriod--; + } + + // If the grace period timer runs out, start decaying damage + if (damageShouldDecay && damageMultiplier > 1f && decayGracePeriod <= 0) + { + damageMultiplier = MathHelper.Max(damageMultiplier - DamageDecayPerFrame, 1f); + } + // Update the beam's damage + Projectile.damage = (int)MathHelper.Clamp(Projectile.originalDamage * damageMultiplier, 0, Projectile.originalDamage * MaxDamageMultiplier); + // After damage has been set, set the decay flag to true. If the beams hits the target, this will be set back to false, but if not, damage will start decaying in the next tick + damageShouldDecay = true; + + return true; + } + + public PrimitiveTrail LightningDrawer; + + public PrimitiveTrail LightningBackgroundDrawer; + internal float WidthFunction(float completionRatio) + { + return MathHelper.Clamp(completionRatio * 15, 1, 1.5f); + } + + internal Color ColorFunction(float completionRatio) + { + return new Color(174, 227, 244); // directly color picked from the source material + } + + internal float BackgroundWidthFunction(float completionRatio) => WidthFunction(completionRatio) * 4f; + + internal Color BackgroundColorFunction(float completionRatio) + { + return new Color(92, 144, 245) * 0.6f; // directly color picked from the source material + } + public override bool PreDraw(ref Color lightColor) + { + Main.spriteBatch.EnterShaderRegion(); + if (LightningDrawer is null) + LightningDrawer = new PrimitiveTrail(WidthFunction, ColorFunction, PrimitiveTrail.RigidPointRetreivalFunction, GameShaders.Misc["CalamityMod:TeslaTrail"]); + if (LightningBackgroundDrawer is null) + LightningBackgroundDrawer = new PrimitiveTrail(BackgroundWidthFunction, BackgroundColorFunction, PrimitiveTrail.RigidPointRetreivalFunction, GameShaders.Misc["CalamityMod:TeslaTrail"]); + GameShaders.Misc["CalamityMod:TeslaTrail"].SetShaderTexture(ModContent.Request("CalamityMod/ExtraTextures/Trails/ZapTrail")); + + // every 2 frames update the offsets are randomized. This is effectively control for the lightning's fps + if (Projectile.ai[2] % 2 == 0) + { + offsetPoints.Clear(); + for (int i = 0; i <= 75; i++) + { + Vector2 baseVec = Vector2.Zero; + float width = 16; + if (i > 0) + { + baseVec += Main.rand.NextVector2Square(-width, width); + } + offsetPoints.Add(Main.rand.NextVector2Square(-width, width)); + } + } + // do not try to draw the lightning if the offset list isn't filled yet or an index error will occur + if (offsetPoints.Count < 75) + return false; + + // the final list of points that will be used to draw the lightning which combines a series of points travelling up the beam with the random values from the offset list + List finalPoints = new List(); + for (int i = 0; i <= 75; i++) + { + Vector2 baseVec = Vector2.Lerp(Projectile.Center, Projectile.Center + Projectile.velocity * LaserLength, i / 73.5f); + float width = 16 + (i / 75 * 10); + if (i > 0) + { + baseVec += offsetPoints[i]; + } + finalPoints.Add(baseVec); + } + + LightningBackgroundDrawer.Draw(finalPoints, -Main.screenPosition, 75); + LightningDrawer.Draw(finalPoints, -Main.screenPosition, 75); + + Main.spriteBatch.ExitShaderRegion(); + return false; + } + // Gently adjusts the aim vector of the laser to point towards the mouse. + private void UpdateAim(Vector2 source) + { + Vector2 aimVector = Vector2.Normalize(Main.MouseWorld - source); + if (aimVector.HasNaNs()) + aimVector = -Vector2.UnitY; + aimVector = Vector2.Normalize(Vector2.Lerp(aimVector, Vector2.Normalize(Projectile.velocity), AimResponsiveness)); + + if (aimVector != Projectile.velocity) + Projectile.netUpdate = true; + Projectile.velocity = aimVector; + } + + public override bool ShouldUpdatePosition() => false; + + // Update CutTiles so the laser will cut tiles (like grass). + public override void CutTiles() + { + DelegateMethods.tilecut_0 = TileCuttingContext.AttackProjectile; + Vector2 unit = Projectile.velocity; + Utils.PlotTileLine(Projectile.Center, Projectile.Center + unit * LaserLength, Projectile.width + 16, DelegateMethods.CutTiles); + } + + public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone) + { + // if the beam's damage multiplier is at default value, the hit enemy becomes the beam's new target + if (damageMultiplier <= 1f) + { + Victim = target; + } + // if the beam hits its current target, disable damage decay for the frame, reset its grace period, and increase its damage + if (Victim == target) + { + damageShouldDecay = false; + decayGracePeriod = Projectile.idStaticNPCHitCooldown + GracePeriod; + if (damageMultiplier < MaxDamageMultiplier) + { + damageMultiplier = Math.Min(damageMultiplier + DamagePerHit, MaxDamageMultiplier); + } + } + target.AddBuff(BuffID.Electrified, 30); + } + } +}