Files
pgs/Assets/Scripts/PortalController.cs
2026-02-21 16:58:22 -08:00

65 lines
2.1 KiB
C#

using Com.LuisPedroFonseca.ProCamera2D;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PortalController : MonoBehaviour {
// If transports to same-scene, specify otherPortal.
[SerializeField] public PortalController otherPortal;
// If transports to another scene, specify otherScene and otherScenePortalIndex.
[SerializeField] public MapID otherScene;
[SerializeField] public int otherScenePortalIndex;
private PortalRegistryController registry;
private Transform player;
public bool isPlayerInRange;
private void Awake() {
player = GameObject.FindGameObjectWithTag("Player").transform;
registry = GameObject.FindGameObjectWithTag("PortalRegistry").GetComponent<PortalRegistryController>();
}
// Start is called before the first frame update
void Start()
{
isPlayerInRange = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Teleport") && isPlayerInRange) {
if (otherPortal) {
Debug.Log("Player used portal for " + otherScene.ToString());
player.position = otherPortal.transform.position;
} else {
Debug.Log("Player used portal for " + otherScene.ToString());
State.state.locationHistory.history.Push(new Location(
(MapID)Enum.Parse(typeof(MapID), SceneManager.GetActiveScene().name), otherScenePortalIndex));
// The other scene's portal index will be loaded by the Player at load time.
SceneManager.LoadScene(otherScene.ToString());
}
}
}
private void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.CompareTag("Player")) {
Debug.Log("Player entered portal range");
isPlayerInRange = true;
}
}
private void OnTriggerExit2D(Collider2D collision) {
if (collision.gameObject.CompareTag("Player")) {
Debug.Log("Player exited portal range");
isPlayerInRange = false;
}
}
}