|
| 1 | +using System; |
| 2 | +using Microsoft.Xna.Framework; |
| 3 | +using Microsoft.Xna.Framework.Graphics; |
| 4 | +using Terraria; |
| 5 | +using Terraria.DataStructures; |
| 6 | +using Terraria.GameContent; |
| 7 | +using Terraria.ID; |
| 8 | +using Terraria.ModLoader; |
| 9 | +using TerrariaOverhaul.Common.Charging; |
| 10 | +using TerrariaOverhaul.Core.ItemComponents; |
| 11 | +using TerrariaOverhaul.Core.Time; |
| 12 | + |
| 13 | +namespace TerrariaOverhaul.Common.Archery; |
| 14 | + |
| 15 | +[Autoload(Side = ModSide.Client)] |
| 16 | +public sealed class ItemArrowRendering : ItemComponent |
| 17 | +{ |
| 18 | + public bool Visible { get; private set; } |
| 19 | + public byte ArrowCount { get; private set; } = 1; |
| 20 | + public float AnimationProgress { get; private set; } |
| 21 | + public Projectile? AmmoProjectileSample { get; private set; } |
| 22 | + |
| 23 | + public override void SetDefaults(Item item) |
| 24 | + { |
| 25 | + // Hardcoded for now. |
| 26 | + if (item.type == ItemID.Tsunami) { |
| 27 | + ArrowCount = 5; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public override void HoldItem(Item item, Player player) |
| 32 | + { |
| 33 | + Visible = Enabled && UpdateIsVisible(item, player); |
| 34 | + } |
| 35 | + |
| 36 | + private bool UpdateIsVisible(Item item, Player player) |
| 37 | + { |
| 38 | + // Only render while a use is in progress |
| 39 | + if (!player.ItemAnimationActive) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + // Must be charging the weapon |
| 44 | + if (item.TryGetGlobalItem(out ItemPowerAttacks powerAttacks) && powerAttacks.IsCharging) { |
| 45 | + AnimationProgress = powerAttacks.Charge.Progress; |
| 46 | + } else if (item.TryGetGlobalItem(out ItemPrimaryUseCharging primaryCharging) && primaryCharging.Charge.Active) { |
| 47 | + AnimationProgress = primaryCharging.Charge.Progress; |
| 48 | + } else { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + // Update ammo kind only at the beginning of the animation. |
| 53 | + if (!Visible) { |
| 54 | + player.PickAmmo(item, out int projectileType, out _, out _, out _, out _, dontConsume: true); |
| 55 | + |
| 56 | + if (!ContentSamples.ProjectilesByType.TryGetValue(projectileType, out var projectile) || projectile == null) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + AmmoProjectileSample = projectile; |
| 61 | + } |
| 62 | + |
| 63 | + return true; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +[Autoload(Side = ModSide.Client)] |
| 68 | +public sealed class ArrowPlayerDrawLayer : PlayerDrawLayer |
| 69 | +{ |
| 70 | + public override Position GetDefaultPosition() |
| 71 | + => new AfterParent(PlayerDrawLayers.HeldItem); |
| 72 | + |
| 73 | + public override bool GetDefaultVisibility(PlayerDrawSet drawInfo) |
| 74 | + { |
| 75 | + // Don't render as after-image |
| 76 | + return drawInfo.shadow == 0f; |
| 77 | + } |
| 78 | + |
| 79 | + protected override void Draw(ref PlayerDrawSet drawInfo) |
| 80 | + { |
| 81 | + var player = drawInfo.drawPlayer; |
| 82 | + |
| 83 | + // Must be holding a weapon with the ItemArrowRendering component. |
| 84 | + if (player.HeldItem is not { IsAir: false } weapon |
| 85 | + || !weapon.TryGetGlobalItem(out ItemArrowRendering arrowRendering) |
| 86 | + || arrowRendering is not { Enabled: true, Visible: true, AmmoProjectileSample: Projectile projectile }) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Attack info |
| 91 | + float itemRotation = player.itemRotation + (player.direction < 0 ? MathHelper.Pi : 0f); |
| 92 | + Vector2 itemDirection = itemRotation.ToRotationVector2(); |
| 93 | + |
| 94 | + // Texture info |
| 95 | + Main.instance.LoadProjectile(projectile.type); |
| 96 | + |
| 97 | + var projectileTexture = TextureAssets.Projectile[projectile.type].Value; |
| 98 | + var frame = new SpriteFrame(1, (byte)Main.projFrames[projectile.type]); |
| 99 | + var sourceRectangle = frame.GetSourceRectangle(projectileTexture); |
| 100 | + |
| 101 | + // Animation |
| 102 | + const float StartOffset = 32f; |
| 103 | + const float EndOffset = 14f; |
| 104 | + |
| 105 | + float animationProgress = arrowRendering.AnimationProgress; |
| 106 | + float positionOffsetEasing = 1f - MathF.Pow(1f - animationProgress, 2f); |
| 107 | + var positionOffset = itemDirection * MathHelper.Lerp(StartOffset, EndOffset, positionOffsetEasing); |
| 108 | + float rotationOffsetIntensity = Math.Min(1f, MathF.Pow(animationProgress, 5f) + 0.1f); |
| 109 | + float rotationOffset = MathF.Sin(TimeSystem.RenderTime * 50f) * MathHelper.ToRadians(7.5f) * rotationOffsetIntensity; |
| 110 | + |
| 111 | + for (int i = 0; i < arrowRendering.ArrowCount; i++) { |
| 112 | + int arrowStep = (int)MathF.Ceiling(i * 0.5f) * (i % 2 == 0 ? 1 : -1); |
| 113 | + var itemDirectionRight = itemDirection.RotatedBy(MathHelper.PiOver2); |
| 114 | + var arrowOffset = (itemDirectionRight * arrowStep * 8f) + (itemDirection * MathF.Abs(arrowStep) * -2f); |
| 115 | + |
| 116 | + // Drawing info |
| 117 | + var position = player.Center + positionOffset + arrowOffset; |
| 118 | + float rotation = itemRotation - MathHelper.PiOver2 * player.direction + rotationOffset; |
| 119 | + Vector2 origin = sourceRectangle.Size() * 0.5f; |
| 120 | + float scale = 1.0f; |
| 121 | + var effect = player.direction > 0 ? SpriteEffects.FlipVertically : SpriteEffects.None; |
| 122 | + var color = Lighting.GetColor(position.ToTileCoordinates()); |
| 123 | + |
| 124 | + // Drawing |
| 125 | + drawInfo.DrawDataCache.Add(new DrawData(projectileTexture, position - Main.screenPosition, sourceRectangle, color, rotation, origin, scale, effect, 0)); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments