HeavenStudioPlus/Assets/Scripts/Games/ClappyTrio/ClappyTrio.cs

145 lines
4.6 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2021-12-23 22:39:03 +00:00
using RhythmHeavenMania.Util;
namespace RhythmHeavenMania.Games.ClappyTrio
{
2021-12-24 03:36:16 +00:00
public class ClappyTrio : Minigame
{
2021-12-23 22:39:03 +00:00
[SerializeField] private GameObject LionLeft;
private GameObject LionMiddle;
private GameObject LionPlayer;
[SerializeField] private Sprite[] faces;
public SpriteRenderer lionHeadLeft, lionHeadMiddle, lionHeadPlayer;
2021-12-23 22:39:03 +00:00
private bool isClapping;
private float currentClappingLength;
private float lastClapStart;
private int clapIndex;
private ClappyTrioPlayer ClappyTrioPlayer;
public bool playerHitLast = false;
2021-12-23 22:39:03 +00:00
public static ClappyTrio instance { get; set; }
private void Awake()
{
instance = this;
}
2021-12-24 03:36:16 +00:00
public override void OnGameSwitch()
{
SetFace(0, 0);
SetFace(1, 0);
SetFace(2, 0);
PlayAnimationAll("Idle");
}
2021-12-23 22:39:03 +00:00
private void Start()
{
LionMiddle = Instantiate(LionLeft, LionLeft.transform.parent);
LionMiddle.transform.localPosition = new Vector3(3.1f, 0);
LionPlayer = Instantiate(LionLeft, LionLeft.transform.parent);
LionPlayer.transform.localPosition = new Vector3(6.2f, 0);
ClappyTrioPlayer = LionPlayer.AddComponent<ClappyTrioPlayer>();
lionHeadLeft = LionLeft.transform.GetChild(1).GetComponent<SpriteRenderer>();
lionHeadMiddle = LionMiddle.transform.GetChild(1).GetComponent<SpriteRenderer>();
lionHeadPlayer = LionPlayer.transform.GetChild(1).GetComponent<SpriteRenderer>();
2021-12-23 22:39:03 +00:00
}
private void Update()
{
if (isClapping)
{
float songPosBeat = Conductor.instance.songPositionInBeats;
if (songPosBeat > lastClapStart && songPosBeat < lastClapStart + 1 && clapIndex == 0)
{
SetFace(0, 4);
2021-12-23 22:39:03 +00:00
LionLeft.GetComponent<Animator>().Play("Clap", 0, 0);
Jukebox.PlayOneShotGame("clappyTrio/leftClap");
clapIndex++;
}
else if (songPosBeat > lastClapStart + currentClappingLength && songPosBeat < lastClapStart + (currentClappingLength * 2) && clapIndex == 1)
{
SetFace(1, 4);
2021-12-23 22:39:03 +00:00
LionMiddle.GetComponent<Animator>().Play("Clap", 0, 0);
Jukebox.PlayOneShotGame("clappyTrio/middleClap");
clapIndex++;
}
else if (songPosBeat > lastClapStart + (currentClappingLength * 2 - 0.35f) && clapIndex == 2)
2021-12-23 22:39:03 +00:00
{
ClappyTrioPlayer.SetClapAvailability(lastClapStart + (currentClappingLength * 2 - 0.35f));
2021-12-23 22:39:03 +00:00
clapIndex = 0;
isClapping = false;
currentClappingLength = 0;
2021-12-24 03:36:16 +00:00
ClappyTrioPlayer.clapStarted = false;
2021-12-23 22:39:03 +00:00
}
}
}
public void Clap(float beat, float length)
{
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
playerHitLast = false;
2021-12-23 22:39:03 +00:00
isClapping = true;
lastClapStart = beat;
currentClappingLength = length;
}
public void Prepare(int type)
{
SetFace(0, type);
SetFace(1, type);
SetFace(2, type);
PlayAnimationAll("Prepare");
Jukebox.PlayOneShotGame("clappyTrio/ready");
}
public void Bop()
{
if (playerHitLast)
{
SetFace(0, 1);
SetFace(1, 1);
SetFace(2, 1);
}
2021-12-24 03:36:16 +00:00
else
{
SetFace(0, 2);
SetFace(1, 2);
SetFace(2, 0);
}
PlayAnimationAll("Bop");
2021-12-23 22:39:03 +00:00
}
private void PlayAnimationAll(string anim)
{
2021-12-24 03:36:16 +00:00
LionLeft.GetComponent<Animator>().Play(anim, -1, 0);
LionMiddle.GetComponent<Animator>().Play(anim, -1, 0);
LionPlayer.GetComponent<Animator>().Play(anim, -1, 0);
2021-12-23 22:39:03 +00:00
}
public void SetFace(int lion, int type)
{
2021-12-23 22:39:03 +00:00
if (lion == 0)
lionHeadLeft.sprite = faces[type];
if (lion == 1)
lionHeadMiddle.sprite = faces[type];
if (lion == 2)
lionHeadPlayer.sprite = faces[type];
}
}
}