update clappy trio to use PlayerActionEvent

This commit is contained in:
minenice55 2023-01-14 19:23:56 -05:00
parent 6dd7569a6b
commit bea85ff636
3 changed files with 68 additions and 83 deletions

View file

@ -72,9 +72,13 @@ namespace HeavenStudio.Games
public static ClappyTrio instance { get; set; } public static ClappyTrio instance { get; set; }
MultiSound clapSounds = null;
BeatAction clapAction = null;
private void Awake() private void Awake()
{ {
instance = this; instance = this;
clapSounds = null;
InitLions(); InitLions();
} }
public override void OnGameSwitch(float beat) public override void OnGameSwitch(float beat)
@ -108,52 +112,16 @@ namespace HeavenStudio.Games
ClappyTrioPlayer = lion.AddComponent<ClappyTrioPlayer>(); ClappyTrioPlayer = lion.AddComponent<ClappyTrioPlayer>();
} }
if (clapSounds != null)
clapSounds.Delete();
if (clapAction != null)
clapAction.Delete();
} }
private void Update() private void Update()
{ {
if (isClapping)
{
float songPosBeat = Conductor.instance.songPositionInBeats;
for (int i = 0; i < Lion.Count; i++)
{
float length = currentClappingLength * (i);
float lengthplusone = (currentClappingLength * (i + 1));
// i spent like 25 minutes trying to figure out what was wrong with this when i forgot to subtract the currentClapLength :(
if (i == Lion.Count - 1)
{
length = 0;
}
if (songPosBeat > lastClapStart + length && songPosBeat < lastClapStart + lengthplusone && clapIndex == i)
{
if (i == Lion.Count - 1)
{
ClappyTrioPlayer.SetClapAvailability(lastClapStart + (currentClappingLength * (i - 1)), currentClappingLength);
clapIndex = 0;
isClapping = false;
currentClappingLength = 0;
ClappyTrioPlayer.clapStarted = false;
} else
{
SetFace(i, 4);
Lion[i].GetComponent<Animator>().Play("Clap", 0, 0);
// lazy fix rn
if (i > 0)
Jukebox.PlayOneShotGame("clappyTrio/middleClap");
else
Jukebox.PlayOneShotGame("clappyTrio/leftClap");
clapIndex++;
}
break;
}
}
}
} }
public void Clap(float beat, float length) public void Clap(float beat, float length)
@ -163,8 +131,21 @@ namespace HeavenStudio.Games
playerHitLast = false; playerHitLast = false;
isClapping = true; isClapping = true;
lastClapStart = beat;
currentClappingLength = length; // makes the other lions clap
List<MultiSound.Sound> sounds = new List<MultiSound.Sound>();
List<BeatAction.Action> actions = new List<BeatAction.Action>();
for (int i = 0; i < Lion.Count - 1; i++)
{
int idx = i;
sounds.Add(new MultiSound.Sound((i > 0) ? "clappyTrio/middleClap" : "clappyTrio/leftClap", beat + (length * i)));
actions.Add(new BeatAction.Action(beat + (length * i), delegate { SetFace(idx, 4); Lion[idx].GetComponent<Animator>().Play("Clap", 0, 0);}));
}
clapSounds = MultiSound.Play(sounds.ToArray());
clapAction = BeatAction.New(this.gameObject, actions);
// prepare player input
ClappyTrioPlayer.QueueClap(beat, length * (Lion.Count - 1));
} }
public void Prepare(int type) public void Prepare(int type)

View file

@ -8,84 +8,81 @@ namespace HeavenStudio.Games.Scripts_ClappyTrio
{ {
public class ClappyTrioPlayer : PlayerActionObject public class ClappyTrioPlayer : PlayerActionObject
{ {
ClappyTrio game;
private float lastClapBeat; private float lastClapBeat;
private float lastClapLength; private float lastClapLength;
[SerializeField] private bool clapVacant;
public bool clapStarted = false; public bool clapStarted = false;
public bool canHit; public bool canHit;
private GameObject clapEffect; private GameObject clapEffect;
new int aceTimes = 0;
private void Awake() private void Awake()
{ {
game = ClappyTrio.instance;
clapEffect = transform.GetChild(4).GetChild(3).gameObject; clapEffect = transform.GetChild(4).GetChild(3).gameObject;
} }
public override void OnAce()
{
if (aceTimes == 0)
{
Clap(true);
aceTimes++;
}
}
private void Update() private void Update()
{ {
if (clapVacant == true) if (PlayerInput.Pressed() && !game.IsExpectingInputNow(InputType.STANDARD_DOWN))
{
float normalizedBeat = (Conductor.instance.GetPositionFromBeat(lastClapBeat, lastClapLength));
StateCheck(normalizedBeat);
if (normalizedBeat > Minigame.EndTime())
{
clapVacant = false;
lastClapLength = 0;
lastClapBeat = 0;
}
}
if (PlayerInput.Pressed())
{ {
Clap(false); Clap(false);
} }
} }
public void SetClapAvailability(float startBeat, float length) public void QueueClap(float startBeat, float length)
{ {
aceTimes = 0;
lastClapBeat = startBeat; lastClapBeat = startBeat;
clapVacant = true;
lastClapLength = length; lastClapLength = length;
ResetState(); game.ScheduleInput(startBeat, length, InputType.STANDARD_DOWN, Just, Miss, Out);
} }
private void Clap(bool overrideCanHit) private void Just(PlayerActionEvent caller, float state)
{ {
if (state.early || state.perfect || overrideCanHit) if (!canHit) {
Clap(false);
return;
}
if (state >= 1f || state <= -1f) { //todo: proper near miss feedback
Clap(false);
return;
}
Clap(true);
}
private void Miss(PlayerActionEvent caller) {
game.playerHitLast = false;
if (clapStarted)
this.canHit = false;
}
private void Out(PlayerActionEvent caller) {}
private void Clap(bool just)
{
if (just)
{ {
clapEffect.SetActive(true); clapEffect.SetActive(true);
Jukebox.PlayOneShotGame("clappyTrio/rightClap"); Jukebox.PlayOneShotGame("clappyTrio/rightClap");
if (this.canHit) if (this.canHit)
ClappyTrio.instance.playerHitLast = true; game.playerHitLast = true;
} }
else else
{ {
clapEffect.SetActive(false); clapEffect.SetActive(false);
Jukebox.PlayOneShot("miss"); Jukebox.PlayOneShot("miss");
ClappyTrio.instance.playerHitLast = false; game.playerHitLast = false;
if (clapStarted) if (clapStarted)
this.canHit = false; this.canHit = false;
} }
ClappyTrio.instance.SetFace(ClappyTrio.instance.Lion.Count - 1, 4); clapStarted = false;
game.SetFace(game.Lion.Count - 1, 4);
this.GetComponent<Animator>().Play("Clap", 0, 0); this.GetComponent<Animator>().Play("Clap", 0, 0);
} }
} }

View file

@ -13,25 +13,27 @@ namespace HeavenStudio.Util
public class Action public class Action
{ {
public float beat { get; set; } public double beat { get; set; }
public EventCallback function { get; set; } public EventCallback function { get; set; }
public Action(float beat, EventCallback function) public Action(double beat, EventCallback function)
{ {
this.beat = beat; this.beat = beat;
this.function = function; this.function = function;
} }
} }
public static void New(GameObject prefab, List<Action> actions) public static BeatAction New(GameObject prefab, List<Action> actions)
{ {
BeatAction beatAction = prefab.AddComponent<BeatAction>(); BeatAction beatAction = prefab.AddComponent<BeatAction>();
beatAction.actions = actions; beatAction.actions = actions;
return beatAction;
} }
private void Update() private void Update()
{ {
float songPositionInBeats = Conductor.instance.songPositionInBeats; double songPositionInBeats = Conductor.instance.songPositionInBeatsAsDouble;
for (int i = 0; i < actions.Count; i++) for (int i = 0; i < actions.Count; i++)
{ {
@ -42,6 +44,11 @@ namespace HeavenStudio.Util
} }
} }
} }
public void Delete()
{
Destroy(this);
}
} }
} }