Creating a projectile trail that uses a copy of the parent projectile

POCKETS

Terrarian
OK, so I've asked about projectile trails in the past, but that involved dust, and that's not what I'd like to accomplish here.

In my previous thread on creating a dust trail at a set distance behind the projectile, the code that was used utilized a ring array. I don't know that this would be an efficient way of handling this this time around, but we'll see ;-)

Alright, so here's what I am trying to accomplish -

I need a trail behind the parent projectile that are basically copies of the original projectile at set distances behind that original projectile (a distance that I can set on a per copy basis). I also need to control their alpha values individually (each separate copy).

At this point, you may be thinking about ProjectileID.Sets.TrailCacheLength[projectile.type] which basically stores the old positions and uses that as a trail, just like the example bullet. However, this won't work for me. Using this, the projectile copies are much too close together, I cannot set the distance for each copy, and I cannot control their alpha values individually.

Here's where it gets fun -

These projectiles also have homing capabilities, so the copies also need to follow the "path" of the original. As I mentioned above, in the other thread regarding dust, a ring array was used, but I'm not certain that would apply here. It has been suggested that I use Projectile.NewProjectile to spawn the copies using a List<> of the original's stored positions, and I like this idea, but I'm not sure this would be viable.

Whether this requires a list or an array, I need to be able to control not only the distance for each copy, but also the alpha value per copy. Unfortunately, I don't think I can use dust in this particular situation, as the projectile has a distinct shape, so the shape needs to rotate with the original.
 
Alright, so I just wanted to post an update on this so that there's a record of where I'm at with this so far.

I've found a way to do this using the trail cache from the example bullet, but it's not ideal. This does allow me to adjust the positions of each copy manually, but the alpha is based on the TrailCacheLength. Therefore, I cannot set the alpha for each copy individually.

OK, so here's the code I am currently using. I've removed the for loop that creates several copies using the trail cache value, and I've replaced the variables with actual integers -

C#:
ProjectileID.Sets.TrailCacheLength[projectile.type] = 7;
ProjectileID.Sets.TrailingMode[projectile.type] = 0;


public override bool PreDraw(SpriteBatch spriteBatch, Color lightColor)
{
    //Redraw the projectile with the color not influenced by light
    Vector2 drawOrigin = new Vector2(Main.projectileTexture[projectile.type].Width * 0.5f, projectile.height * 0.5f);
    Vector2 drawPos = projectile.oldPos[6] - Main.screenPosition + drawOrigin + new Vector2(0f, projectile.gfxOffY);
    Color color = projectile.GetAlpha(lightColor) * ((float)(projectile.oldPos.Length - 6) / (float)projectile.oldPos.Length);
    spriteBatch.Draw(mod.GetTexture("Items/Projectiles/TestProjectileItem"), drawPos, null, color, projectile.rotation, drawOrigin, projectile.scale, SpriteEffects.None, 0f);

    Vector2 drawOrigin2 = new Vector2(Main.projectileTexture[projectile.type].Width * 0.5f, projectile.height * 0.5f);
    Vector2 drawPos2 = projectile.oldPos[3] - Main.screenPosition + drawOrigin2 + new Vector2(0f, projectile.gfxOffY);
    Color color2 = projectile.GetAlpha(lightColor) * ((float)(projectile.oldPos.Length - 3) / (float)projectile.oldPos.Length);
    spriteBatch.Draw(mod.GetTexture("Items/Projectiles/TestProjectileItem"), drawPos2, null, color2, projectile.rotation, drawOrigin, projectile.scale, SpriteEffects.None, 0f);
    return true;
}

As you can see, I've created two instances of the texture for the trail. The first instance is the last position in the cache, which means that's going to have the most transparent alpha value. The second instance is the fourth position (I think - does it start at 0?). Anyhow, seeing as the alpha values are based on the positions, this is not ideal, but will work in a pinch.

I need to know if there's a way to control the alpha individually, or if by using the trail cache, I'm basically stuck using the positions for the alpha.

If there's a better way to do this, I'm more than willing to give it a try :)
 
Back
Top Bottom