Skip to content

Commit

Permalink
Add Spin properties
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicAglialoro committed Sep 15, 2022
1 parent fdd2bcf commit 57ead26
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
using SRXDCustomVisuals.Core;
using System;
using SRXDCustomVisuals.Core;
using UnityEngine;

namespace SRXDCustomVisuals.Behaviors;

public class ParticleEffectController : VisualsController {
[SerializeField] private ParticleSystem particleSystem;

public override IVisualsEvent GetEvent(string key) => key switch {
"Play" => new VisualsEvent(Play),
"Stop" => new VisualsEvent(Stop),
_ => null
};

public override IVisualsProperty GetProperty(string key) => key switch {
"EnableEmission" => new VisualsProperty(EnableEmissionChanged),
_ => null
};

private void Play(IVisualsParams parameters) => particleSystem.Play();

private void Stop(IVisualsParams parameters) => particleSystem.Stop();

private void EnableEmissionChanged(VisualsProperty property) {
var emission = particleSystem.emission;

emission.enabled = property.GetBool();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace SRXDCustomVisuals.Core;

[Serializable]
public class VisualsEventMapping {
public VisualsController target;

public string name;

public VisualsController target;

public VisualsParamMapping[] parameters;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace SRXDCustomVisuals.Core;

[Serializable]
public class VisualsPropertyMapping {
public VisualsController target;

public string name;

public VisualsController target;

public VisualsParamType type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
namespace SRXDCustomVisuals.Core;

public class VisualsProperty : IVisualsProperty {
public static VisualsProperty Empty = new();

private Vector4 currentValue;
private Vector4 value = Vector4.positiveInfinity;
private Action<VisualsProperty> changed;

public VisualsProperty(Action<VisualsProperty> changed = null) => this.changed = changed;

public event Action Changed;

public void SetBool(bool value) => SetFloat(value ? 1f : 0f);

public void SetInt(int value) => SetFloat(value);
Expand All @@ -20,21 +19,21 @@ public class VisualsProperty : IVisualsProperty {

public void SetColor(Color value) => SetValue(value);

public bool GetBool() => currentValue.x > 0f;
public bool GetBool() => value.x > 0f;

public int GetInt() => Mathf.RoundToInt(currentValue.x);
public int GetInt() => Mathf.RoundToInt(value.x);

public float GetFloat() => currentValue.x;
public float GetFloat() => value.x;

public Vector3 GetVector() => currentValue;
public Vector3 GetVector() => value;

public Color GetColor() => currentValue;
public Color GetColor() => value;

private void SetValue(Vector4 value) {
if (value == currentValue)
if (value == this.value)
return;

currentValue = value;
Changed?.Invoke();
this.value = value;
changed?.Invoke(this);
}
}
80 changes: 67 additions & 13 deletions SRXDCustomVisuals/SRXDCustomVisuals.Plugin/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using SMU;
using SMU.Utilities;
using SpinCore.Utility;
using SRXDCustomVisuals.Core;
Expand All @@ -27,6 +26,9 @@ public class Patches {
private static NoteClearType previousClearType;
private static bool holding;
private static bool beatHolding;
private static bool spinningRight;
private static bool spinningLeft;
private static bool scratching;
private static bool invokeHitMatch;
private static bool invokeHitBeat;
private static bool invokeHitSpinRight;
Expand All @@ -41,6 +43,9 @@ public class Patches {
private static IVisualsEvent hitScratchEvent;
private static IVisualsProperty holdingProperty;
private static IVisualsProperty beatHoldingProperty;
private static IVisualsProperty spinningRightProperty;
private static IVisualsProperty spinningLeftProperty;
private static IVisualsProperty scratchingProperty;

private static bool TryGetScene(string uniqueName, CustomVisualsInfo info, out VisualsSceneLoader sceneLoader) {
if (scenes.TryGetValue(uniqueName, out sceneLoader))
Expand Down Expand Up @@ -153,16 +158,23 @@ private static void Track_PlayTrack_Postfix(Track __instance) {
mainCamera.backgroundColor = Color.black;
}

sceneManager.SetScene(currentSceneLoader.Load(new[] { null, mainCamera.transform }));
sceneManager.InitControllers(new VisualsParams(), new VisualsResources());
hitMatchEvent = sceneManager.GetEvent("HitMatch");
hitBeatEvent = sceneManager.GetEvent("HitBeat");
hitSpinRightEvent = sceneManager.GetEvent("HitSpinRight");
hitSpinLeftEvent = sceneManager.GetEvent("HitSpinLeft");
hitTapEvent = sceneManager.GetEvent("HitTap");
hitScratchEvent = sceneManager.GetEvent("HitScratch");
holdingProperty = sceneManager.GetProperty("Holding");
beatHoldingProperty = sceneManager.GetProperty("BeatHolding");
var scene = currentSceneLoader.Load(new[] { null, mainCamera.transform });

Dispatcher.QueueForNextFrame(() => {
sceneManager.SetScene(scene);
sceneManager.InitControllers(new VisualsParams(), new VisualsResources());
hitMatchEvent = sceneManager.GetEvent("HitMatch");
hitBeatEvent = sceneManager.GetEvent("HitBeat");
hitSpinRightEvent = sceneManager.GetEvent("HitSpinRight");
hitSpinLeftEvent = sceneManager.GetEvent("HitSpinLeft");
hitTapEvent = sceneManager.GetEvent("HitTap");
hitScratchEvent = sceneManager.GetEvent("HitScratch");
holdingProperty = sceneManager.GetProperty("Holding");
beatHoldingProperty = sceneManager.GetProperty("BeatHolding");
spinningRightProperty = sceneManager.GetProperty("SpinningRight");
spinningLeftProperty = sceneManager.GetProperty("SpinningLeft");
scratchingProperty = sceneManager.GetProperty("Scratching");
});
}

[HarmonyPatch(typeof(Track), nameof(Track.ReturnToPickTrack)), HarmonyPostfix]
Expand All @@ -187,6 +199,9 @@ private static void ScoreState_UpdateNoteStates_Prefix() {
invokeHitScratch = false;
holding = false;
beatHolding = false;
spinningRight = false;
spinningLeft = false;
scratching = false;
}

[HarmonyPatch(typeof(PlayState.ScoreState), nameof(PlayState.ScoreState.UpdateNoteStates)), HarmonyPostfix]
Expand Down Expand Up @@ -214,6 +229,9 @@ private static void ScoreState_UpdateNoteStates_Postfix() {

holdingProperty.SetBool(holding);
beatHoldingProperty.SetBool(beatHolding);
spinningRightProperty.SetBool(spinningRight);
spinningLeftProperty.SetBool(spinningLeft);
scratchingProperty.SetBool(scratching);
}

[HarmonyPatch(typeof(TrackGameplayLogic), nameof(TrackGameplayLogic.UpdateNoteStateInternal)), HarmonyPrefix]
Expand All @@ -233,7 +251,7 @@ private static void TrackGameplayLogic_UpdateNoteStateInternal_Postfix(PlayState
var drumNote = trackData.NoteData.GetDrumForNoteIndex(noteIndex).GetValueOrDefault();
ref var sustainNoteState = ref playState.scoreState.GetSustainState(drumNote.FirstNoteIndex);

if (!sustainNoteState.IsDoneWith && sustainNoteState.isSustained)
if (sustainNoteState.isSustained && playState.currentTrackTick < trackData.GetNote(drumNote.LastNoteIndex).tick)
beatHolding = true;
}

Expand Down Expand Up @@ -274,11 +292,47 @@ private static void TrackGameplayLogic_UpdateNoteStateInternal_Postfix(PlayState

[HarmonyPatch(typeof(FreestyleSectionLogic), nameof(FreestyleSectionLogic.UpdateFreestyleSectionState)), HarmonyPostfix]
private static void FreestyleSectionLogic_UpdateFreestyleSectionState_Postfix(PlayState playState, int noteIndex) {
var sustainSection = playState.trackData.NoteData.FreestyleSections.GetSectionForNote(noteIndex);

if (!sustainSection.HasValue)
return;

ref var sustainNoteState = ref playState.scoreState.GetSustainState(noteIndex);

if (!sustainNoteState.IsDoneWith && sustainNoteState.isSustained)
if (sustainNoteState.isSustained && playState.currentTrackTick < sustainSection.Value.EndTick)
holding = true;
}

[HarmonyPatch(typeof(SpinSectionLogic), nameof(SpinSectionLogic.UpdateSpinSectionState)), HarmonyPostfix]
private static void SpinSectionLogic_UpdateSpinSectionState_Postfix(PlayState playState, int noteIndex) {
var spinSection = playState.trackData.NoteData.SpinnerSections.GetSectionForNote(noteIndex);

if (!spinSection.HasValue)
return;

ref var sustainNoteState = ref playState.scoreState.GetSustainState(noteIndex);

if (!sustainNoteState.isSustained || playState.currentTrackTick >= playState.trackData.GetNote(spinSection.Value.LastNoteIndex).tick)
return;

if (spinSection.Value.direction == 1)
spinningRight = true;
else
spinningLeft = true;
}

[HarmonyPatch(typeof(ScratchSectionLogic), nameof(ScratchSectionLogic.UpdateScratchSectionState)), HarmonyPostfix]
private static void ScratchSectionLogic_UpdateScratchSectionState_Postfix(PlayState playState, int noteIndex) {
var scratchSection = playState.trackData.NoteData.ScratchSections.GetSectionForNote(noteIndex);

if (!scratchSection.HasValue)
return;

ref var sustainNoteState = ref playState.scoreState.GetSustainState(noteIndex);

if (sustainNoteState.isSustained && playState.currentTrackTick < playState.trackData.GetNote(scratchSection.Value.LastNoteIndex).tick)
scratching = true;
}

[HarmonyPatch(typeof(PlayableTrackDataHandle), "Loading"), HarmonyTranspiler]
private static IEnumerable<CodeInstruction> PlayableTrackDataHandle_Loading_Transpiler(IEnumerable<CodeInstruction> instructions) {
Expand Down

0 comments on commit 57ead26

Please sign in to comment.