Created
July 30, 2019 19:25
-
-
Save AG-Dan/d6c220861f471389aa5373981b19373a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DunGen; | |
using UnityEngine; | |
[RequireComponent(typeof(RuntimeDungeon))] | |
class PlayerSpawner : MonoBehaviour | |
{ | |
public GameObject PlayerPrefab = null; | |
private GameObject currentPlayer; | |
private void Awake() | |
{ | |
var runtimeDungeon = GetComponent<RuntimeDungeon>(); | |
runtimeDungeon.Generator.OnGenerationStatusChanged += OnDungeonGenerationStatusChanged; | |
} | |
private void OnDungeonGenerationStatusChanged(DungeonGenerator generator, GenerationStatus status) | |
{ | |
if (status == GenerationStatus.Complete) | |
SpawnPlayer(); | |
} | |
private void SpawnPlayer() | |
{ | |
// Destroy the current player if one exists | |
if (currentPlayer != null) | |
Destroy(currentPlayer); | |
// Instantiate a new player instance | |
currentPlayer = Instantiate(PlayerPrefab); | |
// Find a random GameObject with the "Respawn" tag | |
var potentialSpawnPoints = GameObject.FindGameObjectsWithTag("Respawn"); | |
var chosenSpawnPoint = potentialSpawnPoints[Random.Range(0, potentialSpawnPoints.Length)]; | |
// Teleport the newly instantiated player to the chosen spawn point | |
currentPlayer.transform.position = chosenSpawnPoint.transform.position; | |
currentPlayer.transform.rotation = chosenSpawnPoint.transform.rotation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment