HeavenStudio/Assets/Scripts/Games/ClappyTrio/ClappyTrio.cs

308 lines
11 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2022-03-14 14:21:05 +00:00
using HeavenStudio.Util;
using Jukebox;
using Jukebox.Legacy;
2021-12-23 22:39:03 +00:00
namespace HeavenStudio.Games.Loaders
{
using static Minigames;
public static class AgbClapLoader
{
public static Minigame AddGame(EventCaller eventCaller) {
return new Minigame("clappyTrio", "The Clappy Trio", "deffff", false, false, new List<GameAction>()
{
2022-08-21 03:13:52 +00:00
new GameAction("clap", "Clap")
{
2022-08-21 03:13:52 +00:00
function = delegate { ClappyTrio.instance.Clap(eventCaller.currentEntity.beat, eventCaller.currentEntity.length); },
resizable = true
},
new GameAction("bop", "Bop")
{
function = delegate { var e = eventCaller.currentEntity; ClappyTrio.instance.BopToggle(e.beat, e.length, e["bop"], e["autoBop"], e["emo"]); },
resizable = true,
parameters = new List<Param>()
{
new Param("bop", true, "Bop", "Should the lions bop?"),
new Param("autoBop", false, "Bop (Auto)", "Should the lions auto bop?"),
new Param("emo", false, "Disable Emotion", "Should the lions just show the neutral face while bopping?")
}
2022-08-21 03:13:52 +00:00
},
new GameAction("prepare", "Prepare Stance")
{
function = delegate { ClappyTrio.instance.Prepare(eventCaller.currentEntity["toggle"] ? 3 : 0); },
2022-08-21 03:13:52 +00:00
parameters = new List<Param>()
{
new Param("toggle", false, "Alt", "Whether or not the alternate version should be played")
}
},
new GameAction("sign", "Sign Enter")
{
function = delegate { var e = eventCaller.currentEntity; ClappyTrio.instance.Sign(e.beat, e.length, e["ease"], e["down"]); },
parameters = new List<Param>()
{
new Param("ease", Util.EasingFunction.Ease.Linear, "Ease", "Which ease should the sign move with?"),
new Param("down", true, "Down", "Should the sign go down?")
},
resizable = true
},
2022-08-21 03:13:52 +00:00
new GameAction("change lion count", "Change Lion Count")
{
function = delegate { ClappyTrio.instance.ChangeLionCount((int)eventCaller.currentEntity["valA"]); },
2022-08-21 03:13:52 +00:00
defaultLength = 0.5f,
parameters = new List<Param>()
{
new Param("valA", new EntityTypes.Integer(3, 8, 3), "Lion Count", "The amount of lions")
}
},
// This is still here for backwards-compatibility but is hidden in the editor
2022-08-21 03:13:52 +00:00
new GameAction("prepare_alt", "")
{
function = delegate { ClappyTrio.instance.Prepare(3); },
hidden = true
},
},
new List<string>() {"agb", "normal"},
"agbclap", "en",
new List<string>() {}
);
}
}
}
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.Games
{
2022-03-12 04:10:13 +00:00
using Scripts_ClappyTrio;
2021-12-24 03:36:16 +00:00
public class ClappyTrio : Minigame
{
public int lionCount = 3;
public List<GameObject> Lion;
2021-12-23 22:39:03 +00:00
[SerializeField] private Sprite[] faces;
private bool isClapping;
private float currentClappingLength;
private float lastClapStart;
private int clapIndex;
private ClappyTrioPlayer ClappyTrioPlayer;
public int misses;
bool shouldBop;
bool doEmotion = true;
public int emoCounter;
public GameEvent bop = new GameEvent();
[SerializeField] Animator signAnim;
double signStartBeat;
float signLength;
Util.EasingFunction.Ease lastEase;
bool signGoDown;
2021-12-23 22:39:03 +00:00
public static ClappyTrio instance { get; set; }
MultiSound clapSounds = null;
BeatAction clapAction = null;
2021-12-23 22:39:03 +00:00
private void Awake()
{
instance = this;
clapSounds = null;
2022-03-27 00:53:34 +00:00
InitLions();
2021-12-23 22:39:03 +00:00
}
public override void OnGameSwitch(double beat)
{
RiqEntity changeLion = GameManager.instance.Beatmap.Entities.FindLast(c => c.datamodel == "clappyTrio/change lion count" && c.beat <= beat);
if(changeLion != null)
{
EventCaller.instance.CallEvent(changeLion, true);
}
}
2021-12-23 22:39:03 +00:00
void Update()
{
var cond = Conductor.instance;
if (cond.ReportBeat(ref bop.lastReportedBeat, bop.startBeat % 1))
{
if (shouldBop) Bop(cond.songPositionInBeatsAsDouble);
}
if (cond.isPlaying && !cond.isPaused)
{
float normalizedBeat = cond.GetPositionFromBeat(signStartBeat, signLength);
if (normalizedBeat > 0 && normalizedBeat <= 1)
{
Util.EasingFunction.Function func = Util.EasingFunction.GetEasingFunction(lastEase);
float newPos = func(0, 1, normalizedBeat);
signAnim.DoNormalizedAnimation(signGoDown ? "Enter" : "Exit", newPos);
}
}
}
public void Sign(double beat, float length, int ease, bool down)
{
SoundByte.PlayOneShotGame("clappyTrio/sign");
signStartBeat = beat;
signLength = length;
lastEase = (Util.EasingFunction.Ease)ease;
signGoDown = down;
}
private void InitLions()
2021-12-23 22:39:03 +00:00
{
float startPos = -3.066667f;
float maxWidth = 12.266668f;
for (int i = 0; i < lionCount; i++)
{
GameObject lion;
if (i == 0)
lion = Lion[0];
else
lion = Instantiate(Lion[0], Lion[0].transform.parent);
lion.transform.localPosition = new Vector3(startPos + ((maxWidth / (lionCount + 1)) * (i + 1)), 0);
if (i > 0)
Lion.Add(lion);
if (i == lionCount - 1)
ClappyTrioPlayer = lion.AddComponent<ClappyTrioPlayer>();
}
if (clapSounds != null)
clapSounds.Delete();
if (clapAction != null)
clapAction.Delete();
2021-12-23 22:39:03 +00:00
}
public void Clap(double beat, float length)
2021-12-23 22:39:03 +00:00
{
2021-12-24 03:36:16 +00:00
ClappyTrioPlayer.clapStarted = true;
ClappyTrioPlayer.canHit = true; // this is technically a lie, this just restores the ability to hit
2021-12-23 22:39:03 +00:00
isClapping = true;
// 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));
2021-12-23 22:39:03 +00:00
}
public void Prepare(int type)
{
for (int i = 0; i < Lion.Count; i++)
{
SetFace(i, type);
}
PlayAnimationAll("Prepare");
SoundByte.PlayOneShotGame("clappyTrio/ready");
}
public void BopToggle(double beat, float length, bool startBop, bool autoBop, bool emo)
{
doEmotion = !emo;
shouldBop = autoBop;
if (startBop)
{
List<BeatAction.Action> bops = new List<BeatAction.Action>();
for (int i = 0; i < length; i++)
{
if (i == 0 && startBop && autoBop) continue;
double spawnBeat = beat + i;
bops.Add(new BeatAction.Action(spawnBeat, delegate { Bop(spawnBeat); }));
if (i == length - 1)
{
bops.Add(new BeatAction.Action(spawnBeat, delegate { misses = 0; }));
}
}
if (bops.Count > 0) BeatAction.New(instance.gameObject, bops);
}
}
public void Bop(double beat)
{
if (doEmotion && emoCounter > 0)
{
if (misses == 0)
{
for (int i = 0; i < Lion.Count; i++)
{
SetFace(i, 1);
}
}
else if (misses > 0)
{
var a = EventCaller.GetAllInGameManagerList("clappyTrio", new string[] { "clap" });
var b = a.FindAll(c => c.beat < beat);
if (b.Count > 0)
{
for (int i = 0; i < Lion.Count; i++)
{
if (i == Lion.Count - 1)
{
SetFace(i, 0);
}
else
{
SetFace(i, 2);
}
}
}
}
emoCounter--;
2021-12-24 03:36:16 +00:00
}
else
{
for (int i = 0; i < Lion.Count; i++)
{
SetFace(i, 0);
}
}
PlayAnimationAll("Bop");
2021-12-23 22:39:03 +00:00
}
public void ChangeLionCount(int lions)
{
for(int i=1; i<lionCount; i++)
{
Destroy(Lion[i]);
}
Lion.RemoveRange(1, lionCount - 1);
lionCount = lions;
SetFace(0, 0);
InitLions();
PlayAnimationAll("Idle");
}
2021-12-23 22:39:03 +00:00
private void PlayAnimationAll(string anim)
{
for (int i = 0; i < Lion.Count; i++)
{
Lion[i].GetComponent<Animator>().Play(anim, -1, 0);
}
2021-12-23 22:39:03 +00:00
}
public void SetFace(int lion, int type)
{
Lion[lion].transform.GetChild(1).GetComponent<SpriteRenderer>().sprite = faces[type];
}
}
}