123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- using KairoEngine.Utilities;
- using Sirenix.OdinInspector;
- using NodeCanvas.Framework;
- using NodeCanvas.BehaviourTrees;
- using KairoEngine.Core;
- using KairoEngine.CharacterSystem;
- namespace KairoEngine.CharacterSystem
- {
- public enum CharacterType
- {
- Player,
- NPC
- }
- [HideMonoScript]
- public class SpawnerSingle : MonoBehaviour
- {
-
- [HideIf("@characterRandom != null")] public CharacterData characterData;
- [HideIf("@characterData != null")] public CharacterRandom characterRandom;
- [HorizontalGroup("id"), ShowIf("@characterRandom != null && characterData == null")] public string id;
- [HorizontalGroup("id", 90), Button("Randomize"), ShowIf("@characterRandom != null && characterData == null")]
- public void RandomizeId() { id = KairoEngine.Core.Utilities.RandomString(12); }
- public CharacterType characterType = CharacterType.NPC;
- [ShowIf("@characterType == CharacterType.NPC")] public Transform waypoint;
- public float wait = 0.1f;
- private float timer = 0f;
- private bool done = false;
- void Update()
- {
- if(!done)
- {
- timer += Time.deltaTime;
- if(timer > wait)
- {
- CreateCharacter();
- done = true;
- }
- }
-
- }
- private void CreateCharacter()
- {
- CharacterController character = null;
- // Spawn character from data
- if(characterData != null)
- {
- if(CharacterManager.nameIsRegistered($"{characterData.name}_{characterData.id}"))
- {
- Debug.Log($"{characterData.name}_{characterData.id} has already been created.");
- return;
- }
- character = CharacterGenerator.Create(characterData, transform.position, transform.rotation);
- }
- // Spawn random character
- else if (characterRandom != null)
- {
- CharacterData data = characterRandom.Generate(id);
- if(CharacterManager.nameIsRegistered($"{data.name}_{data.id}"))
- {
- Debug.Log($"{data.name}_{data.id} has already been created.");
- return;
- }
- character = CharacterGenerator.Create(data, transform.position, transform.rotation);
- }
- // Set Character to follow waypoint
- if(character != null && waypoint != null && characterType == CharacterType.NPC)
- {
- Blackboard blackboard = character.GetComponent<Blackboard>();
- blackboard.SetVariableValue("currentWaypoint", waypoint);
- BehaviourTreeOwner behaviour = character.GetComponent<BehaviourTreeOwner>();
- behaviour.enabled = true;
- NavMeshAgent navMeshAgent = character.GetComponent<NavMeshAgent>();
- navMeshAgent.enabled = true;
- }
- if(characterType == CharacterType.Player)
- {
- EventManager.broadcast.Trigger("PlayerCharacter-Instantiated", character);
- }
- }
- }
- }
|