HeavenStudio/Assets/Scripts/Games/RhythmTweezers/LongHair.cs

135 lines
4.2 KiB
C#
Raw Normal View History

2022-02-10 08:13:54 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2022-03-14 14:21:05 +00:00
using HeavenStudio.Util;
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.Games.Scripts_RhythmTweezers
2022-02-10 08:13:54 +00:00
{
public class LongHair : PlayerActionObject
{
public float createBeat;
public GameObject hairSprite;
public GameObject stubbleSprite;
2022-02-10 08:13:54 +00:00
private RhythmTweezers game;
private Tweezers tweezers;
2022-02-11 07:21:43 +00:00
private Animator anim;
private int pluckState = 0;
public GameObject holder;
2022-02-11 07:21:43 +00:00
public GameObject loop;
2022-03-10 03:59:48 +00:00
private Sound pullSound;
PlayerActionEvent pluckEvent;
PlayerActionEvent endEvent;
InputType endInput;
2022-02-10 08:13:54 +00:00
private void Awake()
{
game = RhythmTweezers.instance;
2022-02-11 07:21:43 +00:00
anim = GetComponent<Animator>();
2022-02-10 08:13:54 +00:00
tweezers = game.Tweezers;
}
private void Start() {
game.ScheduleInput(createBeat, game.tweezerBeatOffset + game.beatInterval, InputType.STANDARD_DOWN | InputType.DIRECTION_DOWN, StartJust, StartMiss, Out);
}
2022-02-10 08:13:54 +00:00
private void Update()
{
2022-02-11 07:21:43 +00:00
if (pluckState == 1)
{
bool input = PlayerInput.PressedUp();
if (endInput == InputType.DIRECTION_UP) input = PlayerInput.GetAnyDirectionUp();
if (input && !game.IsExpectingInputNow(endInput))
{
endEvent.MakeInEligible();
EndEarly();
return;
}
Vector3 tst = tweezers.tweezerSpriteTrans.position;
var hairDirection = new Vector3(tst.x + 0.173f, tst.y) - holder.transform.position;
2022-02-11 07:21:43 +00:00
holder.transform.rotation = Quaternion.FromToRotation(Vector3.down, hairDirection);
2022-02-11 07:21:43 +00:00
float normalizedBeat = Conductor.instance.GetPositionFromBeat(createBeat + game.tweezerBeatOffset + game.beatInterval, 0.5f);
anim.Play("LoopPull", 0, normalizedBeat);
tweezers.anim.Play("Tweezers_LongPluck", 0, normalizedBeat);
2022-02-11 07:21:43 +00:00
// Auto-release if holding at release time.
if (normalizedBeat >= 1f)
endEvent.Hit(0f, 1f);
}
2022-02-11 07:21:43 +00:00
loop.transform.localScale = Vector2.one / holder.transform.localScale;
}
public void EndAce()
2022-02-10 08:13:54 +00:00
{
tweezers.LongPluck(true, this);
tweezers.hitOnFrame++;
2022-02-11 07:21:43 +00:00
if (pullSound != null)
pullSound.Stop();
pluckState = -1;
}
public void EndEarly()
2022-02-11 07:21:43 +00:00
{
var normalized = Conductor.instance.GetPositionFromBeat(createBeat + game.tweezerBeatOffset + game.beatInterval, 0.5f);
anim.Play("LoopPullReverse", 0, normalized);
tweezers.anim.Play("Idle", 0, 0);
if (pullSound != null)
pullSound.Stop();
pluckState = -1;
}
private void StartJust(PlayerActionEvent caller, float state)
{
// don't count near misses
if (state >= 1f || state <= -1f) {
pluckState = -1;
return;
}
if (PlayerInput.GetAnyDirectionDown())
2022-02-11 07:21:43 +00:00
{
endInput = InputType.DIRECTION_UP;
2022-02-11 07:21:43 +00:00
}
else
2022-02-11 07:21:43 +00:00
{
endInput = InputType.STANDARD_UP;
}
pullSound = Jukebox.PlayOneShotGame($"rhythmTweezers/longPull{UnityEngine.Random.Range(1, 5)}");
pluckState = 1;
endEvent = game.ScheduleInput(createBeat, game.tweezerBeatOffset + game.beatInterval + 0.5f, endInput, EndJust, Out, Out);
}
private void StartMiss(PlayerActionEvent caller)
{
// this is where perfect challenge breaks
}
private void Out(PlayerActionEvent caller) {}
private void EndJust(PlayerActionEvent caller, float state)
{
if (state <= -1f) {
EndEarly();
return;
2022-02-11 07:21:43 +00:00
}
EndAce();
2022-02-10 08:13:54 +00:00
}
void OnDestroy()
{
if (pluckEvent != null)
pluckEvent.Disable();
if (endEvent != null)
endEvent.Disable();
}
2022-02-10 08:13:54 +00:00
}
}