using System; using System.Collections.Generic; using System.IO; using System.Linq; using CalamityMod.Buffs.DamageOverTime; using CalamityMod.NPCs; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; using Terraria; using Terraria.Audio; using Terraria.GameContent; using Terraria.ID; using Terraria.ModLoader; namespace CalamityMod.Projectiles.BaseProjectiles { /// /// Implementation to make ModProjectile worms much easier to make. /// Modified version of BaseWormNPC to function on a ModProjectile instead of ModNPC /// public abstract class BaseWormProjectile : ModProjectile { #region Abstracts / Commonly Overridden Fields /// /// The amount of segments of this worm. This does not include the head /// public abstract int SegmentCount { get; } /// /// A list of the offsets to the next segment in the worm from this segment /// DOES include the head /// public abstract List SegmentTypePositionOffsets { get; } /// /// A list of all textures for the segments to draw with /// does NOT include the head /// public abstract List SegmentTextures { get; } /// /// Offsets for drawing each segment /// public List SegmentTypeDrawOffsets = new List(); /// /// How far through the current animation this worm is /// public float AnimationFrame = 0; /// /// The active animation for the worm /// public WormAnimation ActiveAnimation = null; #endregion #region Segments /// /// The textures for each segment type of this worm. Works like getting a texture from TextureAssets /// public List> SegmentTextureAssets { get { if (internalTexAssets.Count == 0) for (var i = 0; i < SegmentTextures.Count; i++) { internalTexAssets.Add(ModContent.Request(SegmentTextures[i])); if (SegmentTypeDrawOffsets.Count <= i) { SegmentTypeDrawOffsets.Add(Vector2.Zero); } } return internalTexAssets; } } /// /// Internal list that stores the textureassets. /// Use SegmentTextureAssets to get the data stored here. /// private List> internalTexAssets = new List>(); public List Segments = new(); public enum SegmentFollowLogic { Regular = 0, //Traditional worm segment logic Exact = 1, //Follows the path the head took exactly // In the future, add a segment logic that supports solid tile collisions and forces applied to *any* segment } /// /// Which type of segment following logic this worm should currently use /// public SegmentFollowLogic SegmentFollowType; /// /// How rigid the segment should be when using default segment logic /// public float SegmentRigidity = 0.2f; /// /// The points used by ExactSegmentLogic to exactly follow the head /// private List segmentPoints = new List(); /// /// Updates the positions of the segments based on the value set in SegmentFollowType /// public void UpdateSegments() { Projectile.position += Projectile.velocity; //Update this segment's movement so that all other segments use this as the base. This is undone at the end of the method. if (ActiveAnimation is not null) { ActiveAnimation.ApplyAnimationFrame(Projectile, AnimationFrame); AnimationFrame++; if (AnimationFrame > ActiveAnimation.AnimationKeyframes.Keys.Max()) { AnimationFrame = 0; ActiveAnimation = null; } } else { if (segmentPoints.Count < 1 || segmentPoints[0].Distance(Projectile.Center) > 8) { segmentPoints.Insert(0, Projectile.Center); } while (segmentPoints.Count > 300) { segmentPoints.RemoveAt(segmentPoints.Count - 1); } switch (SegmentFollowType) { case (SegmentFollowLogic.Regular): RegularSegmentLogic(); break; case (SegmentFollowLogic.Exact): ExactSegmentLogic(); break; } } Projectile.position -= Projectile.velocity; } private void RegularSegmentLogic() { for (int i = 0; i < Segments.Count; i++) { float segmentDistance = SegmentTypePositionOffsets[0]; var thisSeg = Segments[i]; var aheadSeg = new BaseWormSegment(this); if (i != 0) { aheadSeg = Segments[i - 1]; segmentDistance = SegmentTypePositionOffsets[Segments[i - 1].segmentType + 1]; } segmentDistance *= Projectile.scale; Vector2 nexSegDir = aheadSeg.Center - thisSeg.Center; if (aheadSeg.rotation != thisSeg.rotation) { nexSegDir = nexSegDir.RotatedBy(MathHelper.WrapAngle(aheadSeg.rotation - thisSeg.rotation) * SegmentRigidity); nexSegDir = nexSegDir.MoveTowards((aheadSeg.rotation - thisSeg.rotation).ToRotationVector2(), 1f); } thisSeg.rotation = nexSegDir.ToRotation() + MathHelper.PiOver2; thisSeg.Center = aheadSeg.Center - nexSegDir.SafeNormalize(Vector2.Zero) * segmentDistance; } } private void ExactSegmentLogic() { float dist = 40f; int segmentPointInUse = 0; for (int i = 0; i < Segments.Count; i++) { var thisSeg = Segments[i]; var aheadSeg = new BaseWormSegment(this); if (i != 0) aheadSeg = Segments[i - 1]; bool hasMoved = false; while (segmentPointInUse < segmentPoints.Count) { if (segmentPointInUse == 0) { if (aheadSeg.Center.Distance(segmentPoints[0]) >= dist) { thisSeg.Center = aheadSeg.Center + aheadSeg.Center.DirectionTo(segmentPoints[0]) * dist; Segments[i].velocity = Segments[i].Center.DirectionTo(aheadSeg.Center); Segments[i].rotation = Segments[i].velocity.ToRotation() + MathHelper.PiOver2; hasMoved = true; break; } else { segmentPointInUse++; } } else { if (aheadSeg.Center.Distance(segmentPoints[segmentPointInUse]) >= dist) { thisSeg.Center = aheadSeg.Center + aheadSeg.Center.DirectionTo(segmentPoints[segmentPointInUse]) * dist; Segments[i].velocity = Segments[i].Center.DirectionTo(aheadSeg.Center); Segments[i].rotation = Segments[i].velocity.ToRotation() + MathHelper.PiOver2; hasMoved = true; break; } else { segmentPointInUse++; } } } if (!hasMoved && segmentPointInUse >= segmentPoints.Count) { thisSeg.Center = segmentPoints[segmentPoints.Count - 1]; Segments[i].velocity = Segments[i].Center.DirectionTo(aheadSeg.Center); Segments[i].rotation = Segments[i].velocity.ToRotation() + MathHelper.PiOver2; hasMoved = true; break; } } } #endregion #region Defaults public override void SetStaticDefaults() { ProjectileID.Sets.DrawScreenCheckFluff[Type] = 1600; //100 tiles offscreen } public override void SetDefaults() { for (var i = 0; i < SegmentCount - 1; i++) { Segments.Add(new BaseWormSegment(this, 0)); } Segments.Add(new BaseWormSegment(this, 1)); } #endregion #region Draw public override bool PreDraw(ref Color lightColor) { for (int i = Segments.Count - 1; i >= 0; i--) { DrawSegment(ref lightColor, Segments[i]); } Main.spriteBatch.Draw(TextureAssets.Projectile[Type].Value, Projectile.Center - Main.screenPosition, null, lightColor * Projectile.Opacity, Projectile.rotation, TextureAssets.Projectile[Type].Value.Size() / 2, Projectile.scale, SpriteEffects.None, 1); return false; } public virtual void DrawSegment(ref Color lightColor, BaseWormSegment segment) { var color = Lighting.GetColor(segment.Center.ToTileCoordinates()); if (!SegmentTextureAssets.IndexInRange(segment.segmentType)) { return; } var tex = SegmentTextureAssets[segment.segmentType].Value; Main.spriteBatch.Draw(tex, segment.Center - Main.screenPosition, null, color *segment.Opacity, segment.rotation, tex.Size() / 2 + (SegmentTypeDrawOffsets[segment.segmentType]), Projectile.scale, SpriteEffects.None, 1); } #endregion #region Netcode public override void SendExtraAI(BinaryWriter writer) { writer.Write(AnimationFrame); } public override void ReceiveExtraAI(BinaryReader reader) { AnimationFrame = reader.ReadSingle(); } #endregion public override bool? Colliding(Rectangle projHitbox, Rectangle targetHitbox) { for (var i = 0; i < Segments.Count; i++) { var prevSeg = new BaseWormSegment(this); if (i != 0) { prevSeg = Segments[i - 1]; } float cpoint = 0; if (Collision.CheckAABBvLineCollision(targetHitbox.TopLeft(), targetHitbox.Size(), prevSeg.Center, Segments[i].Center, 16, ref cpoint)) { return true; } } return base.Colliding(projHitbox, targetHitbox); } } }