CapersProject/Assets/02.Scripts/Tycoon/TycoonGameOver.cs

167 lines
5.2 KiB
C#
Raw Normal View History

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