HeavenStudio/Assets/Scripts/Games/MonkeyWatch/WatchMonkey.cs
2023-08-07 21:25:21 +02:00

111 lines
3.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HeavenStudio.Util;
namespace HeavenStudio.Games.Scripts_MonkeyWatch
{
public class WatchMonkey : MonoBehaviour
{
private Animator anim;
private Animator holeAnim;
[Header("Properties")]
[SerializeField] private bool isPink;
private MonkeyWatch game;
private int direction = 0;
public double monkeyBeat;
private PlayerActionEvent inputEvent;
private void Awake()
{
game = MonkeyWatch.instance;
anim = GetComponent<Animator>();
}
private void Update()
{
if (inputEvent != null && inputEvent.enabled)
{
if (PlayerInput.Pressed() && !game.IsExpectingInputNow(InputType.STANDARD_DOWN) && !game.monkeyClockArrow.PlayerIsClapAnim())
{
SoundByte.PlayOneShot("miss");
Miss(inputEvent);
inputEvent.Disable();
game.ScoreMiss();
}
}
}
public void Appear(double beat, bool instant, Animator hole, int dir)
{
monkeyBeat = beat;
direction = dir;
holeAnim = hole;
holeAnim.DoScaledAnimationAsync("HoleOpen", 0.4f, instant ? 1 : 0);
anim.DoScaledAnimationAsync(isPink ? "PinkAppear" : "Appear", 0.4f, instant ? 1 : 0);
}
public void Disappear()
{
holeAnim.DoScaledAnimationAsync("HoleClose", 0.4f);
anim.DoScaledAnimationAsync(isPink ? "PinkAppear" : "Appear", -0.4f, 1);
StartCoroutine(DisappearCoroutine());
}
private IEnumerator DisappearCoroutine()
{
yield return !anim.IsAnimationNotPlaying();
Destroy(gameObject);
}
public void Prepare(double prepareBeat, double inputBeat)
{
anim.DoScaledAnimationAsync(isPink ? "PinkPrepare" + direction : "Prepare" + direction, 0);
inputEvent = game.ScheduleInput(prepareBeat, inputBeat - prepareBeat, InputType.STANDARD_DOWN, Just, Miss, Empty);
BeatAction.New(gameObject, new List<BeatAction.Action>()
{
new BeatAction.Action(prepareBeat, delegate
{
if (inputEvent != null && inputEvent.enabled) anim.DoScaledAnimationAsync(isPink ? "PinkPrepare" + direction : "Prepare" + direction, 0.4f);
})
});
}
private void Just(PlayerActionEvent caller, float state)
{
bool barely = state >= 1f || state <= -1f;
if (barely)
{
SoundByte.PlayOneShot("miss");
}
else
{
SoundByte.PlayOneShotGame(isPink ? "monkeyWatch/clapOffbeat" : $"monkeyWatch/clapOnbeat{UnityEngine.Random.Range(1, 6)}");
}
game.PlayerMonkeyClap(isPink, barely);
anim.DoScaledAnimationAsync(isPink ? "PinkClap" + direction : "Clap" + direction, 0.4f);
BeatAction.New(gameObject, new List<BeatAction.Action>()
{
new BeatAction.Action(caller.timer + caller.startBeat + 1, delegate
{
string whichAnim = barely ? "Barely" : "Just";
anim.DoScaledAnimationAsync(isPink ? "Pink" + whichAnim : whichAnim, 0.4f);
})
});
}
private void Miss(PlayerActionEvent caller)
{
anim.DoScaledAnimationAsync(isPink ? "PinkMiss" : "Miss", 0.4f);
}
private void Empty(PlayerActionEvent caller) { }
}
}