using System; using CalamityMod.Projectiles.Healing; using Microsoft.Xna.Framework; using Terraria; using Terraria.ModLoader; namespace CalamityMod.NPCs { public class CalamityTileCollisionHarmNPC : GlobalNPC { /// /// How much "gravity damage" is this npc slated to recieve once it hits the ground. /// private int potentialEnergyDamage = 0; /// /// This keeps track of the last recorded velocity, so that we can dispel the potential fall damage if the enemy slows their fall down by any means /// private Vector2 OldVelocity = Vector2.Zero; /// /// This keeps track of the last recorded velocity, because terraria is dumb and we need two of them /// private Vector2 OlderVelocity = Vector2.Zero; private float terminalVelocityForFullFallDamage = 0; private bool CheckTiles = false; private Vector2 ForcedVel = Vector2.Zero; private bool hitVoid = false; public int PotentialEnergyDamage { get { return potentialEnergyDamage; } private set { //You can't stack gravity damage. Higher "gravity" simply replaces the gravity damage. potentialEnergyDamage = value == 0 ? 0 : Math.Max(potentialEnergyDamage, value); } } public bool FallDamageSusceptible => PotentialEnergyDamage > 0; public override bool InstancePerEntity => true; public override GlobalNPC Clone(NPC npc, NPC npcClone) { CalamityTileCollisionHarmNPC myClone = (CalamityTileCollisionHarmNPC)base.Clone(npc, npcClone); myClone.potentialEnergyDamage = potentialEnergyDamage; myClone.OldVelocity = OldVelocity; myClone.OlderVelocity = OlderVelocity; myClone.terminalVelocityForFullFallDamage = terminalVelocityForFullFallDamage; return myClone; } public override void SetDefaults(NPC npc) { PotentialEnergyDamage = 0; OldVelocity = Vector2.Zero; OlderVelocity = Vector2.Zero; } /// /// Sets up a NPC to recieve fall damage when they hit the ground. /// /// The npc to apply fall damage to /// The maximum fall damage taken by the npc once they hit the ground /// The downwards velocity necessary to recieve the full fall damage. By default 10, the npc max fall speed /// Will check for tiles in the collision instead of velocity public void ApplyCollisionDamage(NPC npc, int potentialDamage, Vector2 forcedVel, float terminalVelocityForFullDamage = 10f, bool checkTiles = false) { //NPCs that don't collide with tiles simply don't get fall damage, lol. if (npc.noTileCollide) return; PotentialEnergyDamage = potentialDamage; OldVelocity = npc.velocity; OlderVelocity = npc.velocity; terminalVelocityForFullFallDamage = terminalVelocityForFullDamage; CheckTiles = checkTiles; ForcedVel = forcedVel; } public override void PostAI(NPC npc) { if (FallDamageSusceptible) { if (CheckTiles) { float oldVelocity = OldVelocity.Length(); if (Collision.SolidCollision(npc.Center, (int)(npc.width * 0.5f), (int)(npc.height * 0.5f)) || !WorldGen.InWorld(npc.Center.ToTileCoordinates().X, npc.Center.ToTileCoordinates().Y, 40)) { npc.StrikeNPC(npc.CalculateHitInfo((int)(PotentialEnergyDamage * Math.Clamp(oldVelocity / terminalVelocityForFullFallDamage, 0f, 1f)), 1), true); PotentialEnergyDamage = 0; hitVoid = false; } oldVelocity = npc.velocity.Length(); npc.Center += ForcedVel; ForcedVel *= 0.995f; if (!WorldGen.InWorld(npc.Center.ToTileCoordinates().X, npc.Center.ToTileCoordinates().Y, 40) && !hitVoid) { npc.velocity = -npc.velocity * 0.5f; ForcedVel = -ForcedVel * 0.5f; hitVoid = true; } } else { float newVerticalVelocity = npc.velocity.Y; float oldVerticalVelocity = OldVelocity.Y; float olderVerticalVelocity = OlderVelocity.Y; //If the thing flies back up, cancel the fall dmg if (newVerticalVelocity < 0 && oldVerticalVelocity >= 0) PotentialEnergyDamage = 0; //Same goes for if it slows its fall. //We use 3 variables cuz there is a frame inbetween the fall and the landing where the velocity is still set to something. if (newVerticalVelocity > 0 && olderVerticalVelocity > 0 && oldVerticalVelocity < olderVerticalVelocity) PotentialEnergyDamage = 0; //If the npc hit a tile/Came to a stop after falling if (newVerticalVelocity == 0 && oldVerticalVelocity > 0) //Collision.SolidCollision(npc.Center, npc.width, npc.height)) { //I don't think a tile check below the npc is necessary but if we find weird edge cases ill add it npc.StrikeNPC(npc.CalculateHitInfo((int)(PotentialEnergyDamage * Math.Clamp(olderVerticalVelocity / terminalVelocityForFullFallDamage, 0f, 1f)), 1), true); PotentialEnergyDamage = 0; } OlderVelocity = OldVelocity; OldVelocity = npc.velocity; } } } } }