Damage to the player

JerryPlayz101

Terrarian
Hello all, it has been about a year since I tried to make a mod for Terraria; and in that time my knowledge of C# has increased, but my remembrance of the API of TModloader hasn't. This might seem like a very silly question, but how would I go about trying to "hurt" the player. Where I need it is close to the end of the script. Its where the comment is.

Thanks in advance for any help you people can offer.

C#:
using System;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria;

namespace MK
{
    class MyModPlayer : ModPlayer
    {
        public bool silkArmorEquipped;

        public override void ResetEffects()
        {
            silkArmorEquipped = false; // If we don't do this, the class will think the armor is still on, even when it's taken off.
        }

        public override void ModifyHitByNPC(NPC npc, ref int damage, ref bool crit)
        {
            ModifyDamageToMana(ref damage);
        }
        public override void ModifyHitByProjectile(Projectile proj, ref int damage, ref bool crit)
        {
            ModifyDamageToMana(ref damage);
        }

        void ModifyDamageToMana(ref int damage)
        {
            if (!silkArmorEquipped) return; // Do nothing if the armor is not equipped.

            float damagePercent = (damage);
            int damageToMana = (int)(0.75F * damagePercent);
            if (damageToMana <= player.statMana) // We have enough mana to absorb the damage.
            {
                player.statMana -= damageToMana;
                player.ManaEffect(damageToMana);
                damage = (int)(damagePercent * 25); // 25% damage.
                //return;
            }
            // Now this executes when we don't have enough mana to absord all damage.
            else
            {
                damageToMana -= player.statMana;
                player.statMana = 0;
                damage = (int)(damagePercent * 25) + damageToMana; // 25% damage + the leftover we couldn't absorb.
                //TODO player.hurt();  -- this is where I need to trigger hurt to the player, the dame amount
                //HERE
            }
            

        }
    }
}
 
Hello all, it has been about a year since I tried to make a mod for Terraria; and in that time my knowledge of C# has increased, but my remembrance of the API of TModloader hasn't. This might seem like a very silly question, but how would I go about trying to "hurt" the player. Where I need it is close to the end of the script. Its where the comment is.

Thanks in advance for any help you people can offer.

C#:
using System;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria;

namespace MK
{
    class MyModPlayer : ModPlayer
    {
        public bool silkArmorEquipped;

        public override void ResetEffects()
        {
            silkArmorEquipped = false; // If we don't do this, the class will think the armor is still on, even when it's taken off.
        }

        public override void ModifyHitByNPC(NPC npc, ref int damage, ref bool crit)
        {
            ModifyDamageToMana(ref damage);
        }
        public override void ModifyHitByProjectile(Projectile proj, ref int damage, ref bool crit)
        {
            ModifyDamageToMana(ref damage);
        }

        void ModifyDamageToMana(ref int damage)
        {
            if (!silkArmorEquipped) return; // Do nothing if the armor is not equipped.

            float damagePercent = (damage);
            int damageToMana = (int)(0.75F * damagePercent);
            if (damageToMana <= player.statMana) // We have enough mana to absorb the damage.
            {
                player.statMana -= damageToMana;
                player.ManaEffect(damageToMana);
                damage = (int)(damagePercent * 25); // 25% damage.
                //return;
            }
            // Now this executes when we don't have enough mana to absord all damage.
            else
            {
                damageToMana -= player.statMana;
                player.statMana = 0;
                damage = (int)(damagePercent * 25) + damageToMana; // 25% damage + the leftover we couldn't absorb.
                //TODO player.hurt();  -- this is where I need to trigger hurt to the player, the dame amount
                //HERE
            }
           

        }
    }
}
If there is also any way to capture all on_hurt events, please tell me.... it'll make the code more efficient. I need to capture the event before the hurt is applied to the player.
 
To damage the player, you can simply call the Hurt method:
C#:
public double Hurt(PlayerDeathReason damageSource, int Damage, int hitDirection, bool pvp = false, bool quiet = false, bool Crit = false, int cooldownCounter = -1)
The respective Hurt hooks are PreHurt, Hurt and PostHurt: check the link in the post above to find their method signatures.
 
Back
Top Bottom