HeavenStudioPlus/Assets/Scripts/Games/CropStomp/Veggie.cs

235 lines
6.6 KiB
C#
Raw Normal View History

2022-03-01 06:38:38 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NaughtyBezierCurves;
2022-03-01 20:57:37 +00:00
using DG.Tweening;
2022-03-01 06:38:38 +00:00
using RhythmHeavenMania.Util;
namespace RhythmHeavenMania.Games.CropStomp
{
public class Veggie : PlayerActionObject
{
2022-03-01 20:57:37 +00:00
static float pickedRotationSpeed = -1080f;
2022-03-01 06:38:38 +00:00
public bool isMole;
public Sprite[] veggieSprites;
public Sprite[] moleSprites;
public SpriteRenderer veggieSprite;
public Transform veggieTrans;
public BezierCurve3D curve;
public float targetBeat;
private float stompedBeat;
2022-03-01 20:57:37 +00:00
private float pickedBeat;
2022-03-01 06:38:38 +00:00
private int veggieState = 0;
private bool boinked; // Player got barely when trying to pick.
private float landBeat;
2022-03-01 20:57:37 +00:00
private Tween squashTween;
2022-03-01 06:38:38 +00:00
private CropStomp game;
private void Start()
{
game = CropStomp.instance;
if (!isMole)
{
veggieSprite.sprite = veggieSprites[UnityEngine.Random.Range(0, veggieSprites.Length)];
}
}
private bool gotStomped; // Safeguard in case nested Update() call breaks.
private void Update()
{
if (!game.isMarching)
return;
// Veggie missed. Handle missed state.
if (veggieState == -1)
{
MissedUpdate();
return;
}
// Veggie picked. Handle picked state.
if (veggieState == 2)
{
PickedUpdate();
return;
}
var cond = Conductor.instance;
float normalizedBeat = cond.GetPositionFromMargin(targetBeat, 1f);
StateCheck(normalizedBeat);
// In ground.
if (veggieState == 0)
{
if (normalizedBeat > Minigame.LateTime())
{
veggieState = -1;
return;
}
if (PlayerInput.Pressed())
{
if (state.perfect)
{
StompVeggie(false);
}
else if (state.notPerfect())
{
veggieState = -1;
}
}
}
// In air.
else if (veggieState == 1)
{
float airPosition = cond.GetPositionFromBeat(stompedBeat, landBeat - stompedBeat);
veggieTrans.position = curve.GetPoint(Mathf.Clamp(airPosition, 0, 1));
if (normalizedBeat > Minigame.EndTime())
{
veggieState = -1;
2022-03-01 20:57:37 +00:00
Jukebox.PlayOneShotGame("cropStomp/veggieMiss");
2022-03-01 06:38:38 +00:00
return;
}
if (PlayerInput.PressedUp())
{
if (state.perfect)
{
PickVeggie(false);
}
else if (state.notPerfect())
{
veggieState = -1;
boinked = true;
2022-03-01 20:57:37 +00:00
Jukebox.PlayOneShot("miss");
2022-03-01 06:38:38 +00:00
MissedUpdate();
}
game.bodyAnim.Play("Pick", 0, 0);
game.isFlicking = true;
}
}
}
private void MissedUpdate()
{
if (boinked)
{
}
else
{
}
}
private void PickedUpdate()
{
2022-03-01 20:57:37 +00:00
float pickPosition = Conductor.instance.GetPositionFromBeat(pickedBeat, 1f);
pickPosition = Mathf.Clamp(pickPosition, 0, 1);
veggieTrans.position = game.pickCurve.GetPoint(pickPosition);
veggieTrans.rotation = Quaternion.Euler(0, 0, veggieTrans.rotation.eulerAngles.z + (pickedRotationSpeed * Time.deltaTime));
2022-03-01 06:38:38 +00:00
2022-03-01 20:57:37 +00:00
var veggieScale = Mathf.Min(1.5f - pickPosition, 1f);
veggieTrans.localScale = Vector2.one * veggieScale;
2022-03-01 06:38:38 +00:00
}
private void StompVeggie(bool autoTriggered)
{
// Juuuuuust in case.
if (gotStomped)
{
Debug.Log("Recursion moment?");
return;
}
gotStomped = true;
var cond = Conductor.instance;
veggieState = 1;
targetBeat = targetBeat + (isMole ? 0.5f : 1f);
stompedBeat = cond.songPositionInBeats;
landBeat = cond.GetBeatFromPositionAndMargin(Minigame.EndTime(), targetBeat, 1f);
if (autoTriggered)
{
game.Stomp();
game.bodyAnim.Play("Stomp", 0, 0);
}
2022-03-01 20:57:37 +00:00
if (!isMole)
{
BeatAction.New(gameObject, new List<BeatAction.Action>()
{
new BeatAction.Action(targetBeat - 0.5f, delegate { Jukebox.PlayOneShotGame("cropStomp/veggieOh"); })
});
}
var veggieScale = veggieTrans.localScale;
veggieTrans.localScale = new Vector3(veggieScale.x * 0.5f, veggieScale.y, veggieScale.z);
squashTween = veggieTrans.DOScaleX(veggieScale.x, cond.secPerBeat * 0.5f / cond.musicSource.pitch);
2022-03-01 06:38:38 +00:00
ResetState();
Update(); // Update flying veggie state immediately.
}
private void PickVeggie(bool autoTriggered)
{
veggieState = 2;
if (autoTriggered)
{
game.bodyAnim.Play("Pick", 0, 0);
game.isFlicking = true;
}
2022-03-01 20:57:37 +00:00
var key1 = game.pickCurve.KeyPoints[0];
var keyPos = key1.Position;
key1.Position = new Vector3(keyPos.x, veggieTrans.position.y, keyPos.z);
pickedBeat = Conductor.instance.songPositionInBeats;
if (!isMole)
{
BeatAction.New(gameObject, new List<BeatAction.Action>()
{
new BeatAction.Action(pickedBeat + 0.5f, delegate { veggieSprite.sortingOrder = -1; }),
new BeatAction.Action(pickedBeat + 1f, delegate { GameObject.Destroy(gameObject); })
});
Jukebox.PlayOneShotGame("cropStomp/veggieKay");
}
2022-03-01 06:38:38 +00:00
2022-03-01 20:57:37 +00:00
if (squashTween != null)
squashTween.Kill(true);
2022-03-01 06:38:38 +00:00
PickedUpdate();
}
public override void OnAce()
{
if (veggieState == 0)
StompVeggie(true);
else
PickVeggie(true);
}
}
}