tMx/Content/Projectiles/MeteorHammerProjectile.cs
2025-02-11 16:04:10 -05:00

87 lines
5.4 KiB
C#

using tMx.Content.Items;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using System;
using Terraria.Audio;
using Microsoft.Xna.Framework;
namespace tMx.Content.Projectiles
{
// This file showcases the concept of piercing.
// The code of the item that spawns it is located at the bottom.
// NPC.immune determines if an npc can be hit by a item or projectile owned by a particular player (it is an array, each slot corresponds to different players (whoAmI))
// NPC.immune is decremented towards 0 every update
// Melee items set NPC.immune to player.itemAnimation, which starts at item.useAnimation and decrements towards 0
// Projectiles, however, provide mechanisms for custom immunity.
// 1. penetrate == 1: A projectile with penetrate set to 1 in SetDefaults will hit regardless of the NPC's immunity counters (The penetrate from SetDefaults is remembered in maxPenetrate)
// Ex: Wooden Arrow.
// 2. No code and penetrate > 1, penetrate == -1, or (appliesImmunityTimeOnSingleHits && penetrate == 1): npc.immune[owner] will be set to 10.
// The NPC will be hit if not immune and will become immune to all damage for 10 ticks
// Ex: Unholy Arrow
// 3. Override OnHitNPC: If not immune, when it hits it manually set an immune other than 10
// Ex: Arkhalis: Sets it to 5
// Ex: Sharknado Minion: Sets to 20
// Video: https://media-1.discordapp.net/attachments/242228770855976960/1150275205017636995/Projectile_Immunity_Sharknado_Arkhalis_Example.mp4 Notice how Sharknado minion hits prevent Arkhalis hits for a brief moment.
// 4. Projectile.usesIDStaticNPCImmunity and Projectile.idStaticNPCHitCooldown: Specifies that a type of projectile has a shared immunity timer for each npc.
// Use this if you want other projectiles a chance to damage, but don't want the same projectile type to hit an npc rapidly.
// Ex: Ghastly Glaive is the only one who uses this.
// 5. Projectile.usesLocalNPCImmunity and Projectile.localNPCHitCooldown: Specifies the projectile manages it's own immunity timers for each npc
// Use this if you want the multiple projectiles of the same type to have a chance to attack rapidly, but don't want a single projectile to hit rapidly. A -1 value prevents the same projectile from ever hitting the npc again.
// Ex: Lightning Aura sentries use this. (localNPCHitCooldown = 3, but other code controls how fast the projectile itself hits)
// Overlapping Auras all have a chance to hit after each other even though they share the same ID.
// Try the above by uncommenting out the respective bits of code in the projectile below.
// This is a simple item that is based on the FlintlockPistol and shoots ExamplePiercingProjectile to showcase it.
internal class MeteorHammerProjectile : ModProjectile
{
public override void SetDefaults()
{
Projectile.width = 8; // The width of projectile hitbox
Projectile.height = 8; // The height of projectile hitbox
Projectile.aiStyle = 1; // The ai style of the projectile, please reference the source code of Terraria
Projectile.friendly = true; // Can the projectile deal damage to enemies?
Projectile.hostile = false; // Can the projectile deal damage to the player?
Projectile.DamageType = DamageClass.Melee; // Is the projectile shoot by a ranged weapon?
Projectile.penetrate = 0; // How many monsters the projectile can penetrate. (OnTileCollide below also decrements penetrate for bounces as well)
Projectile.timeLeft = 60; // The live time for the projectile (60 = 1 second, so 600 is 10 seconds)
Projectile.alpha = 5; // The transparency of the projectile, 255 for completely transparent. (aiStyle 1 quickly fades the projectile in) Make sure to delete this if you aren't using an aiStyle that fades in. You'll wonder why your projectile is invisible.
Projectile.light = 10.0f; // How much light emit around the projectile
Projectile.ignoreWater = true; // Does the projectile's speed be influenced by water?
Projectile.tileCollide = true; // Can the projectile collide with tiles?
Projectile.extraUpdates = 1; // Set to above 0 if you want the projectile to update multiple time in a frame
}
public override bool OnTileCollide(Vector2 oldVelocity)
{
// If collide with tile, reduce the penetrate.
// So the projectile can reflect at most 5 times
Projectile.penetrate--;
if (Projectile.penetrate <= 0)
{
Projectile.Kill();
}
else
{
Collision.HitTiles(Projectile.position, Projectile.velocity, Projectile.width, Projectile.height);
SoundEngine.PlaySound(SoundID.Item10, Projectile.position);
// If the projectile hits the left or right side of the tile, reverse the X velocity
if (Math.Abs(Projectile.velocity.X - oldVelocity.X) > float.Epsilon)
{
Projectile.velocity.X = -oldVelocity.X;
}
// If the projectile hits the top or bottom side of the tile, reverse the Y velocity
if (Math.Abs(Projectile.velocity.Y - oldVelocity.Y) > float.Epsilon)
{
Projectile.velocity.Y = -oldVelocity.Y;
}
}
return false;
}
}
}