|
| 1 | +using Celeste.Mod.Entities; |
| 2 | +using Microsoft.Xna.Framework; |
| 3 | +using Monocle; |
| 4 | +using MonoMod.Cil; |
| 5 | +using System; |
| 6 | + |
| 7 | +namespace Celeste.Mod.SpringCollab2020.Triggers { |
| 8 | + [CustomEntity("SpringCollab2020/LeaveTheoBehindTrigger")] |
| 9 | + class LeaveTheoBehindTrigger : Trigger { |
| 10 | + private static bool leaveTheoBehind = false; |
| 11 | + |
| 12 | + public static void Load() { |
| 13 | + IL.Celeste.Level.EnforceBounds += onLevelEnforceBounds; |
| 14 | + On.Celeste.Level.TransitionTo += onLevelTransitionTo; |
| 15 | + } |
| 16 | + |
| 17 | + public static void Unload() { |
| 18 | + IL.Celeste.Level.EnforceBounds -= onLevelEnforceBounds; |
| 19 | + On.Celeste.Level.TransitionTo -= onLevelTransitionTo; |
| 20 | + } |
| 21 | + |
| 22 | + private static void onLevelEnforceBounds(ILContext il) { |
| 23 | + ILCursor cursor = new ILCursor(il); |
| 24 | + |
| 25 | + while (cursor.TryGotoNext(MoveType.After, instr => instr.MatchCallvirt<Tracker>("GetEntity"))) { |
| 26 | + // the only usage of GetEntity gets the TheoCrystal entity on the screen. |
| 27 | + Logger.Log("SpringCollab2020/LeaveTheoBehindTrigger", $"Adding hook to leave Theo behind at {cursor.Index} in IL for Level.EnforceBounds"); |
| 28 | + cursor.EmitDelegate<Func<TheoCrystal, TheoCrystal>>(theo => { |
| 29 | + if (leaveTheoBehind) { |
| 30 | + return null; // pretend there is no Theo, so we can exit the room. |
| 31 | + } |
| 32 | + return theo; |
| 33 | + }); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private static void onLevelTransitionTo(On.Celeste.Level.orig_TransitionTo orig, Level self, LevelData next, Vector2 direction) { |
| 38 | + if (leaveTheoBehind) { |
| 39 | + // freeze all Theo Crystals that the player isn't carrying, to prevent them from crashing the game or being weird. |
| 40 | + Player player = self.Tracker.GetEntity<Player>(); |
| 41 | + foreach (TheoCrystal crystal in self.Tracker.GetEntities<TheoCrystal>()) { |
| 42 | + if (player?.Holding?.Entity != crystal) { |
| 43 | + crystal.RemoveTag(Tags.TransitionUpdate); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + orig(self, next, direction); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public LeaveTheoBehindTrigger(EntityData data, Vector2 offset) : base(data, offset) { } |
| 53 | + |
| 54 | + public override void OnEnter(Player player) { |
| 55 | + base.OnEnter(player); |
| 56 | + leaveTheoBehind = true; |
| 57 | + } |
| 58 | + |
| 59 | + public override void Removed(Scene scene) { |
| 60 | + base.Removed(scene); |
| 61 | + leaveTheoBehind = false; |
| 62 | + } |
| 63 | + |
| 64 | + public override void SceneEnd(Scene scene) { |
| 65 | + base.SceneEnd(scene); |
| 66 | + leaveTheoBehind = false; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments