KeySpawnPoint.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using DunGen.LockAndKey;
  2. using UnityEngine;
  3. namespace DunGen.Demo
  4. {
  5. public class KeySpawnPoint : MonoBehaviour, IKeySpawner
  6. {
  7. public bool SetColourOnSpawn = true;
  8. private MaterialPropertyBlock propertyBlock;
  9. private GameObject spawnedKey;
  10. #region IKeySpawner Implementation
  11. public bool CanSpawnKey(KeyManager keyManaager, Key key)
  12. {
  13. // Has already spawned a key (this check shouldn't be necessary)
  14. if (spawnedKey != null)
  15. return false;
  16. // Cannot spawn a key that doesn't have a prefab
  17. return key.Prefab != null;
  18. }
  19. public void SpawnKey(KeySpawnParameters keySpawnParameters)
  20. {
  21. // Spawn the key attached to the dungeon root
  22. spawnedKey = GameObject.Instantiate(keySpawnParameters.Key.Prefab);
  23. spawnedKey.transform.parent = keySpawnParameters.DungeonGenerator.Root.transform;
  24. spawnedKey.transform.SetPositionAndRotation(transform.position, transform.rotation);
  25. if (SetColourOnSpawn && Application.isPlaying)
  26. {
  27. if (propertyBlock == null)
  28. propertyBlock = new MaterialPropertyBlock();
  29. propertyBlock.SetColor("_Color", keySpawnParameters.Key.Colour);
  30. foreach (var r in spawnedKey.GetComponentsInChildren<Renderer>())
  31. r.SetPropertyBlock(propertyBlock);
  32. }
  33. // Pass any components that implement IKeyLock back to the dungeon generator
  34. keySpawnParameters.OutputSpawnedKeys.AddRange(spawnedKey.GetComponents<IKeyLock>());
  35. }
  36. #endregion
  37. }
  38. }