Initial commit
This commit is contained in:
199
Assets/Scripts/StartGempa.cs
Normal file
199
Assets/Scripts/StartGempa.cs
Normal file
@ -0,0 +1,199 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class StartGempa : MonoBehaviour
|
||||
{
|
||||
public FlowController flowController; // Tambahkan ini
|
||||
public Transform kameraKiri;
|
||||
public Transform kameraKanan;
|
||||
public float kekuatan = 0.1f;
|
||||
|
||||
public AudioSource audioGempa;
|
||||
public MengaturSlideGempa slideUI;
|
||||
public PlayerBergerak1 kontrolPlayer;
|
||||
|
||||
public Transform posisiBawahMeja;
|
||||
public Transform transformPlayer;
|
||||
|
||||
public GameObject[] tombolBerlindung;
|
||||
public NonaktifkanTombol pengaturTombol;
|
||||
|
||||
|
||||
private Vector3 awalKiri;
|
||||
private Vector3 awalKanan;
|
||||
private Vector3 posisiAwalPlayer;
|
||||
private Quaternion rotasiAwalPlayer;
|
||||
|
||||
public SetelahGempaUI uiSetelahGempaController;
|
||||
private Coroutine guncangCoroutine;
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
if (slideUI != null)
|
||||
{
|
||||
slideUI.kontrolPlayer = kontrolPlayer;
|
||||
slideUI.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void SiapkanGempa()
|
||||
{
|
||||
Debug.Log("Gempa disiapkan.");
|
||||
awalKiri = kameraKiri.localPosition;
|
||||
awalKanan = kameraKanan.localPosition;
|
||||
}
|
||||
|
||||
public void MulaiGempa()
|
||||
{
|
||||
Debug.Log("Gempa dimulai!");
|
||||
guncangCoroutine = StartCoroutine(Guncang());
|
||||
StartCoroutine(UISetelahDelay(0.5f));
|
||||
}
|
||||
|
||||
public void TampilkanSlideDekatPlayer(Transform kamera, GameObject panel, float jarakMax = 2f)
|
||||
{
|
||||
Vector3 arah = kamera.forward;
|
||||
Ray ray = new Ray(kamera.position, arah);
|
||||
RaycastHit hit;
|
||||
|
||||
Vector3 posisiFinal;
|
||||
|
||||
// Jika ada tembok atau furniture di depan
|
||||
if (Physics.Raycast(ray, out hit, jarakMax))
|
||||
{
|
||||
posisiFinal = hit.point - arah * 0.05f;
|
||||
}
|
||||
else
|
||||
{
|
||||
posisiFinal = kamera.position + arah * jarakMax;
|
||||
}
|
||||
|
||||
panel.transform.position = posisiFinal;
|
||||
|
||||
// Rotasi agar selalu hadap ke kamera/player
|
||||
Vector3 arahRotasi = posisiFinal - kamera.position;
|
||||
arahRotasi.y = 0; // hanya rotasi horizontal
|
||||
panel.transform.rotation = Quaternion.LookRotation(arahRotasi);
|
||||
}
|
||||
|
||||
|
||||
IEnumerator Guncang()
|
||||
{
|
||||
if (audioGempa != null) audioGempa.Play();
|
||||
|
||||
while (true)
|
||||
{
|
||||
kameraKiri.localPosition = awalKiri + Random.insideUnitSphere * kekuatan;
|
||||
kameraKanan.localPosition = awalKanan + Random.insideUnitSphere * kekuatan;
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator UISetelahDelay(float delay)
|
||||
{
|
||||
|
||||
yield return new WaitForSeconds(delay);
|
||||
|
||||
if (slideUI != null)
|
||||
{
|
||||
GameObject slidePanel = slideUI.slidePanel;
|
||||
slidePanel.SetActive(true);
|
||||
|
||||
Transform kamera = kameraKiri != null ? kameraKiri : Camera.main.transform;
|
||||
TampilkanSlideDekatPlayer(kamera, slidePanel, 2f);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("slideUI belum di-assign!");
|
||||
}
|
||||
foreach (GameObject tombol in tombolBerlindung)
|
||||
{
|
||||
if (tombol != null)
|
||||
tombol.SetActive(true);
|
||||
}
|
||||
|
||||
if (kontrolPlayer != null)
|
||||
{
|
||||
kontrolPlayer.Gerak = true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void HentikanGempa()
|
||||
{
|
||||
if (guncangCoroutine != null)
|
||||
StopCoroutine(guncangCoroutine);
|
||||
|
||||
kameraKiri.localPosition = awalKiri;
|
||||
kameraKanan.localPosition = awalKanan;
|
||||
|
||||
if (audioGempa != null) audioGempa.Stop();
|
||||
|
||||
Debug.Log("Gempa berhenti total.");
|
||||
}
|
||||
|
||||
|
||||
public void TeleportKeBawahMeja(Transform targetBerlindung)
|
||||
{
|
||||
if (transformPlayer != null && targetBerlindung != null)
|
||||
{
|
||||
posisiAwalPlayer = transformPlayer.position;
|
||||
rotasiAwalPlayer = transformPlayer.rotation;
|
||||
|
||||
CharacterController cc = transformPlayer.GetComponent<CharacterController>();
|
||||
if (cc != null) cc.enabled = false;
|
||||
|
||||
if (kontrolPlayer != null)
|
||||
kontrolPlayer.Gerak = false;
|
||||
|
||||
transformPlayer.position = targetBerlindung.position;
|
||||
transformPlayer.rotation = targetBerlindung.rotation;
|
||||
|
||||
Debug.Log("Player dipindahkan ke bawah meja: " + targetBerlindung.name);
|
||||
|
||||
HentikanGempa();
|
||||
|
||||
StartCoroutine(KembalikanPlayer(4f, cc));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
IEnumerator KembalikanPlayer(float delay, CharacterController cc)
|
||||
{
|
||||
yield return new WaitForSeconds(delay);
|
||||
|
||||
transformPlayer.position = posisiAwalPlayer;
|
||||
transformPlayer.rotation = rotasiAwalPlayer;
|
||||
|
||||
if (cc != null)
|
||||
cc.enabled = true;
|
||||
|
||||
if (kontrolPlayer != null)
|
||||
kontrolPlayer.Gerak = true;
|
||||
|
||||
Debug.Log("Player dikembalikan ke posisi awal.");
|
||||
|
||||
|
||||
yield return new WaitForSeconds(0.5f); // delay sebelum UI muncul
|
||||
|
||||
if (pengaturTombol != null)
|
||||
pengaturTombol.NonaktifkanSemuaTombol();
|
||||
|
||||
Debug.Log("UI muncul di posisi: " + uiSetelahGempaController.transform.position);
|
||||
|
||||
if (uiSetelahGempaController != null && kameraKiri != null)
|
||||
{
|
||||
uiSetelahGempaController.TampilkanUI(kameraKiri);
|
||||
}
|
||||
|
||||
if (flowController != null)
|
||||
{
|
||||
flowController.SelesaiBerlindung();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user