2024-04-04 12:05:18 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-04-05 14:45:41 +02:00
|
|
|
using System.ComponentModel;
|
2024-04-05 16:14:51 +02:00
|
|
|
using Unity.PlasticSCM.Editor.WebApi;
|
2024-04-04 12:05:18 +02:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class NPC_Behavior : MonoBehaviour
|
|
|
|
{
|
2024-04-05 16:14:51 +02:00
|
|
|
[SerializeField] GameObject confettiEffect;
|
2024-04-05 21:10:10 +02:00
|
|
|
[SerializeField] double eventRate = 1.0; // max 60, min 0 -> how many events are fired per minute
|
2024-04-05 20:16:15 +02:00
|
|
|
[SerializeField] private float _startTime = 30.0f;
|
2024-04-04 12:05:18 +02:00
|
|
|
|
2024-04-05 18:11:26 +02:00
|
|
|
private NPC_EventStack _eventStack;
|
|
|
|
private Text2Speech _text2Speech;
|
|
|
|
private AudioSource _audioSource;
|
2024-04-05 16:14:51 +02:00
|
|
|
|
2024-04-05 18:11:26 +02:00
|
|
|
[SerializeField] private bool _talk = false;
|
2024-04-05 17:29:05 +02:00
|
|
|
[SerializeField] private bool _fullfillNeedManually = false;
|
2024-04-05 14:45:41 +02:00
|
|
|
[SerializeField] private float _timer;
|
2024-04-05 17:29:05 +02:00
|
|
|
[SerializeField] private float _newNeedDelay = 3.0f;
|
2024-04-05 16:14:51 +02:00
|
|
|
[SerializeField] private List<string> _lastTenNeeds = new List<string>();
|
|
|
|
private DeveloperNeeds _developerNeeds;
|
2024-04-05 14:45:41 +02:00
|
|
|
private float _timeBetweenEvents;
|
2024-04-05 16:14:51 +02:00
|
|
|
private GameObject _currentNeed = null;
|
2024-04-05 17:29:05 +02:00
|
|
|
private float _newNeedDelayTimer = 0.0f;
|
|
|
|
private float _effectCreationTime;
|
|
|
|
private GameObject _effect;
|
2024-04-05 16:14:51 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The name of the current need
|
|
|
|
/// </summary>
|
|
|
|
public string CurrentNeed => _lastTenNeeds[_lastTenNeeds.Count - 1];
|
|
|
|
/// <summary>
|
|
|
|
/// Indicates if the developer has a need right now
|
|
|
|
/// </summary>
|
|
|
|
public bool HasNeed = false;
|
2024-04-05 13:18:15 +02:00
|
|
|
|
2024-04-04 12:05:18 +02:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2024-04-05 14:08:45 +02:00
|
|
|
_developerNeeds = GetComponent<DeveloperNeeds>();
|
2024-04-05 18:11:26 +02:00
|
|
|
_eventStack = GetComponent<NPC_EventStack>();
|
|
|
|
_text2Speech = GetComponent<Text2Speech>();
|
|
|
|
_audioSource = GetComponent<AudioSource>();
|
2024-04-05 20:16:15 +02:00
|
|
|
ResetTimer(_startTime);
|
2024-04-04 12:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2024-04-05 17:29:05 +02:00
|
|
|
WatchEffect();
|
2024-04-05 14:45:41 +02:00
|
|
|
_timer -= Time.deltaTime;
|
2024-04-05 16:14:51 +02:00
|
|
|
|
2024-04-05 17:29:05 +02:00
|
|
|
if (_newNeedDelayTimer <= 0)
|
2024-04-05 16:14:51 +02:00
|
|
|
{
|
2024-04-05 17:29:05 +02:00
|
|
|
if (_timer <= 0 && _currentNeed == null)
|
2024-04-05 16:14:51 +02:00
|
|
|
{
|
2024-04-05 17:29:05 +02:00
|
|
|
GenerateNeed();
|
|
|
|
ResetTimer();
|
2024-04-05 16:14:51 +02:00
|
|
|
}
|
2024-04-05 17:29:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_newNeedDelayTimer -= Time.deltaTime;
|
2024-04-05 14:45:41 +02:00
|
|
|
}
|
2024-04-05 16:14:51 +02:00
|
|
|
|
|
|
|
// for Debugging
|
2024-04-05 17:29:05 +02:00
|
|
|
if (_fullfillNeedManually)
|
2024-04-05 16:14:51 +02:00
|
|
|
{
|
2024-04-05 17:29:05 +02:00
|
|
|
_fullfillNeedManually = false;
|
2024-04-05 16:14:51 +02:00
|
|
|
NeedFullfilled();
|
|
|
|
}
|
2024-04-05 18:11:26 +02:00
|
|
|
if (_talk)
|
|
|
|
{
|
|
|
|
_talk = false;
|
|
|
|
Talk();
|
|
|
|
}
|
2024-04-05 14:45:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 20:16:15 +02:00
|
|
|
private void ResetTimer(float startTime = 0.0f)
|
2024-04-05 14:45:41 +02:00
|
|
|
{
|
|
|
|
if (eventRate <= 0)
|
|
|
|
{
|
|
|
|
_timeBetweenEvents = float.MaxValue;
|
|
|
|
}
|
|
|
|
else
|
2024-04-05 13:18:15 +02:00
|
|
|
{
|
2024-04-05 14:45:41 +02:00
|
|
|
_timeBetweenEvents = 60f / (float)eventRate;
|
2024-04-05 20:16:15 +02:00
|
|
|
if (startTime > 0)
|
|
|
|
{
|
|
|
|
_timer = startTime;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_timer = Random.Range(0.5f * _timeBetweenEvents, 1.25f * _timeBetweenEvents);
|
|
|
|
}
|
2024-04-05 17:29:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WatchEffect()
|
|
|
|
{
|
|
|
|
if (_effect != null && GetEffectLifetime() >= 3.0f)
|
|
|
|
{
|
|
|
|
RemoveEffect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private float GetEffectLifetime()
|
|
|
|
{
|
|
|
|
return Time.time - _effectCreationTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool RemoveEffect()
|
|
|
|
{
|
|
|
|
Destroy(_effect);
|
|
|
|
return _effect != null;
|
|
|
|
}
|
|
|
|
|
2024-04-06 14:06:11 +02:00
|
|
|
public bool GenerateNeed(string needName = null)
|
2024-04-05 17:29:05 +02:00
|
|
|
{
|
|
|
|
List<string> needs = new List<string>() { "coffee", "mate", "toilet", "money" };
|
|
|
|
|
|
|
|
string need = needs[UnityEngine.Random.Range(0, needs.Count)];
|
2024-04-06 14:06:11 +02:00
|
|
|
if (needName != null)
|
|
|
|
{
|
|
|
|
need = needName;
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:30:26 +02:00
|
|
|
_currentNeed = _developerNeeds.spawnNeed(need, 0);
|
2024-04-05 17:29:05 +02:00
|
|
|
HasNeed = true;
|
|
|
|
_lastTenNeeds.Add(need);
|
|
|
|
if (_lastTenNeeds.Count > 10)
|
|
|
|
{
|
|
|
|
_lastTenNeeds.RemoveAt(0);
|
2024-04-05 13:18:15 +02:00
|
|
|
}
|
2024-04-05 17:29:05 +02:00
|
|
|
return _currentNeed != null;
|
2024-04-04 12:05:18 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 16:14:51 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Deletes the current need and its gameobject.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>bool: wether successfully deleted the gameobject or not</returns>
|
2024-04-05 19:24:26 +02:00
|
|
|
[ContextMenu("Fullfill Need")]
|
2024-04-05 16:14:51 +02:00
|
|
|
public bool NeedFullfilled()
|
|
|
|
{
|
|
|
|
Destroy(_currentNeed);
|
|
|
|
if (HasNeed)
|
|
|
|
{
|
|
|
|
HasNeed = false;
|
2024-04-05 17:29:05 +02:00
|
|
|
_newNeedDelayTimer = _newNeedDelay;
|
|
|
|
_effect = Instantiate(confettiEffect, new Vector3(0.0f, 1.5f, 0.0f), confettiEffect.transform.rotation);
|
|
|
|
_effect.transform.SetParent(transform, false);
|
|
|
|
_effectCreationTime = Time.time;
|
2024-04-05 16:14:51 +02:00
|
|
|
}
|
|
|
|
return _currentNeed == null;
|
|
|
|
}
|
|
|
|
|
2024-04-05 18:11:26 +02:00
|
|
|
public void Talk()
|
|
|
|
{
|
|
|
|
//if (!_audioSource.isPlaying)
|
|
|
|
//{
|
|
|
|
string context = _eventStack.GetEntireContext();
|
|
|
|
_text2Speech.Generate(context);
|
|
|
|
//}
|
|
|
|
}
|
2024-04-04 12:05:18 +02:00
|
|
|
}
|