multiple lasers not drawing

Laifj

Terrarian
I am trying to make a weapon that shoots three lasers from points around the player. Two of the lasers don't draw but act like they are there. The hit boxes and dust are still there but the actual laser isn't. I can't figure out why this is and how to fix it. Also How would you make the lasers converge on the mouse and not just point in the same direction?

Here is the code for all the laser and the weapon.(it is from the example mod but altered some)

Laser (all that I changed between the lasers is the values in vector2_1)

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Terraria;
using Terraria.Enums;
using Terraria.ModLoader;

namespace ExampleMod.Projectiles
{

public class TestLaser : ModProjectile
{
Vector2 vector2_1 = new Vector2(70, 60);


private const float MAX_CHARGE = 50f;
private const float MOVE_DISTANCE = 60f;

public float Distance {
get => projectile.ai[0];
set => projectile.ai[0] = value;
}

public float Charge {
get => projectile.localAI[0];
set => projectile.localAI[0] = value;
}

public bool IsAtMaxCharge => Charge == MAX_CHARGE;

public override void SetDefaults() {
projectile.width = 10;
projectile.height = 10;
projectile.friendly = true;
projectile.penetrate = -1;
projectile.tileCollide = false;
projectile.magic = true;
projectile.hide = true;
}

public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor) {
if (IsAtMaxCharge) {

DrawLaser(spriteBatch, Main.projectileTexture[projectile.type], Main.player[projectile.owner].Center + vector2_1,
projectile.velocity, 10, projectile.damage, -1.57f, 1f, 1000f, Color.White, 12);
}
return false;
}

public void DrawLaser(SpriteBatch spriteBatch, Texture2D texture, Vector2 start, Vector2 unit, float step, int damage, float rotation = 0f, float scale = 1f, float maxDist = 2000f, Color color = default(Color), int transDist = 50) {

float r = unit.ToRotation() + rotation;






for (float i = transDist; i <= Distance; i += step) {
Color c = Color.White;
var origin = start + i * unit;
spriteBatch.Draw(texture, origin - (Main.screenPosition),
new Rectangle(0, 26, 28, 26), i < transDist ? Color.Transparent : c, r,
new Vector2(28 * .5f, 26 * .5f), scale, 0, 0);
}

spriteBatch.Draw(texture, start + unit * (transDist - step) - (Main.screenPosition),
new Rectangle(0, 0, 28, 26), Color.White, r, new Vector2(28 * .5f, 26 * .5f), scale, 0, 0);

spriteBatch.Draw(texture, start + (Distance + step) * unit - (Main.screenPosition),
new Rectangle(0, 52, 28, 26), Color.White, r, new Vector2(28 * .5f, 26 * .5f), scale, 0, 0);
}

public override bool? Colliding(Rectangle projHitbox, Rectangle targetHitbox) {
if (!IsAtMaxCharge) return false;

Player player = Main.player[projectile.owner];

Vector2 unit = projectile.velocity;
float point = 0f;
return Collision.CheckAABBvLineCollision(targetHitbox.TopLeft(), targetHitbox.Size(), player.Center + vector2_1,
player.Center + unit * Distance + vector2_1, 22, ref point);
}

public override void OnHitNPC(NPC target, int damage, float knockback, bool crit) {
target.immune[projectile.owner] = 1;
}

public override void AI() {
Player player = Main.player[projectile.owner];
projectile.position = player.Center + projectile.velocity * MOVE_DISTANCE + vector2_1;
projectile.timeLeft = 2;


UpdatePlayer(player);
ChargeLaser(player);

if (Charge < MAX_CHARGE) return;

SetLaserPosition(player);
SpawnDusts(player);
CastLights();
}

private void SpawnDusts(Player player)
{
Vector2 unit = projectile.velocity * -1;
Vector2 dustPos = player.Center + projectile.velocity * Distance + vector2_1;

for (int i = 0; i < 2; ++i) {
float num1 = projectile.velocity.ToRotation() + (Main.rand.Next(2) == 1 ? -1.0f : 1.0f) * 1.57f;
float num2 = (float)(Main.rand.NextDouble() * 0.8f + 1.0f);
Vector2 dustVel = new Vector2((float)Math.Cos(num1) * num2, (float)Math.Sin(num1) * num2);
Dust dust = Main.dust[Dust.NewDust(dustPos, 0, 0, 226, dustVel.X, dustVel.Y)];
dust.noGravity = true;
dust.scale = 1.2f;
dust = Dust.NewDustDirect(Main.player[projectile.owner].Center + vector2_1, 0, 0, 31,
-unit.X * Distance, -unit.Y * Distance);
dust.fadeIn = 0f;
dust.noGravity = true;
dust.scale = 0.88f;
dust.color = Color.Cyan;
}

if (Main.rand.NextBool(5)) {
Vector2 offset = projectile.velocity.RotatedBy(1.57f) * ((float)Main.rand.NextDouble() - 0.5f) * projectile.width;
Dust dust = Main.dust[Dust.NewDust(dustPos + offset - Vector2.One * 4f, 8, 8, 31, 0.0f, 0.0f, 100, new Color(), 1.5f)];
dust.velocity *= 0.5f;
dust.velocity.Y = -Math.Abs(dust.velocity.Y);
unit = dustPos - Main.player[projectile.owner].Center;
unit.Normalize();
dust = Main.dust[Dust.NewDust(Main.player[projectile.owner].Center + vector2_1 * unit, 8, 8, 31, 0.0f, 0.0f, 100, new Color(), 1.5f)];
dust.velocity = dust.velocity * 0.5f;
dust.velocity.Y = -Math.Abs(dust.velocity.Y);
}
}

private void SetLaserPosition(Player player)
{
for (Distance = MOVE_DISTANCE; Distance <= 2200f; Distance += 5f)
{

var start = (player.Center + vector2_1) + projectile.velocity * Distance;
if (!Collision.CanHit(player.Center + vector2_1, 1, 1, start, 1, 1)) {
Distance -= 5f;
break;
}
}
}

private void ChargeLaser(Player player)
{
if (!player.channel) {
projectile.Kill();
}
else {
if (Main.time % 10 < 1 && !player.CheckMana(player.inventory[player.selectedItem].mana, true)) {
projectile.Kill();
}

Vector2 pos = player.Center - new Vector2(10, 10) + vector2_1;
if (Charge < MAX_CHARGE) {
Charge++;
}
int chargeFact = (int)(Charge / 20f);
Vector2 dustVelocity = Vector2.UnitX * 18f;
dustVelocity = dustVelocity.RotatedBy(projectile.rotation - 1.57f);
Vector2 spawnPos = projectile.Center + dustVelocity;
for (int k = 0; k < chargeFact + 1; k++) {
Vector2 spawn = spawnPos + ((float)Main.rand.NextDouble() * 6.28f).ToRotationVector2() * (12f - chargeFact * 2);
Dust dust = Main.dust[Dust.NewDust(pos, 20, 20, 226, projectile.velocity.X / 2f, projectile.velocity.Y / 2f)];
dust.velocity = Vector2.Normalize(spawnPos - spawn) * 1.5f * (10f - chargeFact * 2f) / 10f;
dust.noGravity = true;
dust.scale = Main.rand.Next(10, 20) * 0.05f;
}
}
}

private void UpdatePlayer(Player player)
{
if (projectile.owner == Main.myPlayer) {


Vector2 diff = Main.MouseWorld - player.Center;
diff.Normalize();
projectile.velocity = diff;
projectile.direction = Main.MouseWorld.X > player.position.X ? 1 : -1;
projectile.netUpdate = true;
}
int dir = projectile.direction;
player.ChangeDir(dir); // Set player direction to where we are shooting
player.heldProj = projectile.whoAmI; // Update player's held projectile
player.itemTime = 2; // Set item time to 2 frames while we are used
player.itemAnimation = 2; // Set item animation time to 2 frames while we are used

player.itemRotation = (float)Math.Atan2(projectile.velocity.Y * dir, projectile.velocity.X * dir); // Set the item rotation to where we are shooting
}

private void CastLights()
{
DelegateMethods.v3_1 = new Vector3(0.8f, 0.8f, 1f);
Utils.PlotTileLine(projectile.Center, projectile.Center + projectile.velocity * (Distance - MOVE_DISTANCE), 26, DelegateMethods.CastLight);
}

public override bool ShouldUpdatePosition() => false;


public override void CutTiles() {
DelegateMethods.tilecut_0 = TileCuttingContext.AttackProjectile;
Vector2 unit = projectile.velocity;
Utils.PlotTileLine(projectile.Center, projectile.Center + unit * Distance, (projectile.width + 16) * projectile.scale, DelegateMethods.CutTiles);
}
}
}






Weapon



using ExampleMod.Projectiles;
using ExampleMod.Tiles;
using Terraria;
using Microsoft.Xna.Framework;
using Terraria.ID;
using System;
using Terraria.ModLoader;

namespace ExampleMod.Items.Weapons
{

public class TestStaff : ModItem
{

public override void SetStaticDefaults()
{
Tooltip.SetDefault("This is a modded magic weapon.");
Item.staff[item.type] = true;
}

public override void SetDefaults()
{
item.damage = 40;
item.noMelee = true;
item.magic = true;
item.channel = true;
item.mana = 5;
item.rare = ItemRarityID.Pink;
item.width = 28;
item.height = 30;
item.useTime = 20;
item.UseSound = SoundID.Item13;
item.useStyle = ItemUseStyleID.HoldingOut;
item.shootSpeed = 14f;
item.useAnimation = 20;
item.shoot = ModContent.ProjectileType<TestLaser>();
item.value = Item.sellPrice(silver: 3);

}


public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(ModContent.ItemType<ExampleItem>(), 10);
recipe.AddTile(ModContent.TileType<ExampleWorkbench>());
recipe.SetResult(this);
recipe.AddRecipe();
}
public override bool Shoot(Player player, ref Vector2 position, ref float speedX, ref float speedY, ref int type, ref int damage, ref float knockBack)
{
Projectile.NewProjectile(player.position.X + 70, player.position.Y + 60, 10, 10, ModContent.ProjectileType<TestLaser>(), damage, knockBack, Main.myPlayer, 0.0f, (float)Main.rand.Next(5));
Projectile.NewProjectile(player.position.X + 0, player.position.Y + -85, 10, 10, ModContent.ProjectileType<TestLaser1>(), damage, knockBack, Main.myPlayer, 0.0f, (float)Main.rand.Next(5));
Projectile.NewProjectile(player.position.X + -70, player.position.Y + 60, 10, 10, ModContent.ProjectileType<TestLaser2>(), damage, knockBack, Main.myPlayer, 0.0f, (float)Main.rand.Next(5));







return true;
}
}
}
 
Back
Top Bottom