You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.9 KiB

10 months ago
using UnityEngine;
using UnityEngine.SceneManagement;
/// <summary>
/// This class is attached to a "boundary" in a scene. It handles moving from the
/// current scene to the next scene that the "boundary leads to. For example, this
/// script can be attached to a collider along the side of the scene that leads
/// from a cityscape to an overland.Or this script can be attached to a collider
/// that represents the door on a building that leads to the interior of the building.
///
/// SETUP
/// BoxCollider2D
/// Add a BoxCollider2D to the scene where the "boundary" exists.This class makes
/// use of the object's tag to determine the destination, so add a tag that matches
/// the name of the destination.The name of the destination will match a key in a
/// Dictionary in the NavigationManager. So make sure the spelling matches. Also use
/// the icon feature to make the collider easier to see in the scene view.
/// </summary>
public class NavigationPrompt : MonoBehaviour
{
/// <summary>
/// Used to track the player's starting position on the map when the scene is loaded.
/// </summary>
public Vector3 startingPosition;
/// <summary>
/// When the player object enters the "boundary" check the tag of the boundary
/// and compare it to the Dictionary values in the NavigationManager. If the
/// manager has an entry for the tag AND allows navigation to the tag, then
/// this method will load the scene that matches the tag.
///
/// NOTE: Notice that all three things match; the tag, the key in the Dictionary,
/// and the name of the scene.
/// </summary>
/// <param name="col"></param>
void OnCollisionEnter2D(Collision2D col)
{
// Check if the tag on this object is defined in the Nav Manager
if (NavigationManager.CanNavigate(this.tag))
{
// Log the tag so we can track what we are trying to find in the Dictionary
Debug.Log("attempting to exit via " + tag);
// Use the NavigationManager to load the scene whose name matches the tag
NavigationManager.NavigateTo(this.tag);
// Should we save the player's position on the map?
GameState.saveLastPosition = false;
// Save the Player's position on the map so that when we return the object
// will be in the right place
GameState.SetLastScenePosition(SceneManager.GetActiveScene().name, startingPosition);
}
}
/// <summary>
/// Use this method for objects that are set as triggers.
/// </summary>
/// <param name="col"></param>
void OnTriggerEnter2D(Collider2D col)
{
if (NavigationManager.CanNavigate(this.tag))
{
Debug.Log("attempting to exit via " + tag);
NavigationManager.NavigateTo(this.tag);
GameState.saveLastPosition = false;
GameState.SetLastScenePosition(SceneManager.GetActiveScene().name, startingPosition);
}
}
}