/* * ------------------------------------------------------------------------ * Description: This class handles door open/ close logic * Author: Eli Simpkins-Simmonds * ------------------------------------------------------------------------ */ using UnityEngine; public class Door : MonoBehaviour { public Sprite openSprite; // Sprite to display when the door is open public Sprite closedSprite; // Sprite to display when the door is closed private SpriteRenderer spriteRenderer; // Reference to the SpriteRenderer component public int teleportIndex; // Index to determine which teleport location to use private TeleportManager teleportManager; // Reference to the TeleportManager script private Camera mainCamera; // Reference to the main camera private bool isOpen = false; // Boolean to track if the door is open or closed private Inventory inventory; // Reference to the Inventory script void Start() { spriteRenderer = GetComponent(); // Get the SpriteRenderer component attached to the door CloseDoor(); // Start with the door closed mainCamera = Camera.main; // Gets the main camera teleportManager = FindObjectOfType(); // Find the TeleportManager in the scene inventory = FindObjectOfType(); // Find the Inventory script in the scene } public void OpenDoor() { spriteRenderer.sprite = openSprite; // Changes the sprite to the open sprite isOpen = true; // Sets the door status to open } public void CloseDoor() { spriteRenderer.sprite = closedSprite; // Changes the sprite to the closed sprite isOpen = false; // Set the door status to closed } void OnTriggerEnter2D(Collider2D other) { // Check if the door is open and the collider that entered has the tag "Player" if (isOpen && other.CompareTag("Player")) { // Check if the teleportManager is valid and the teleport index is within range if (teleportManager != null && teleportIndex >= 0 && teleportIndex < teleportManager.teleportDestinations.Length) { // Get the teleport destination based on the teleport index TeleportDestination destination = teleportManager.teleportDestinations[teleportIndex]; // Teleports the player to the new location other.transform.position = destination.playerLocation.position; // Move the camera to the new location mainCamera.transform.position = new Vector3( destination.cameraLocation.position.x, destination.cameraLocation.position.y, mainCamera.transform.position.z ); // Reset the inventory and other game states if (inventory != null) { inventory.ResetInventory(); } // Update the next spawn location teleportManager.UpdateNextSpawnLocation(teleportIndex); // Update the player's spawn point PlayerHealth playerHealth = other.GetComponent(); if (playerHealth != null) { playerHealth.UpdateSpawnPoint(destination.playerLocation.position); } } } } }