How do I make a charging weapon?

SouzouOokami

Terrarian
I'm trying to make a weapon that charges up infinitely but resets if you get hit, but it keeps firing projectiles, constantly setting the charge back to 1. How do I make it so that way it will only fire a projectile after you let go of the mouse, if my charge code isn't also structured in a way where it won't go up?

Here's my code:

using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.DataStructures;
using Microsoft.Xna.Framework;
using System;
using Resurgance;
using Resurgance.Projectiles.Player;
using Microsoft.Xna.Framework.Input;

namespace Resurgance.Items
{
public class ChargeBlaster : ModItem
{
public override void SetStaticDefaults()
{
DisplayName.SetDefault("Charge Blaster"); // By default, capitalization in classnames will add spaces to the display name. You can customize the display name here by uncommenting this line.
Tooltip.SetDefault("Gains more damage infinitely.\nResets after taking damage");
}
public override void SetDefaults()
{
Item.damage = 10 * charge;
Item.DamageType = DamageClass.Ranged;
Item.width = 40;
Item.height = 40;
Item.useTime = 20;
Item.useAnimation = 20;
Item.noMelee = true;
Item.useStyle = ItemUseStyleID.Shoot;
Item.channel = true;
Item.knockBack = 3;
Item.value = 75;
Item.rare = ItemRarityID.Blue;
Item.UseSound = SoundID.Item1;
Item.autoReuse = true;

Item.shoot = ModContent.ProjectileType<ChargeShot>();
Item.shootSpeed = 10f;
}
public float timer = 0f;
public float tickRate = 1f / 60;
public int charge = 1;
public void Update()
{
timer += tickRate;
if (timer >= 1f)
{
timer -= 1f;
charge ++;
}

}
public void Hurt()
{
charge = 1;
}
public override bool Shoot(Player player, EntitySource_ItemUse_WithAmmo source, Vector2 position, Vector2 velocity, int type, int damage, float knockback) {
const int NumProjectiles = 1;
for (int i = 0; i < NumProjectiles; i++) {

Vector2 newVelocity = velocity.RotatedByRandom(MathHelper.ToRadians(1));

newVelocity *= 1f + Main.rand.NextFloat(0.1f);

// Create a projectile.
Projectile.NewProjectileDirect(source, position, newVelocity, type, damage * charge, knockback, player.whoAmI);
}
charge = 1;
return false;
}

}
}
 
The actions performed while channeling are normally handled by the projectile being channeled. If you wanted to make "charging" and "releasing", this projectile would be the one to handle it. Overall, this shouldn't be that hard to make, but I would recommend that you try and understand channeling and C# to make this. The example mod as a replica last prism that demonstrates how to make the channeled projectile.
Replica last prism channeled projectile:
Replica last prism item:
 
The actions performed while channeling are normally handled by the projectile being channeled. If you wanted to make "charging" and "releasing", this projectile would be the one to handle it. Overall, this shouldn't be that hard to make, but I would recommend that you try and understand channeling and C# to make this. The example mod as a replica last prism that demonstrates how to make the channeled projectile.
Replica last prism channeled projectile:
Replica last prism item:
I don't really want it to be like the last prism, I more so want it to be like a normal projectile but it gains around 10 damage every second, but the charge is set back to 1 after taking damage. The problem is that I don't know how to make it not fire any projectile at all until the mouse button is released, I can't find any tutorials that work
 
I don't think there is a specific tutorial that will cover what you are trying to do.

I do something like this in my mod, and the way it works is this. The item has item.channel set to true. It shoots a stationary, non damaging projectile (looks like the player holds it). In this projectile, I increment a timer in AI, and when the timer reaches X amount, I kill the projectile and fire the 2nd projectile, which is the damaging one. The damage is based on the total charge time, so if you let go of the left mouse button before then, it still shoots, but does less damage than waiting for the max charge.
 
I don't think there is a specific tutorial that will cover what you are trying to do.

I do something like this in my mod, and the way it works is this. The item has item.channel set to true. It shoots a stationary, non damaging projectile (looks like the player holds it). In this projectile, I increment a timer in AI, and when the timer reaches X amount, I kill the projectile and fire the 2nd projectile, which is the damaging one. The damage is based on the total charge time, so if you let go of the left mouse button before then, it still shoots, but does less damage than waiting for the max charge.
Hi could i please ask you for an example on how did you achieve this? I am trying to make a rifle that charges and shoots on mouse release, but i am a complete newbie in terraria modding and C# (i only did very basic weapons so far)
 
Last edited:
Back
Top Bottom