|
| 1 | + |
| 2 | +using Celeste.Mod.TASHelper.Utils; |
| 3 | +using Microsoft.Xna.Framework; |
| 4 | +using Monocle; |
| 5 | +namespace Celeste.Mod.TASHelper.Gameplay.AutoWatchEntity; |
| 6 | + |
| 7 | +internal class SwitchGateRenderer : AutoWatchRenderer { |
| 8 | + |
| 9 | + public SwitchGate gate; |
| 10 | + |
| 11 | + public bool Initialized = false; |
| 12 | + |
| 13 | + public List<SwitchLinker> SwitchLinkers; |
| 14 | + |
| 15 | + private static readonly List<SwitchLinker> toRemove = new(); |
| 16 | + public class SwitchLinker : THRenderer { |
| 17 | + public Switch sw; |
| 18 | + |
| 19 | + public SwitchGateRenderer holder; |
| 20 | + |
| 21 | + public Vector2 from; |
| 22 | + |
| 23 | + public SwitchLinker(Switch sw, SwitchGateRenderer holder) { |
| 24 | + this.sw = sw; |
| 25 | + this.holder = holder; |
| 26 | + from = holder.gate.Center; |
| 27 | + } |
| 28 | + |
| 29 | + public void OnRemove() { |
| 30 | + HiresLevelRenderer.Remove(this); |
| 31 | + } |
| 32 | + |
| 33 | + public override void Render() { |
| 34 | + if (DebugRendered && holder.Visible) { |
| 35 | + Monocle.Draw.Line(from * 6f, sw.Entity.Position * 6f, LineColor, Thickness); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static Color LineColor = Color.Lime; |
| 40 | + |
| 41 | + public static float Thickness = 2f; |
| 42 | + } |
| 43 | + public SwitchGateRenderer(RenderMode mode) : base(mode, hasUpdate: true, hasPreUpdate: false) { } |
| 44 | + |
| 45 | + public override void Added(Entity entity) { |
| 46 | + base.Added(entity); |
| 47 | + gate = entity as SwitchGate; |
| 48 | + if (gate.icon?.Color == gate.finishColor || InPosition(gate)) { |
| 49 | + RemoveSelf(); |
| 50 | + } |
| 51 | + Initialized = false; |
| 52 | + } |
| 53 | + |
| 54 | + private void Initialize() { |
| 55 | + // we must wait until switches in last room get unloaded |
| 56 | + // so this cannot be in Added (or say, LoadLevel) |
| 57 | + SwitchLinkers = gate.Scene.Tracker.GetComponents<Switch>().Cast<Switch>().Where(x => !x.Activated).Select(x => new SwitchLinker(x, this)).ToList(); |
| 58 | + foreach (SwitchLinker link in SwitchLinkers) { |
| 59 | + HiresLevelRenderer.AddIfNotPresent(link); |
| 60 | + } |
| 61 | + Initialized = true; |
| 62 | + } |
| 63 | + |
| 64 | + private static bool InPosition(SwitchGate b) { |
| 65 | + return (b.Position + b.movementCounter) == b.node; |
| 66 | + } |
| 67 | + |
| 68 | + public override void UpdateImpl() { |
| 69 | + if (!Initialized) { |
| 70 | + Initialize(); |
| 71 | + } |
| 72 | + foreach (SwitchLinker link in SwitchLinkers) { |
| 73 | + if (link.sw.Activated) { |
| 74 | + toRemove.Add(link); |
| 75 | + } |
| 76 | + } |
| 77 | + foreach (SwitchLinker link in toRemove) { |
| 78 | + SwitchLinkers.Remove(link); |
| 79 | + link.OnRemove(); |
| 80 | + } |
| 81 | + toRemove.Clear(); |
| 82 | + if (SwitchLinkers.IsNullOrEmpty()) { |
| 83 | + RemoveSelf(); |
| 84 | + return; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public override void Removed(Entity entity) { |
| 89 | + base.Removed(entity); |
| 90 | + if (SwitchLinkers.IsNotNullOrEmpty()) { |
| 91 | + foreach (SwitchLinker link in SwitchLinkers) { |
| 92 | + link.OnRemove(); |
| 93 | + } |
| 94 | + SwitchLinkers = null; |
| 95 | + } |
| 96 | + } |
| 97 | + public override void EntityRemoved(Scene scene) { |
| 98 | + base.EntityRemoved(scene); |
| 99 | + if (SwitchLinkers.IsNotNullOrEmpty()) { |
| 100 | + foreach (SwitchLinker link in SwitchLinkers) { |
| 101 | + link.OnRemove(); |
| 102 | + } |
| 103 | + SwitchLinkers = null; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public override void ClearHistoryData() { |
| 108 | + base.ClearHistoryData(); |
| 109 | + Initialized = false; |
| 110 | + if (SwitchLinkers.IsNotNullOrEmpty()) { |
| 111 | + foreach (SwitchLinker link in SwitchLinkers) { |
| 112 | + link.OnRemove(); |
| 113 | + } |
| 114 | + SwitchLinkers = null; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +internal class SwitchGateFactory : IRendererFactory { |
| 120 | + public Type GetTargetType() => typeof(SwitchGate); |
| 121 | + |
| 122 | + public bool Inherited() => true; |
| 123 | + public RenderMode Mode() => Config.SwitchGate; |
| 124 | + public void AddComponent(Entity entity) { |
| 125 | + entity.Add(new SwitchGateRenderer(Mode()).SleepWhenUltraFastforward()); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
0 commit comments