SpawnerSingle.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using KairoEngine.Utilities;
  6. using Sirenix.OdinInspector;
  7. using NodeCanvas.Framework;
  8. using NodeCanvas.BehaviourTrees;
  9. using KairoEngine.Core;
  10. using KairoEngine.CharacterSystem;
  11. namespace KairoEngine.CharacterSystem
  12. {
  13. public enum CharacterType
  14. {
  15. Player,
  16. NPC
  17. }
  18. [HideMonoScript]
  19. public class SpawnerSingle : MonoBehaviour
  20. {
  21. [HideIf("@characterRandom != null")] public CharacterData characterData;
  22. [HideIf("@characterData != null")] public CharacterRandom characterRandom;
  23. [HorizontalGroup("id"), ShowIf("@characterRandom != null && characterData == null")] public string id;
  24. [HorizontalGroup("id", 90), Button("Randomize"), ShowIf("@characterRandom != null && characterData == null")]
  25. public void RandomizeId() { id = KairoEngine.Core.Utilities.RandomString(12); }
  26. public CharacterType characterType = CharacterType.NPC;
  27. [ShowIf("@characterType == CharacterType.NPC")] public Transform waypoint;
  28. public float wait = 0.1f;
  29. private float timer = 0f;
  30. private bool done = false;
  31. void Update()
  32. {
  33. if(!done)
  34. {
  35. timer += Time.deltaTime;
  36. if(timer > wait)
  37. {
  38. CreateCharacter();
  39. done = true;
  40. }
  41. }
  42. }
  43. private void CreateCharacter()
  44. {
  45. CharacterController character = null;
  46. // Spawn character from data
  47. if(characterData != null)
  48. {
  49. if(CharacterManager.nameIsRegistered($"{characterData.name}_{characterData.id}"))
  50. {
  51. Debug.Log($"{characterData.name}_{characterData.id} has already been created.");
  52. return;
  53. }
  54. character = CharacterGenerator.Create(characterData, transform.position, transform.rotation);
  55. }
  56. // Spawn random character
  57. else if (characterRandom != null)
  58. {
  59. CharacterData data = characterRandom.Generate(id);
  60. if(CharacterManager.nameIsRegistered($"{data.name}_{data.id}"))
  61. {
  62. Debug.Log($"{data.name}_{data.id} has already been created.");
  63. return;
  64. }
  65. character = CharacterGenerator.Create(data, transform.position, transform.rotation);
  66. }
  67. // Set Character to follow waypoint
  68. if(character != null && waypoint != null && characterType == CharacterType.NPC)
  69. {
  70. Blackboard blackboard = character.GetComponent<Blackboard>();
  71. blackboard.SetVariableValue("currentWaypoint", waypoint);
  72. BehaviourTreeOwner behaviour = character.GetComponent<BehaviourTreeOwner>();
  73. behaviour.enabled = true;
  74. NavMeshAgent navMeshAgent = character.GetComponent<NavMeshAgent>();
  75. navMeshAgent.enabled = true;
  76. }
  77. if(characterType == CharacterType.Player)
  78. {
  79. EventManager.broadcast.Trigger("PlayerCharacter-Instantiated", character);
  80. }
  81. }
  82. }
  83. }