Chess-II-Counterattack/Assets/Scripts/UI/RythmBarController.cs
Gerard Gascón 78b901484a init
2025-04-24 17:43:50 +02:00

144 lines
4.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RythmBarController : MonoBehaviour{
[SerializeField] bool canSpawn = true;
bool hasMoved;
Animator anim;
bool destroyed;
public bool difficlutySetted = true;
bool hardTriggered;
[System.Serializable] public enum Difficulty {Easy, Hard}
public Difficulty difficulty;
[Header("Hard Mode Speed")]
[SerializeField] float speed;
[Header("Easy Mode PositionOffset")]
[SerializeField] Vector3 offset = new Vector2(145, 0);
Vector3 nextLeftPos;
Vector3 nextRightPos;
[Range(0, .3f)] [SerializeField] float timeToMove = .1f;
Vector2 leftVelocity;
Vector2 rightVelocity;
[Space]
[SerializeField] GameObject defaultBar;
[SerializeField] Transform leftBar;
[SerializeField] Transform rightBar;
[Header("Wind")]
[SerializeField] GameObject windBar;
[SerializeField] Transform leftWindBar;
[SerializeField] Transform rightWindBar;
bool isWind;
CanvasManager canvas;
void Awake(){
canvas = GetComponentInParent<CanvasManager>();
}
// Start is called before the first frame update
void Start(){
anim = GetComponent<Animator>();
FindObjectOfType<PlayerController>().onMove.AddListener(() => Move());
nextLeftPos = leftBar.localPosition;
nextRightPos = rightBar.localPosition;
if (isWind){
leftBar.gameObject.SetActive(false);
rightBar.gameObject.SetActive(false);
}else{
leftWindBar.gameObject.SetActive(false);
rightWindBar.gameObject.SetActive(false);
}
}
public void SetDifficulty(){
if (canvas.difficulty == CanvasManager.CanvasDifficulty.Easy){
difficulty = Difficulty.Easy;
}else{
difficulty = Difficulty.Hard;
}
difficlutySetted = true;
}
// Update is called once per frame
void Update(){
if (!difficlutySetted)
return;
if(difficulty == Difficulty.Hard){
if (!canvas.stopped){
if (isWind){
leftWindBar.transform.Translate(Vector2.right * speed * Time.deltaTime);
rightWindBar.transform.Translate(Vector2.left * speed * Time.deltaTime);
}else{
leftBar.transform.Translate(Vector2.right * speed * Time.deltaTime);
rightBar.transform.Translate(Vector2.left * speed * Time.deltaTime);
}
}
}
if (difficulty == Difficulty.Easy){
if (isWind){
leftWindBar.localPosition = Vector2.SmoothDamp(leftWindBar.localPosition, nextLeftPos, ref leftVelocity, timeToMove);
rightWindBar.localPosition = Vector2.SmoothDamp(rightWindBar.localPosition, nextRightPos, ref rightVelocity, timeToMove);
}else{
leftBar.localPosition = Vector2.SmoothDamp(leftBar.localPosition, nextLeftPos, ref leftVelocity, timeToMove);
rightBar.localPosition = Vector2.SmoothDamp(rightBar.localPosition, nextRightPos, ref rightVelocity, timeToMove);
}
}
if(Vector2.Distance(leftBar.localPosition, rightBar.localPosition) < 175 && !hardTriggered || Vector2.Distance(leftWindBar.localPosition, rightWindBar.localPosition) < 175 && !hardTriggered){
if(difficulty == Difficulty.Hard){
canvas.onHardMoveIn.Invoke();
}
hardTriggered = true;
}
if(Vector2.Distance(leftBar.localPosition, rightBar.localPosition) < 10 && !destroyed || Vector2.Distance(leftWindBar.localPosition, rightWindBar.localPosition) < 10 && !destroyed){
if(difficulty == Difficulty.Hard){
canvas.onHardMoveOut.Invoke();
}
if (isWind){
canvas.onWind.Invoke();
}
destroyed = true;
anim.SetTrigger("Fade");
}
}
public void IsWind(){
isWind = true;
windBar.SetActive(true);
}
public void Destroy(){
if(difficulty == Difficulty.Hard){
canvas.SpawnFromPool(transform.position, transform.rotation);
}
Destroy(gameObject);
}
public void IsNotWind(){
windBar.SetActive(false);
}
void Move(){
if (difficulty == Difficulty.Easy){
nextLeftPos += offset;
nextRightPos -= offset;
if (!hasMoved && canvas != null && canSpawn){
canvas.SpawnFromPool(transform.position, transform.rotation);
hasMoved = true;
}
}
}
}