update AI
This commit is contained in:
@@ -1,30 +1,64 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI; // Nếu dùng UI thường
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.Collections;
|
||||
|
||||
public class FinishGate : MonoBehaviour
|
||||
{
|
||||
[Header("Cài đặt UI")]
|
||||
public GameObject winPanel; // Kéo Panel hoặc Text "You Win" vào đây
|
||||
[Header("Cài đặt UI Chính")]
|
||||
public GameObject winPanel; // Bảng You Win
|
||||
public GameObject warningUI; // Chữ "Chưa đủ rương"
|
||||
|
||||
[Header("Cài đặt Sao (Stars)")]
|
||||
public GameObject star1;
|
||||
public GameObject star2;
|
||||
public GameObject star3;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Time.timeScale = 1;
|
||||
if (winPanel != null) winPanel.SetActive(false); // Ẩn chữ Win lúc đầu game
|
||||
// Reset thời gian và ẩn UI lúc bắt đầu
|
||||
Time.timeScale = 1f;
|
||||
|
||||
if (winPanel != null) winPanel.SetActive(false);
|
||||
if (warningUI != null) warningUI.SetActive(false);
|
||||
|
||||
if (star1) star1.SetActive(false);
|
||||
if (star2) star2.SetActive(false);
|
||||
if (star3) star3.SetActive(false);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
PlayerInventory player = other.GetComponentInParent<PlayerInventory>();
|
||||
|
||||
if (player != null && player.hasTreasure)
|
||||
if (player != null)
|
||||
{
|
||||
WinGame();
|
||||
if (player.treasuresCollected >= player.totalTreasuresNeeded)
|
||||
{
|
||||
WinGame(player.treasuresCollected);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Chưa đủ rương! Bạn cần " + player.totalTreasuresNeeded);
|
||||
if (warningUI != null) StartCoroutine(ShowWarning());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WinGame()
|
||||
IEnumerator ShowWarning()
|
||||
{
|
||||
warningUI.SetActive(true);
|
||||
yield return new WaitForSeconds(2f);
|
||||
warningUI.SetActive(false);
|
||||
}
|
||||
|
||||
void WinGame(int count)
|
||||
{
|
||||
if (winPanel != null) winPanel.SetActive(true);
|
||||
|
||||
// Hiện sao dựa trên số lượng nhặt được
|
||||
if (count >= 1 && star1) star1.SetActive(true);
|
||||
if (count >= 2 && star2) star2.SetActive(true);
|
||||
if (count >= 3 && star3) star3.SetActive(true);
|
||||
|
||||
// Dừng game
|
||||
Time.timeScale = 0f;
|
||||
@@ -37,23 +71,20 @@ public class FinishGate : MonoBehaviour
|
||||
// --- HÀM CHO CÁC NÚT BẤM ---
|
||||
public void RestartGame()
|
||||
{
|
||||
// Đặt lại thời gian về 1 trước khi load lại nếu không game sẽ bị đứng
|
||||
Debug.Log("Đang tải lại màn chơi...");
|
||||
Time.timeScale = 1f;
|
||||
// Tải lại Scene hiện tại
|
||||
|
||||
SceneManager.LoadScene("Only AI");
|
||||
Debug.Log("đang tải lại màn chơi : Only AI");
|
||||
// Sử dụng buildIndex để tránh lỗi tên scene
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
|
||||
public void QuitGame()
|
||||
{
|
||||
Debug.Log("Đang thoát game...");
|
||||
Application.Quit(); // Thoát file .exe (không có tác dụng trong trình dựng Unity)
|
||||
|
||||
Application.Quit();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorApplication.isPlaying = false; // Dừng chạy nếu đang ở trong Editor
|
||||
UnityEditor.EditorApplication.isPlaying = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerInventory : MonoBehaviour
|
||||
{
|
||||
// Biến kiểm tra xem người chơi đã nhặt kho báu chưa
|
||||
public bool hasTreasure = false;
|
||||
public int score = 0; // Điểm số nếu bạn muốn cộng thêm
|
||||
[Header("Cấu hình túi đồ")]
|
||||
public int treasuresCollected = 0; // Đếm số lượng rương đã nhặt
|
||||
public int totalTreasuresNeeded = 3; // Số lượng cần để thắng
|
||||
}
|
||||
@@ -1,18 +1,41 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class TreasureItem : MonoBehaviour
|
||||
{
|
||||
[Header("Cài đặt UI thông báo")]
|
||||
public GameObject notificationText;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (notificationText != null) notificationText.SetActive(false);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
// Kiểm tra xem thứ chạm vào có script PlayerInventory không
|
||||
// Tìm PlayerInventory ở đối tượng hoặc cha của nó
|
||||
PlayerInventory player = other.GetComponentInParent<PlayerInventory>();
|
||||
if (player != null && !player.hasTreasure)
|
||||
|
||||
if (player != null)
|
||||
{
|
||||
player.hasTreasure = true;
|
||||
Debug.Log("Đã nhặt kho báu!");
|
||||
player.treasuresCollected += 1;
|
||||
Debug.Log("Đã nhặt rương! Tổng cộng: " + player.treasuresCollected);
|
||||
|
||||
if (notificationText != null)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(ShowNotification());
|
||||
}
|
||||
|
||||
// Làm vật phẩm biến mất
|
||||
gameObject.SetActive(false); // Hoặc Destroy(gameObject);
|
||||
// Biến mất rương
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator ShowNotification()
|
||||
{
|
||||
notificationText.SetActive(true);
|
||||
yield return new WaitForSeconds(2f);
|
||||
notificationText.SetActive(false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user