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

136 lines
4.7 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;
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 Update()
{
2022-02-11 07:21:43 +00:00
float stateBeat;
2022-02-10 08:13:54 +00:00
2022-02-11 07:21:43 +00:00
switch (pluckState)
2022-02-10 08:13:54 +00:00
{
2022-02-11 07:21:43 +00:00
// Able to be held.
case 0:
stateBeat = Conductor.instance.GetPositionFromMargin(createBeat + game.tweezerBeatOffset + game.beatInterval, 1f);
StateCheck(stateBeat);
if (PlayerInput.Pressed(true))
2022-02-11 07:21:43 +00:00
{
if (state.perfect)
{
pullSound = Jukebox.PlayOneShotGame($"rhythmTweezers/longPull{UnityEngine.Random.Range(1, 5)}");
pluckState = 1;
ResetState();
}
else if (state.notPerfect())
{
// I don't know what happens if you mess up here.
pluckState = -1;
}
}
break;
// In held state. Able to be released.
case 1:
stateBeat = Conductor.instance.GetPositionFromMargin(createBeat + game.tweezerBeatOffset + game.beatInterval + 0.5f, 1f);
StateCheck(stateBeat);
if (PlayerInput.PressedUp(true))
2022-02-11 07:21:43 +00:00
{
// It's possible to release earlier than earlyTime,
// and the hair will automatically be released before lateTime,
// so standard state checking isn't applied here
// (though StateCheck is still used for autoplay).
if (stateBeat >= Minigame.perfectTime)
{
Ace();
}
else
{
var normalized = Conductor.instance.GetPositionFromBeat(createBeat + game.tweezerBeatOffset + game.beatInterval, 0.5f);
// Hair gets released early and returns whoops.
anim.Play("LoopPullReverse", 0, normalized);
tweezers.anim.Play("Idle", 0, 0);
2022-02-11 07:21:43 +00:00
if (pullSound != null)
pullSound.Stop();
pluckState = -1;
}
}
break;
// Released or missed. Can't be held or released.
default:
break;
}
if (pluckState == 1)
{
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)
Ace();
}
2022-02-11 07:21:43 +00:00
loop.transform.localScale = Vector2.one / holder.transform.localScale;
}
2022-02-10 08:13:54 +00:00
public void Ace()
{
tweezers.LongPluck(true, this);
tweezers.hitOnFrame++;
2022-02-11 07:21:43 +00:00
if (pullSound != null)
pullSound.Stop();
pluckState = -1;
}
public override void OnAce()
{
if (pluckState == 0)
{
pullSound = Jukebox.PlayOneShotGame($"rhythmTweezers/longPull{UnityEngine.Random.Range(1, 5)}");
pluckState = 1;
ResetState();
}
else if (pluckState == 1)
{
Ace();
}
2022-02-10 08:13:54 +00:00
}
}
}