From 483f094f9c81ee28dc2567124f2f6fbd3ce7a8ab Mon Sep 17 00:00:00 2001 From: FyloZ Date: Sat, 21 Mar 2020 00:44:09 -0400 Subject: [PATCH] Added better support for animated projectiles --- Content/Projectiles/DecimationProjectile.cs | 33 ++++++++++++++++----- Content/Projectiles/HourHandProjectile.cs | 17 +---------- Content/Projectiles/LightningSphere.cs | 5 +--- Content/Projectiles/Scarab.cs | 24 ++++----------- 4 files changed, 33 insertions(+), 46 deletions(-) diff --git a/Content/Projectiles/DecimationProjectile.cs b/Content/Projectiles/DecimationProjectile.cs index df1427f..5ba6d19 100644 --- a/Content/Projectiles/DecimationProjectile.cs +++ b/Content/Projectiles/DecimationProjectile.cs @@ -6,19 +6,26 @@ namespace Decimation.Content.Projectiles { public abstract class DecimationProjectile : ModProjectile { - protected virtual int Damages { get; set; } = 0; + private static readonly int FramesPerSeconds = 60; + + protected virtual int Damages { get; set; } protected virtual DecimationWeapon.DamageType DamageType { get; set; } = DecimationWeapon.DamageType.Melee; protected virtual int AnimationFrames { get; } = -1; - + protected virtual int AnimationFps { get; } = 5; + + private int _frameTime; + protected abstract void Init(); - // public override void SetStaticDefaults() - // { - // if (AnimationFrames >= 0) Main.projFrames[projectile.type] = AnimationFrames; - // } + public override void SetStaticDefaults() + { + if (AnimationFrames >= 0) Main.projFrames[projectile.type] = AnimationFrames; + } public sealed override void SetDefaults() { + _frameTime = FramesPerSeconds / AnimationFps; + this.projectile.aiStyle = 1; this.projectile.height = 16; this.projectile.width = 16; @@ -29,7 +36,7 @@ namespace Decimation.Content.Projectiles this.projectile.penetrate = 0; this.projectile.tileCollide = true; this.projectile.timeLeft = 180; - + Init(); this.projectile.damage = Damages; @@ -53,5 +60,17 @@ namespace Decimation.Content.Projectiles break; } } + + public override bool PreAI() + { + if (++projectile.frameCounter >= _frameTime) + { + projectile.frameCounter = 0; + + if (++projectile.frame >= AnimationFrames) projectile.frame = 0; + } + + return base.PreAI(); + } } } \ No newline at end of file diff --git a/Content/Projectiles/HourHandProjectile.cs b/Content/Projectiles/HourHandProjectile.cs index 157b3bc..6d38551 100644 --- a/Content/Projectiles/HourHandProjectile.cs +++ b/Content/Projectiles/HourHandProjectile.cs @@ -9,11 +9,6 @@ namespace Decimation.Content.Projectiles protected override DecimationWeapon.DamageType DamageType => DecimationWeapon.DamageType.Ranged; protected override int Damages => 45; - public override void SetStaticDefaults() - { - Main.projFrames[projectile.type] = 4; - } - protected override void Init() { projectile.width = 10; @@ -26,20 +21,10 @@ namespace Decimation.Content.Projectiles public override bool PreAI() { - if (++projectile.frameCounter >= 5) - { - projectile.frameCounter = 0; - - if (++projectile.frame >= 4) - { - projectile.frame = 0; - } - } - Lighting.AddLight(projectile.Center, 0f, 0f, 1f); projectile.rotation = projectile.velocity.ToRotation(); - return true; + return base.PreAI(); } } } \ No newline at end of file diff --git a/Content/Projectiles/LightningSphere.cs b/Content/Projectiles/LightningSphere.cs index 343189f..0d9e544 100644 --- a/Content/Projectiles/LightningSphere.cs +++ b/Content/Projectiles/LightningSphere.cs @@ -11,10 +11,7 @@ namespace Decimation.Content.Projectiles { internal class LightningSphere : DecimationProjectile { - public override void SetStaticDefaults() - { - Main.projFrames[projectile.type] = 5; - } + protected override int AnimationFrames => 5; protected override void Init() { diff --git a/Content/Projectiles/Scarab.cs b/Content/Projectiles/Scarab.cs index 5f1d1f9..3b5b330 100644 --- a/Content/Projectiles/Scarab.cs +++ b/Content/Projectiles/Scarab.cs @@ -12,15 +12,12 @@ using Terraria.ModLoader; namespace Decimation.Content.Projectiles { internal class Scarab : DecimationProjectile - { - public override void SetStaticDefaults() - { - Main.projFrames[projectile.type] = 2; - } + { + protected override int Damages => 0; + protected override int AnimationFrames => 2; protected override void Init() { - Damages = 0; projectile.width = 22; projectile.height = 22; projectile.aiStyle = -1; @@ -36,18 +33,8 @@ namespace Decimation.Content.Projectiles if (!player.HasBuff(ModContent.BuffType())) projectile.Kill(); - // Loop through the 2 animation frames, spending 5 ticks on each. - if (++projectile.frameCounter >= 5) - { - projectile.frameCounter = 0; - if (++projectile.frame >= 2) - { - projectile.frame = 0; - } - } - - projectile.velocity.X += (float)Main.rand.Next(-75, 76) * 0.005f; - projectile.velocity.Y += (float)Main.rand.Next(-75, 76) * 0.005f; + projectile.velocity.X += Main.rand.Next(-75, 76) * 0.005f; + projectile.velocity.Y += Main.rand.Next(-75, 76) * 0.005f; if (projectile.velocity.X > 0.5f) projectile.velocity.X = 0.5f; @@ -63,7 +50,6 @@ namespace Decimation.Content.Projectiles projectile.velocity.Y *= -1; // Follow the player - Vector2 velocity = projectile.velocity; float diffX = projectile.Center.X - player.Center.X; float diffY = projectile.Center.Y - player.Center.Y;