Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite Dream Lock Block #3

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 95 additions & 78 deletions Source/Entities/LockBlocks/BaseLockBlock.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,89 @@
using Celeste.Mod.DzhakeHelper;
using Celeste.Mod.DzhakeHelper.Entities;
using Microsoft.Xna.Framework;
using Monocle;
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using Celeste.Mod.DzhakeHelper;
using Celeste.Mod.DzhakeHelper.Entities;

namespace Celeste.Mod.MoreLockBlocks.Entities
{
public abstract class BaseLockBlock : Solid
public abstract class LegacyBaseLockBlock : Solid
{
public EntityID ID => component.ID;

protected internal string overrideSpritePath => component.overrideSpritePath;
public Sprite Sprite => component.Sprite;

protected internal BaseLockBlockComponent.OpeningSettings openingSettings { get => component.openingSettings; set => component.openingSettings = value; }

protected internal bool opening { get => component.opening; set => component.opening = value; }
public bool UnlockingRegistered { get => component.UnlockingRegistered; set => component.UnlockingRegistered = value; }

protected internal bool stepMusicProgress => component.stepMusicProgress;

protected internal string unlockSfxName => component.unlockSfxName;
public LegacyBaseLockBlock(EntityData data, Vector2 offset, EntityID id, string defaultSpriteID = "MoreLockBlocks_generic_lock", string defaultUnlockSfx = "event:/game/03_resort/key_unlock")
: base(data.Position + offset, 32f, 32f, false)
{
Add(component = new BaseLockBlockComponent(this, data, offset, id, defaultSpriteID, defaultSpriteID));
}
protected readonly BaseLockBlockComponent component;
}
public class BaseLockBlockComponent : Component
{
public Solid RealEntity;
public EntityID ID;

protected readonly string overrideSpritePath;
protected internal readonly string overrideSpritePath;
public readonly Sprite Sprite;

protected struct OpeningSettings
public struct OpeningSettings
{
public bool VanillaKeys;

public bool DzhakeHelperKeysNone;
public bool DzhakeHelperKeysAll;
public int DzhakeHelperKeyGroup;
}
protected OpeningSettings openingSettings;
protected internal OpeningSettings openingSettings;

protected bool opening;
protected internal bool opening;
public bool UnlockingRegistered;

protected readonly bool stepMusicProgress;
protected internal readonly bool stepMusicProgress;

protected internal readonly string unlockSfxName;

protected readonly string unlockSfxName;
private PlayerCollider playerCollider;

public BaseLockBlock(EntityData data, Vector2 offset, EntityID id, string defaultSpriteID = "MoreLockBlocks_generic_lock", string defaultUnlockSfx = "event:/game/03_resort/key_unlock") : base(data.Position + offset, 32f, 32f, false)
public BaseLockBlockComponent(Solid This, EntityData data, Vector2 offset, EntityID id, string defaultSpriteID = "MoreLockBlocks_generic_lock", string defaultUnlockSfx = "event:/game/03_resort/key_unlock")
//: base(data.Position + offset, 32f, 32f, false)
: base(true, true)
{
RealEntity = This;
if (MoreLockBlocksModule.Instance.DzhakeHelperLoaded)
{
OnPlayer = default_OnPlayer_DzhakeHelperLoaded;
UnlockRoutine = default_UnlockRoutine_DzhakeHelperLoaded;
TryOpen = default_TryOpen_DzhakeHelperLoaded;
}
else
{
OnPlayer = default_OnPlayer_DzhakeHelperUnloaded;
UnlockRoutine = default_UnlockRoutine_DzhakeHelperUnloaded;
TryOpen = default_TryOpen_DzhakeHelperUnloaded;
}



ID = id;
DisableLightsInside = false;
Add(new PlayerCollider(OnPlayer, new Circle(60f, 16f, 16f)));
RealEntity.DisableLightsInside = false;
RealEntity.Add(playerCollider = new PlayerCollider(OnPlayer, new Circle(60f, 16f, 16f)));

Add(Sprite = string.IsNullOrWhiteSpace(overrideSpritePath = data.Attr("spritePath", "")) ? MoreLockBlocksGFX.SpriteBank.Create(defaultSpriteID) : BuildCustomSprite(overrideSpritePath));
RealEntity.Add(Sprite = string.IsNullOrWhiteSpace(overrideSpritePath = data.Attr("spritePath", "")) ? MoreLockBlocksGFX.SpriteBank.Create(defaultSpriteID) : BuildCustomSprite(overrideSpritePath));
Sprite.Play("idle");
Sprite.Position = new Vector2(Width / 2f, Height / 2f);
Sprite.Position = new Vector2(RealEntity.Width / 2f, RealEntity.Height / 2f);

string dzhakeHelperKeySettings = data.Attr("dzhakeHelperKeySettings", "");
bool _ = int.TryParse(dzhakeHelperKeySettings, out int dzhakeHelperKeyGroup);
Expand All @@ -58,7 +102,7 @@ public BaseLockBlock(EntityData data, Vector2 offset, EntityID id, string defaul
unlockSfxName = SFX.EventnameByHandle(unlockSfxName);
}

protected static Sprite BuildCustomSprite(string spritePath)
protected internal static Sprite BuildCustomSprite(string spritePath)
{
/*
<Justify x="0.5" y="0.5" />
Expand All @@ -79,20 +123,9 @@ protected static Sprite BuildCustomSprite(string spritePath)

#region OnPlayer

protected void OnPlayer(Player player)
{
if (MoreLockBlocksModule.Instance.DzhakeHelperLoaded)
{
OnPlayer_DzhakeHelperLoaded(player);
}
else
{
OnPlayer_DzhakeHelperUnloaded(player);
}
}

protected internal Action<Player> OnPlayer;
[MethodImpl(MethodImplOptions.NoInlining)]
protected virtual void OnPlayer_DzhakeHelperLoaded(Player player)
protected internal virtual void default_OnPlayer_DzhakeHelperLoaded(Player player)
{
if (opening)
{
Expand All @@ -114,7 +147,7 @@ protected virtual void OnPlayer_DzhakeHelperLoaded(Player player)
}

[MethodImpl(MethodImplOptions.NoInlining)]
protected virtual void OnPlayer_DzhakeHelperUnloaded(Player player)
protected internal virtual void default_OnPlayer_DzhakeHelperUnloaded(Player player)
{
if (opening)
{
Expand All @@ -133,23 +166,12 @@ protected virtual void OnPlayer_DzhakeHelperUnloaded(Player player)
#endregion
#region TryOpen

protected void TryOpen(Player player, Follower fol)
{
if (MoreLockBlocksModule.Instance.DzhakeHelperLoaded)
{
TryOpen_DzhakeHelperLoaded(player, fol);
}
else
{
TryOpen_DzhakeHelperUnloaded(player, fol);
}
}

protected internal Action<Player, Follower> TryOpen;
[MethodImpl(MethodImplOptions.NoInlining)]
protected virtual void TryOpen_DzhakeHelperLoaded(Player player, Follower fol)
protected internal virtual void default_TryOpen_DzhakeHelperLoaded(Player player, Follower fol)
{
Collidable = false;
if (!Scene.CollideCheck<Solid>(player.Center, Center))
RealEntity.Collidable = false;
if (!RealEntity.Scene.CollideCheck<Solid>(player.Center, RealEntity.Center))
{
opening = true;
if (fol.Entity is Key key)
Expand All @@ -160,58 +182,47 @@ protected virtual void TryOpen_DzhakeHelperLoaded(Player player, Follower fol)
{
key2.StartedUsing = true;
}
Add(new Coroutine(UnlockRoutine(fol)));
RealEntity.Add(new Coroutine(UnlockRoutine(fol)));
}
Collidable = true;
RealEntity.Collidable = true;
}

[MethodImpl(MethodImplOptions.NoInlining)]
protected virtual void TryOpen_DzhakeHelperUnloaded(Player player, Follower fol)
protected internal virtual void default_TryOpen_DzhakeHelperUnloaded(Player player, Follower fol)
{
Collidable = false;
if (!Scene.CollideCheck<Solid>(player.Center, Center))
RealEntity.Collidable = false;
if (!RealEntity.Scene.CollideCheck<Solid>(player.Center, RealEntity.Center))
{
opening = true;
if (fol.Entity is Key key)
{
key.StartedUsing = true;
}
Add(new Coroutine(UnlockRoutine(fol)));
RealEntity.Add(new Coroutine(UnlockRoutine(fol)));
}
Collidable = true;
RealEntity.Collidable = true;
}

#endregion
#region UnlockRoutine

protected IEnumerator UnlockRoutine(Follower fol)
{
if (MoreLockBlocksModule.Instance.DzhakeHelperLoaded)
{
yield return new SwapImmediately(UnlockRoutine_DzhakeHelperLoaded(fol));
}
else
{
yield return new SwapImmediately(UnlockRoutine_DzhakeHelperUnloaded(fol));
}
}

protected internal Func<Follower, IEnumerator> UnlockRoutine;
[MethodImpl(MethodImplOptions.NoInlining)]
protected virtual IEnumerator UnlockRoutine_DzhakeHelperLoaded(Follower fol)
protected internal virtual IEnumerator default_UnlockRoutine_DzhakeHelperLoaded(Follower fol)
{
SoundEmitter emitter = SoundEmitter.Play(unlockSfxName, this);
SoundEmitter emitter = SoundEmitter.Play(unlockSfxName, RealEntity);
emitter.Source.DisposeOnTransition = true;
Level level = SceneAs<Level>();
Level level = RealEntity.SceneAs<Level>();

Key key = fol.Entity as Key;
CustomKey key2 = fol.Entity as CustomKey;
if (key is not null)
{
Add(new Coroutine(key.UseRoutine(Center + new Vector2(0f, 2f))));
RealEntity.Add(new Coroutine(key.UseRoutine(RealEntity.Center + new Vector2(0f, 2f))));
}
else if (key2 is not null)
{
Add(new Coroutine(key2.UseRoutine(Center + new Vector2(0f, 2f))));
RealEntity.Add(new Coroutine(key2.UseRoutine(RealEntity.Center + new Vector2(0f, 2f))));
}
yield return 1.2f;

Expand Down Expand Up @@ -242,27 +253,27 @@ protected virtual IEnumerator UnlockRoutine_DzhakeHelperLoaded(Follower fol)
}
}

Tag |= Tags.TransitionUpdate;
Collidable = false;
RealEntity.Tag |= Tags.TransitionUpdate;
RealEntity.Collidable = false;
emitter.Source.DisposeOnTransition = false;
yield return Sprite.PlayRoutine("open");

level.Shake();
Input.Rumble(RumbleStrength.Medium, RumbleLength.Medium);
yield return Sprite.PlayRoutine("burst");

RemoveSelf();
RealEntity.RemoveSelf();
}

[MethodImpl(MethodImplOptions.NoInlining)]
protected virtual IEnumerator UnlockRoutine_DzhakeHelperUnloaded(Follower fol)
protected internal virtual IEnumerator default_UnlockRoutine_DzhakeHelperUnloaded(Follower fol)
{
SoundEmitter emitter = SoundEmitter.Play(unlockSfxName, this);
SoundEmitter emitter = SoundEmitter.Play(unlockSfxName, RealEntity);
emitter.Source.DisposeOnTransition = true;
Level level = SceneAs<Level>();
Level level = RealEntity.SceneAs<Level>();

Key key = fol.Entity as Key;
Add(new Coroutine(key.UseRoutine(Center + new Vector2(0f, 2f))));
RealEntity.Add(new Coroutine(key.UseRoutine(RealEntity.Center + new Vector2(0f, 2f))));
yield return 1.2f;

UnlockingRegistered = true;
Expand All @@ -278,16 +289,22 @@ protected virtual IEnumerator UnlockRoutine_DzhakeHelperUnloaded(Follower fol)
yield return null;
}

Tag |= Tags.TransitionUpdate;
Collidable = false;
RealEntity.Tag |= Tags.TransitionUpdate;
RealEntity.Collidable = false;
emitter.Source.DisposeOnTransition = false;
yield return Sprite.PlayRoutine("open");

level.Shake();
Input.Rumble(RumbleStrength.Medium, RumbleLength.Medium);
yield return Sprite.PlayRoutine("burst");

RemoveSelf();
RealEntity.RemoveSelf();
}

internal void Remove()
{
playerCollider.RemoveSelf();
Sprite.RemoveSelf();
}

#endregion
Expand Down
Loading