|
| 1 | +using Celeste.Mod.Entities; |
| 2 | +using Microsoft.Xna.Framework; |
| 3 | +using Monocle; |
| 4 | +using System.Collections; |
| 5 | +using System.Reflection; |
| 6 | + |
| 7 | +namespace Celeste.Mod.SpringCollab2020.Entities { |
| 8 | + [CustomEntity("SpringCollab2020/UnderwaterSwitchController")] |
| 9 | + class UnderwaterSwitchController : Entity { |
| 10 | + private static FieldInfo waterFill = typeof(Water).GetField("fill", BindingFlags.NonPublic | BindingFlags.Instance); |
| 11 | + |
| 12 | + private string flag; |
| 13 | + private Water water; |
| 14 | + |
| 15 | + public UnderwaterSwitchController(EntityData data, Vector2 offset) : base(data.Position + offset) { |
| 16 | + flag = data.Attr("flag"); |
| 17 | + |
| 18 | + Add(new Coroutine(Routine())); |
| 19 | + |
| 20 | + Add(new TransitionListener() { |
| 21 | + OnInBegin = () => { |
| 22 | + // when transitioning in a room with a controller and the flag is already set, spawn water right away |
| 23 | + Session session = SceneAs<Level>().Session; |
| 24 | + if (session.GetFlag(flag)) { |
| 25 | + spawnWater(session.LevelData.Bounds); |
| 26 | + } |
| 27 | + } |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + public IEnumerator Routine() { |
| 32 | + Session session = SceneAs<Level>().Session; |
| 33 | + while (true) { |
| 34 | + // wait until the flag is set. |
| 35 | + while (!session.GetFlag(flag)) { |
| 36 | + yield return null; |
| 37 | + } |
| 38 | + |
| 39 | + // spawn water. |
| 40 | + if (water == null) { |
| 41 | + spawnWater(session.LevelData.Bounds); |
| 42 | + } |
| 43 | + |
| 44 | + // wait until the flag is set. |
| 45 | + while (session.GetFlag(flag)) { |
| 46 | + yield return null; |
| 47 | + } |
| 48 | + |
| 49 | + // make water go away. |
| 50 | + Scene.Remove(water); |
| 51 | + water = null; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private void spawnWater(Rectangle levelBounds) { |
| 56 | + // flood the room with water, make the water 10 pixels over the top to prevent a "splash" effect when going in a room above. |
| 57 | + water = new Water(new Vector2(levelBounds.Left, levelBounds.Top - 10), |
| 58 | + false, false, levelBounds.Width, levelBounds.Height + 10); |
| 59 | + |
| 60 | + // but we don't want the water to render off-screen, because it is visible on upwards transitions. |
| 61 | + Rectangle fill = (Rectangle) waterFill.GetValue(water); |
| 62 | + fill.Y += 10; |
| 63 | + fill.Height -= 10; |
| 64 | + waterFill.SetValue(water, fill); |
| 65 | + |
| 66 | + Scene.Add(water); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments