Like with any other weapon that shoots a projectile, you just want to assign
item.shoot and
item.shootSpeed (where
shoot should be set to the ID of the projectile and
shootSpeed to the speed at which you want to shoot your projectile (try something like
10)).
It's not really an 'animation'. Strange as it may seem, spears are actually projectiles. Luckily the code ain't too long. Take a look at the following:
Code:
public override bool PreAI()
{
float protractSpeed = 1.5F;
float retractSpeed = 1.4F;
Vector2 heldPos = Main.player[projectile.owner].RotatedRelativePoint(Main.player[projectile.owner].MountedCenter, true);
projectile.direction = Main.player[projectile.owner].direction;
Main.player[projectile.owner].heldProj = projectile.whoAmI;
Main.player[projectile.owner].itemTime = Main.player[projectile.owner].itemAnimation;
projectile.position.X = heldPos.X - (float)(projectile.width / 2);
projectile.position.Y = heldPos.Y - (float)(projectile.height / 2);
if (!Main.player[projectile.owner].frozen)
{
if (projectile.ai[0] == 0f)
{
projectile.ai[0] = 3f;
projectile.netUpdate = true;
}
if (Main.player[projectile.owner].itemAnimation < Main.player[projectile.owner].itemAnimationMax / 3)
{
projectile.ai[0] -= retractSpeed;
}
else
{
projectile.ai[0] += protractSpeed;
}
}
projectile.position += projectile.velocity * projectile.ai[0];
if (Main.player[projectile.owner].itemAnimation == 0)
{
projectile.Kill();
}
projectile.rotation = (float)Math.Atan2((double)projectile.velocity.Y, (double)projectile.velocity.X) + 2.355f;
if (projectile.spriteDirection == -1)
{
projectile.rotation -= 1.57f;
}
return false;
}
You can change the speed of the spear by modifying the top-most variables. DO NOTE: that spears are drawn different from other projectiles. The 'tip of the spear' should be in the top left corner of your sprite. The following drawing code would make your spear draw properly then:
Code:
public override bool PreDraw()
{
Vector2 zero = Vector2.Zero;
SpriteEffects effects1 = SpriteEffects.None;
if (projectile.spriteDirection == -1)
{
zero.X = (float)Main.projectileTexture[projectile.type].Width;
effects1 = SpriteEffects.FlipHorizontally;
}
Main.spriteBatch.Draw(Main.projectileTexture[projectile.type], new Vector2(projectile.position.X - Main.screenPosition.X + (float)(projectile.width / 2), projectile.position.Y - Main.screenPosition.Y + (float)(projectile.height / 2) + projectile.gfxOffY), new Microsoft.Xna.Framework.Rectangle?(new Microsoft.Xna.Framework.Rectangle(0, 0, Main.projectileTexture[projectile.type].Width, Main.projectileTexture[projectile.type].Height)), projectile.GetAlpha(lightColor), projectile.rotation, zero, projectile.scale, effects1, 0.0f);
return false;
}
The following is how the dusts/gores are spawned:
Code:
for (int i = 0; i < 10; i ++)
{
Dust.NewDust(projectile.position, projectile.width, projectile.height, 57, projectile.velocity.X * 0.1f, projectile.velocity.Y * 0.1f, 150, default(Color), 1.2f);
}
for (int j = 0; j < 3; j ++)
{
Gore.NewGore(projectile.position, new Vector2(projectile.velocity.X * 0.05f, projectile.velocity.Y * 0.05f), Main.rand.Next(16, 18), 1f);
}