|
| 1 | +using FMOD.Studio; |
| 2 | +using Microsoft.Xna.Framework; |
| 3 | +using Monocle; |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using YamlDotNet.Serialization; |
| 11 | + |
| 12 | +namespace Celeste.Mod.Ghost { |
| 13 | + public class Ghost : Actor { |
| 14 | + |
| 15 | + public Player Player; |
| 16 | + |
| 17 | + public PlayerSprite Sprite; |
| 18 | + public PlayerHair Hair; |
| 19 | + public int MachineState; |
| 20 | + |
| 21 | + public GhostData Data; |
| 22 | + public int FrameIndex = 0; |
| 23 | + public GhostFrame? ForcedFrame; |
| 24 | + public GhostFrame PrevFrame => ForcedFrame ?? (Data == null ? default(GhostFrame) : Data[FrameIndex - 1]); |
| 25 | + public GhostFrame Frame => ForcedFrame ?? (Data == null ? default(GhostFrame) : Data[FrameIndex]); |
| 26 | + public bool AutoForward = true; |
| 27 | + |
| 28 | + public GhostName Name; |
| 29 | + |
| 30 | + protected float alpha; |
| 31 | + protected float alphaHair; |
| 32 | + |
| 33 | + protected bool playerHadControl; |
| 34 | + |
| 35 | + public Ghost(Player player) |
| 36 | + : this(player, null) { |
| 37 | + } |
| 38 | + public Ghost(Player player, GhostData data) |
| 39 | + : base(player.Position) { |
| 40 | + Player = player; |
| 41 | + Data = data; |
| 42 | + |
| 43 | + Depth = 1; |
| 44 | + |
| 45 | + Sprite = new PlayerSprite(player.Sprite.Mode); |
| 46 | + Sprite.HairCount = player.Sprite.HairCount; |
| 47 | + Add(Hair = new PlayerHair(Sprite)); |
| 48 | + Add(Sprite); |
| 49 | + |
| 50 | + Hair.Color = Player.NormalHairColor; |
| 51 | + } |
| 52 | + |
| 53 | + public override void Added(Scene scene) { |
| 54 | + base.Added(scene); |
| 55 | + |
| 56 | + Hair.Facing = Frame.Facing; |
| 57 | + Hair.Start(); |
| 58 | + UpdateHair(); |
| 59 | + |
| 60 | + if (Data != null && Data.Name != GhostModule.Settings.Name) |
| 61 | + Scene.Add(Name = new GhostName(this, Data.Name)); |
| 62 | + } |
| 63 | + |
| 64 | + public override void Removed(Scene scene) { |
| 65 | + base.Removed(scene); |
| 66 | + |
| 67 | + Name?.RemoveSelf(); |
| 68 | + } |
| 69 | + |
| 70 | + public void UpdateHair() { |
| 71 | + if (!Frame.HasData) |
| 72 | + return; |
| 73 | + |
| 74 | + Hair.Color = Frame.HairColor; |
| 75 | + Hair.Alpha = alphaHair; |
| 76 | + Hair.Facing = Frame.Facing; |
| 77 | + Hair.SimulateMotion = Frame.HairSimulateMotion; |
| 78 | + } |
| 79 | + |
| 80 | + public void UpdateSprite() { |
| 81 | + if (!Frame.HasData) |
| 82 | + return; |
| 83 | + |
| 84 | + Position = Frame.Position; |
| 85 | + Sprite.Rotation = Frame.Rotation; |
| 86 | + Sprite.Scale = Frame.Scale; |
| 87 | + Sprite.Scale.X = Sprite.Scale.X * (float) Frame.Facing; |
| 88 | + Sprite.Color = Frame.Color * alpha; |
| 89 | + |
| 90 | + Sprite.Rate = Frame.SpriteRate; |
| 91 | + Sprite.Justify = Frame.SpriteJustify; |
| 92 | + |
| 93 | + try { |
| 94 | + if (Sprite.CurrentAnimationID != Frame.CurrentAnimationID) |
| 95 | + Sprite.Play(Frame.CurrentAnimationID); |
| 96 | + Sprite.SetAnimationFrame(Frame.CurrentAnimationFrame); |
| 97 | + } catch { |
| 98 | + // Play likes to fail randomly as the ID doesn't exist in an underlying dict. |
| 99 | + // Let's ignore this for now. |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public override void Update() { |
| 104 | + Visible = ((GhostModule.Settings.Mode & GhostModuleMode.Play) == GhostModuleMode.Play); |
| 105 | + Visible &= Frame.HasData; |
| 106 | + if (Data != null && Data.Dead) |
| 107 | + Visible &= GhostModule.Settings.ShowDeaths; |
| 108 | + |
| 109 | + if (Player.InControl && AutoForward && ForcedFrame == null) { |
| 110 | + do { |
| 111 | + FrameIndex++; |
| 112 | + } while ( |
| 113 | + (PrevFrame.HasData && !PrevFrame.InControl) || // Skip any frames we're not in control in. |
| 114 | + (!PrevFrame.HasData && FrameIndex < Data.Frames.Count) // Skip any frames not containing the data chunk. |
| 115 | + ); |
| 116 | + } |
| 117 | + playerHadControl = Player.InControl; |
| 118 | + |
| 119 | + UpdateSprite(); |
| 120 | + UpdateHair(); |
| 121 | + |
| 122 | + if (Data != null && Data.Opacity != null) { |
| 123 | + alpha = Data.Opacity.Value; |
| 124 | + alphaHair = Data.Opacity.Value; |
| 125 | + } else { |
| 126 | + float dist = (Player.Position - Position).LengthSquared(); |
| 127 | + dist -= GhostModule.Settings.InnerRadiusDist; |
| 128 | + if (dist < 0f) |
| 129 | + dist = 0f; |
| 130 | + if (GhostModule.Settings.BorderSize == 0) { |
| 131 | + dist = dist < GhostModule.Settings.InnerRadiusDist ? 0f : 1f; |
| 132 | + } else { |
| 133 | + dist /= GhostModule.Settings.BorderSizeDist; |
| 134 | + } |
| 135 | + alpha = Calc.LerpClamp(GhostModule.Settings.InnerOpacityFactor, GhostModule.Settings.OuterOpacityFactor, dist); |
| 136 | + alphaHair = Calc.LerpClamp(GhostModule.Settings.InnerHairOpacityFactor, GhostModule.Settings.OuterHairOpacityFactor, dist); |
| 137 | + } |
| 138 | + |
| 139 | + Visible &= alpha > 0f; |
| 140 | + |
| 141 | + if (Name != null) |
| 142 | + Name.Alpha = alpha; |
| 143 | + |
| 144 | + base.Update(); |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | +} |
0 commit comments