CapersProject/Assets/02.Scripts/Tycoon/TycoonGameOver.cs
Nam Tae Gun 57b21c004f 0.3.4.0
2024-11-15 16:28:13 +09:00

161 lines
5.0 KiB
C#

using System;
using System.Collections;
using BlueWater;
using BlueWater.Uis;
using UnityEngine;
using Sirenix.OdinInspector;
using Color = UnityEngine.Color;
using Image = UnityEngine.UI.Image;
using Random = UnityEngine.Random;
public class TycoonGameOver : MonoBehaviour
{
// RectTransform을 통해 UI 오브젝트 위치를 제어
private RectTransform _shipRectTransform;
private GameObject _panel;
private GameObject _mainOBJ;
// 흔들림 강도와 범위 설정
public float shakeAmount = 0.1f; // 흔들림 강도
public float shakeLimit = 10f; // 흔들림 제한
// 밑으로 이동하는 속도 설정
public float fallSpeed = 50f; // UI 좌표에서는 보통 작은 값을 사용
// 이동 시간을 제어하는 변수들
private float shakeDuration = 2.0f; // 흔들리기만 하는 시간
private float fallDuration = 5.0f; // 흔들리며 내려가는 시간
// 흔들림 변화 간격
public float shakeInterval = 0.01f; // 흔들림 변화 주기 (뚝뚝 끊기게 만드는 간격)
// 초기 위치 저장
private Vector2 originalPosition;
private Vector2 shakeOffset;
private float shakeTimer = 0f; // 흔들림 타이머
private Color originalColor; // 원래 색상 저장
private GameObject _ship;
private Image _text;
void Start()
{
EventManager.OnDead += GameOver_Start;
_panel = transform.Find("Panel").gameObject;
_mainOBJ = _panel.transform.Find("MainOBJ").gameObject;
_ship = _mainOBJ.transform.Find("Ship").gameObject;
_text = _mainOBJ.transform.Find("Text").GetComponent<Image>();
originalColor = _text.color; // 원래 색상 저장 (투명도 포함)
//_ship = GetComponent.Find("");
//AudioManager.Instance.SetMasterVolume(1.0f);
// RectTransform 가져오기
_shipRectTransform = _ship.GetComponent<RectTransform>();
originalPosition = _shipRectTransform.anchoredPosition;
}
private void OnDestroy()
{
EventManager.OnDead -= GameOver_Start;
}
[Button("게임오버 시작")]
public void GameOver_Start()
{
gameObject.SetActive(true);
// 코루틴 시작
StartCoroutine(MoveObject());
}
// 코루틴 정의
IEnumerator MoveObject()
{
VisualFeedbackManager.Instance.SetBaseTimeScale(0.0f);
PlayerInputKeyManager.Instance.DisableAction("Manual");
_panel.SetActive(true);
// 2.0초 동안은 흔들리기만 함
float timer = 0f;
while (timer < 1.2f) //유령 움직이는 시간...
{
timer += Time.unscaledDeltaTime;
yield return null; // 다음 프레임까지 대기
}
_mainOBJ.SetActive(true);
timer = 0;
while (timer < shakeDuration)
{
ShakeObject();
timer += Time.unscaledDeltaTime;
yield return null; // 다음 프레임까지 대기
}
// 그 후 5.0초 동안은 흔들리며 밑으로 내려감
timer = 0f;
// 이미지의 초기 색상과 알파값을 0으로 설정
Color imageColor = _text.color;
imageColor.a = 0f;
_text.color = imageColor;
float elapsedTime = 0f;
while (timer < fallDuration)
{
elapsedTime += Time.unscaledDeltaTime;
imageColor.a = Mathf.Clamp01(elapsedTime / fallDuration);
_text.color = imageColor;
MoveDown();
ShakeObject();
timer += Time.unscaledDeltaTime;
yield return null; // 다음 프레임까지 대기
}
EventManager.InvokeShowResult();
// 최종적으로 알파값을 1로 설정 (완전히 불투명하게)
imageColor.a = 1f;
_text.color = imageColor;
}
// 흔들림 구현 함수
void ShakeObject()
{
shakeTimer += Time.unscaledDeltaTime;
// 흔들림 타이머가 주기보다 크면 새로운 흔들림 값을 생성
if (shakeTimer > shakeInterval)
{
// 새로운 무작위 흔들림 값 생성
float offsetX = Random.Range(-shakeLimit, shakeLimit);
float offsetY = Random.Range(-shakeLimit, shakeLimit);
// 새로운 흔들림 값을 저장
shakeOffset = new Vector2(offsetX, offsetY);
// 타이머 초기화
shakeTimer = 0f;
}
// 현재 오브젝트의 좌표를 흔들림 값으로 업데이트
_shipRectTransform.anchoredPosition = originalPosition + shakeOffset;
}
// 밑으로 내려가는 함수
void MoveDown()
{
// 점점 밑으로 내려가는 부분 (Y 축 감소, anchoredPosition 사용)
originalPosition.y -= fallSpeed * Time.unscaledDeltaTime;
// 현재 오브젝트 위치도 업데이트
_shipRectTransform.anchoredPosition = originalPosition+ shakeOffset;
}
}