using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; /* -------------------------------------- Project: Programing assessment Standard: 91906 (AS3.7) v.1 School: Tauranga Boys' College Author: Rauputu Noah Phizacklea Date: August 2024 Unity: 2021.3.18f --------------------------------------- */ public class NextLevelDoor : MonoBehaviour { public GameObject FloatingTextPrefab; // Prefab for floating text public string nextLevelName; // Name of the next level scene public string requiredKeyID; // Key ID needed to open the door private bool isPlayerNear = false; // Indicates if the player is near the door private PlayerHealth playerHealth; private AudioManager audioManager; void Awake() { audioManager = GameObject.FindGameObjectWithTag("Audio").GetComponent(); } void Start() { playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent(); } void Update() { if (isPlayerNear && Input.GetKeyDown(KeyCode.Return)) { Inventory playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent(); if (string.IsNullOrEmpty(requiredKeyID) || playerInventory.HasKey(requiredKeyID)) { audioManager.PlaySFX(audioManager.checkpointSFX); LoadNextLevel(); } else { ShowFloatingText("A " + requiredKeyID + " is required to open this door."); } } } private void ShowFloatingText(string message) { // Instantiate floating text at the door's position var displayText = Instantiate(FloatingTextPrefab, transform.position, Quaternion.identity); displayText.GetComponent().text = message; } private void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { isPlayerNear = true; // Player is near the door } } private void OnTriggerExit2D(Collider2D other) { if (other.CompareTag("Player")) { isPlayerNear = false; // Player is no longer near the door } } private void LoadNextLevel() { // Save the death count to PlayerPrefs PlayerPrefs.SetFloat("DeathCount", playerHealth.deathCounter); // Load the next level scene SceneManager.LoadScene(nextLevelName); } }